vjudge: add CheckService (#599)

This commit is contained in:
panda authored and GitHub committed 2023-06-30 00:52:25 +08:00
1 parent 807bfb53e3
commit 9da4340a97
8 files changed
+129 -62

No files matched your search

+27
View File
@@ -15,6 +15,8 @@ class AccountService {
api: IBasicProvider;
problemLists: Set<string>;
listUpdated = false;
working = false;
error = null;
constructor(public Provider: BasicProvider, public account: RemoteAccount) {
this.api = new Provider(account, async (data) => {
@@ -23,6 +25,7 @@ class AccountService {
this.problemLists = Set.union(this.api.entryProblemLists || ['main'], this.account.problemLists || []);
this.main().catch((e) => {
logger.error(`Error occured in ${account.type}/${account.handle}`);
this.error = e.message;
console.error(e);
});
}
@@ -121,6 +124,7 @@ class AccountService {
setInterval(() => this.login(), Time.hour);
TaskModel.consume({ type: 'remotejudge', subType: this.account.type.split('.')[0] }, this.judge.bind(this), false);
const ddocs = await DomainModel.getMulti({ mount: this.account.type.split('.')[0] }).toArray();
this.working = true;
do {
this.listUpdated = false;
for (const listName of this.problemLists) {
@@ -170,6 +174,18 @@ class VJudgeService extends Service {
// TODO dispose session
});
}
async checkStatus(onCheckFunc = false) {
const res: Record<string, { working: boolean, error?: string, status?: any }> = {};
for (const [k, v] of Object.entries(this.pool)) {
res[k] = {
working: v.working,
error: v.error,
status: v.api.checkStatus ? await v.api.checkStatus(onCheckFunc) : null,
};
}
return res;
}
}
export { BasicFetcher } from './fetch';
@@ -189,4 +205,15 @@ export async function apply(ctx: Context) {
}
// });
ctx.vjudge = vjudge;
ctx.check.addChecker('Vjudge', async (_ctx, log, warn, error) => {
const working = [];
const pool = await vjudge.checkStatus(true);
for (const [k, v] of Object.entries(pool)) {
if (!v.working) error(`vjudge worker ${k}: ${v.error}`);
else working.push(k);
if (v.status) log(`vjudge worker ${k}: ${v.status}`);
}
if (!working.length) warn('no vjudge worker is working');
log(`vjudge working workers: ${working.join(', ')}`);
});
}