From 833b960a48caa6b494f89f26cb3608300956baca Mon Sep 17 00:00:00 2001 From: undefined Date: Wed, 5 Oct 2022 18:07:16 +0800 Subject: [PATCH] plugins: mark entrypoint as index --- packages/center/package.json | 2 +- packages/elastic/package.json | 2 +- packages/import-qduoj/package.json | 2 +- packages/login-with-github/package.json | 2 +- packages/login-with-google/package.json | 2 +- packages/migrate/package.json | 2 +- packages/onlyoffice/{handler.ts => index.ts} | 0 packages/onlyoffice/package.json | 2 +- packages/prom-client/{handler.ts => index.ts} | 5 +- packages/prom-client/metrics.ts | 117 +++++++++--------- packages/prom-client/package.json | 1 + packages/recaptcha/package.json | 2 +- packages/sonic/package.json | 2 +- packages/vjudge/src/model.ts | 21 ++-- 14 files changed, 81 insertions(+), 81 deletions(-) rename packages/onlyoffice/{handler.ts => index.ts} (100%) rename packages/prom-client/{handler.ts => index.ts} (95%) diff --git a/packages/center/package.json b/packages/center/package.json index cb16b3495..64d2fabed 100644 --- a/packages/center/package.json +++ b/packages/center/package.json @@ -1,7 +1,7 @@ { "name": "@hydrooj/center", "version": "0.1.3", - "main": "package.json", + "main": "index.ts", "repository": "https://github.com/hydro-dev/Hydro.git", "author": "undefined ", "license": "AGPL-3.0-or-later", diff --git a/packages/elastic/package.json b/packages/elastic/package.json index 24db60871..148a0ae0f 100644 --- a/packages/elastic/package.json +++ b/packages/elastic/package.json @@ -1,7 +1,7 @@ { "name": "@hydrooj/elastic-search", "version": "1.0.8", - "main": "package.json", + "main": "index.ts", "repository": "https://github.com/hydro-dev/Hydro", "author": "undefined ", "license": "AGPL-3.0-or-later", diff --git a/packages/import-qduoj/package.json b/packages/import-qduoj/package.json index b36b03422..b5d0aef27 100644 --- a/packages/import-qduoj/package.json +++ b/packages/import-qduoj/package.json @@ -2,7 +2,7 @@ "name": "@hydrooj/import-qduoj", "version": "1.3.4", "description": "Import QDUOJ problem export", - "main": "package.json", + "main": "index.ts", "repository": "https://github.com/hydro-dev/Hydro.git", "author": "undefined ", "license": "AGPL-3.0-or-later", diff --git a/packages/login-with-github/package.json b/packages/login-with-github/package.json index bf2e1f5dd..e07e64e93 100644 --- a/packages/login-with-github/package.json +++ b/packages/login-with-github/package.json @@ -1,7 +1,7 @@ { "name": "@hydrooj/login-with-github", "version": "0.1.6", - "main": "package.json", + "main": "index.ts", "repository": "git@github.com:hydro-dev/Hydro.git", "author": "undefined ", "license": "AGPL-3.0-or-later", diff --git a/packages/login-with-google/package.json b/packages/login-with-google/package.json index 9c315542d..db122f75b 100644 --- a/packages/login-with-google/package.json +++ b/packages/login-with-google/package.json @@ -1,7 +1,7 @@ { "name": "@hydrooj/login-with-google", "version": "0.1.1", - "main": "package.json", + "main": "index.ts", "repository": "git@github.com:hydro-dev/Hydro.git", "author": "undefined ", "license": "AGPL-3.0-or-later", diff --git a/packages/migrate/package.json b/packages/migrate/package.json index 23f08b50c..58caa76a3 100644 --- a/packages/migrate/package.json +++ b/packages/migrate/package.json @@ -1,7 +1,7 @@ { "name": "@hydrooj/migrate", "version": "0.1.5", - "main": "package.json", + "main": "index.ts", "repository": "git@github.com:hydro-dev/Hydro.git", "author": "undefined ", "license": "AGPL-3.0-or-later", diff --git a/packages/onlyoffice/handler.ts b/packages/onlyoffice/index.ts similarity index 100% rename from packages/onlyoffice/handler.ts rename to packages/onlyoffice/index.ts diff --git a/packages/onlyoffice/package.json b/packages/onlyoffice/package.json index f4edb4316..898902737 100644 --- a/packages/onlyoffice/package.json +++ b/packages/onlyoffice/package.json @@ -1,7 +1,7 @@ { "name": "@hydrooj/onlyoffice", "version": "1.2.0", - "main": "package.json", + "main": "index.ts", "repository": "git@github.com:hydro-dev/Hydro.git", "author": "undefined ", "preferUnplugged": true, diff --git a/packages/prom-client/handler.ts b/packages/prom-client/index.ts similarity index 95% rename from packages/prom-client/handler.ts rename to packages/prom-client/index.ts index 193a322ec..8f5f88b87 100644 --- a/packages/prom-client/handler.ts +++ b/packages/prom-client/index.ts @@ -1,13 +1,11 @@ import { AggregatorRegistry, metric } from 'prom-client'; import { Context, Handler, SystemModel } from 'hydrooj'; -import { registry } from './metrics'; +import { createRegistry } from './metrics'; declare module 'hydrooj' { interface EventMap { metrics: (id: string, metrics: any) => void; } -} -declare module 'hydrooj' { interface SystemKeys { 'prom-client.name': string; 'prom-client.password': string; @@ -41,6 +39,7 @@ class MetricsHandler extends Handler { } export function apply(ctx: Context) { + const registry = createRegistry(ctx); ctx.on('metrics', (id, metrics) => { instances[id] = metrics; }); ctx.setInterval(async () => { ctx.broadcast('metrics', process.env.NODE_APP_INSTANCE!, await registry.getMetricsAsJSON()); diff --git a/packages/prom-client/metrics.ts b/packages/prom-client/metrics.ts index f89473e6d..286e07f45 100644 --- a/packages/prom-client/metrics.ts +++ b/packages/prom-client/metrics.ts @@ -1,62 +1,65 @@ import { collectDefaultMetrics, Counter, Gauge, Metric, Registry, } from 'prom-client'; -import { db } from 'hydrooj'; -import * as bus from 'hydrooj/src/service/bus'; +import { Context, db } from 'hydrooj'; -export const registry = new Registry(); -registry.setDefaultLabels({ instanceid: process.env.NODE_APP_INSTANCE }); -function createMetric Metric)>( - C: T, name: string, help: string, extra?: T extends new (a: infer R) => any ? Partial : never, -): T extends (new (a) => Gauge) ? Gauge : T extends (new (a) => Counter) ? Counter : Metric { - const metric = new C({ name, help, ...(extra || {}) }); - registry.registerMetric(metric); - return metric as any; +export function createRegistry(ctx: Context) { + const registry = new Registry(); + + registry.setDefaultLabels({ instanceid: process.env.NODE_APP_INSTANCE }); + function createMetric Metric)>( + C: T, name: string, help: string, extra?: T extends new (a: infer R) => any ? Partial : never, + ): T extends (new (a) => Gauge) ? Gauge : T extends (new (a) => Counter) ? Counter : Metric { + const metric = new C({ name, help, ...(extra || {}) }); + registry.registerMetric(metric); + return metric as any; + } + + const reqCounter = createMetric(Counter, 'hydro_reqcount', 'reqcount', { + labelNames: ['domainId'], + }); + ctx.on('handler/create', (h) => reqCounter.inc({ domainId: h.args.domainId })); + + const judgeCounter = createMetric(Counter, 'hydro_judgecount', 'judgecount'); + ctx.on('record/judge', () => judgeCounter.inc()); + + createMetric(Gauge, 'hydro_regcount', 'regcount', { + async collect() { + this.set({}, await db.collection('user').countDocuments()); + }, + }); + + const submissionCounter = createMetric(Counter, 'hydro_submission', 'submissioncount', { + labelNames: ['lang', 'domainId'], + }); + ctx.on('handler/after/ProblemSubmit', (that) => { + submissionCounter.inc({ lang: that.args.lang, domainId: that.args.domainId }); + }); + + const connectionGauge = createMetric(Gauge, 'hydro_connection', 'connectioncount', { + labelNames: ['domainId'], + }); + ctx.on('connection/active', (h) => { + connectionGauge.inc({ domainId: h.args.domainId }); + }); + ctx.on('connection/close', (h) => { + connectionGauge.dec({ domainId: h.args.domainId }); + }); + + const taskColl = db.collection('task'); + createMetric(Gauge, 'hydro_task', 'taskcount', { + async collect() { + const data = await taskColl.aggregate([ + { $group: { _id: '$type', count: { $sum: 1 } } }, + ]).toArray(); + this.reset(); + for (const line of data) { + this.set({ type: line._id as unknown as string }, line.count); + } + }, + labelNames: ['type'], + }); + + collectDefaultMetrics({ register: registry }); + return registry; } - -const reqCounter = createMetric(Counter, 'hydro_reqcount', 'reqcount', { - labelNames: ['domainId'], -}); -bus.on('handler/create', (h) => reqCounter.inc({ domainId: h.args.domainId })); - -const judgeCounter = createMetric(Counter, 'hydro_judgecount', 'judgecount'); -bus.on('record/judge', () => judgeCounter.inc()); - -createMetric(Gauge, 'hydro_regcount', 'regcount', { - async collect() { - this.set({}, await db.collection('user').countDocuments()); - }, -}); - -const submissionCounter = createMetric(Counter, 'hydro_submission', 'submissioncount', { - labelNames: ['lang', 'domainId'], -}); -bus.on('handler/after/ProblemSubmit', (that) => { - submissionCounter.inc({ lang: that.args.lang, domainId: that.args.domainId }); -}); - -const connectionGauge = createMetric(Gauge, 'hydro_connection', 'connectioncount', { - labelNames: ['domainId'], -}); -bus.on('connection/active', (h) => { - connectionGauge.inc({ domainId: h.args.domainId }); -}); -bus.on('connection/close', (h) => { - connectionGauge.dec({ domainId: h.args.domainId }); -}); - -const taskColl = db.collection('task'); -createMetric(Gauge, 'hydro_task', 'taskcount', { - async collect() { - const data = await taskColl.aggregate([ - { $group: { _id: '$type', count: { $sum: 1 } } }, - ]).toArray(); - this.reset(); - for (const line of data) { - this.set({ type: line._id as unknown as string }, line.count); - } - }, - labelNames: ['type'], -}); - -collectDefaultMetrics({ register: registry }); diff --git a/packages/prom-client/package.json b/packages/prom-client/package.json index 3e3e971a4..08d81e6a3 100644 --- a/packages/prom-client/package.json +++ b/packages/prom-client/package.json @@ -1,6 +1,7 @@ { "name": "@hydrooj/prom-client", "version": "0.0.4", + "main": "index.ts", "dependencies": { "prom-client": "^14.1.0" } diff --git a/packages/recaptcha/package.json b/packages/recaptcha/package.json index 4ab2518dd..6ac55db7f 100644 --- a/packages/recaptcha/package.json +++ b/packages/recaptcha/package.json @@ -2,7 +2,7 @@ "name": "@hydrooj/recaptcha", "version": "1.1.0", "description": "Google reCAPTCHA", - "main": "package.json", + "main": "index.ts", "repository": "https://github.com/hydro-dev/Hydro.git", "author": "undefined", "license": "AGPL-3.0-or-later", diff --git a/packages/sonic/package.json b/packages/sonic/package.json index 3bdfc12a4..7b2e7a33c 100644 --- a/packages/sonic/package.json +++ b/packages/sonic/package.json @@ -2,7 +2,7 @@ "name": "@hydrooj/sonic", "version": "1.2.7", "description": "Sonic search service", - "main": "service.ts", + "main": "index.ts", "preferUnplugged": true, "repository": "https://github.com/hydro-dev/Hydro.git", "author": "undefined ", diff --git a/packages/vjudge/src/model.ts b/packages/vjudge/src/model.ts index 9f42ab827..ef36b8428 100644 --- a/packages/vjudge/src/model.ts +++ b/packages/vjudge/src/model.ts @@ -121,17 +121,6 @@ class Service { } } -async function loadAccounts() { - // Only start a single daemon - if (process.env.NODE_APP_INSTANCE !== '0') return; - const accounts = await coll.find().toArray(); - for (const account of accounts) { - if (!providers[account.type]) continue; - if (account.enableOn && !account.enableOn.includes(os.hostname())) continue; - Pool[`${account.type}/${account.handle}`] = new Service(providers[account.type], account); - } -} - declare module 'hydrooj' { interface Model { vjudge: VJudgeModel; @@ -151,5 +140,13 @@ class VJudgeModel { global.Hydro.model.vjudge = VJudgeModel; export function apply(ctx: Context) { - ctx.on('ready', loadAccounts); + if (process.env.NODE_APP_INSTANCE !== '0') return; + ctx.on('ready', async () => { + const accounts = await coll.find().toArray(); + for (const account of accounts) { + if (!providers[account.type]) continue; + if (account.enableOn && !account.enableOn.includes(os.hostname())) continue; + Pool[`${account.type}/${account.handle}`] = new Service(providers[account.type], account); + } + }); }