Add auto-rescheduler and error handlers
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
Signed-off-by: adithyagenie <adithyagenie@gmail.com>
This commit is contained in:
parent
80c923bf1c
commit
65590c88f2
5 changed files with 38 additions and 13 deletions
|
@ -35,7 +35,8 @@ export async function fetchLCContests() {
|
|||
contestNames.sort();
|
||||
return contestNames;
|
||||
} else {
|
||||
throw new Error("Can't access LeetCode API :(");
|
||||
throw new Error(`Can't access LeetCode API :(
|
||||
Error: ${resp.status} - ${resp.statusText}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ import { reminderContestCF } from "../bot/commands/contestRem";
|
|||
|
||||
|
||||
export async function cfSchedule() {
|
||||
try {
|
||||
const contests = await cfFetchContests();
|
||||
if (contests.length === 0) {
|
||||
console.log("No codeforces contests found!");
|
||||
|
@ -15,4 +16,9 @@ export async function cfSchedule() {
|
|||
scheduleJob(`${contest.name}`, contestRemTime, reminderContestCF.bind(null, contest.name, contest.id, contestTime));
|
||||
}
|
||||
console.log("Codeforces contests scheduled!");
|
||||
}
|
||||
catch (e) {
|
||||
console.error("Unable to fetch CodeForces API :(");
|
||||
setTimeout(cfSchedule, 3000);
|
||||
}
|
||||
}
|
|
@ -19,7 +19,15 @@ function getNextBiweeklyDate(): Date {
|
|||
|
||||
|
||||
export async function lcSchedule() {
|
||||
const contestNames = await fetchLCContests();
|
||||
let contestNames: {key: string, name: string}[];
|
||||
try {
|
||||
contestNames = await fetchLCContests();
|
||||
}
|
||||
catch {
|
||||
console.error(`Unable to fetch LeetCode API :(`);
|
||||
setTimeout(lcSchedule, 2000);
|
||||
return;
|
||||
}
|
||||
const biweekly = contestNames.filter(o =>
|
||||
o.key.match(/^biweekly-contest-(\d+)$/)
|
||||
);
|
||||
|
|
|
@ -1,11 +1,20 @@
|
|||
import { lcSchedule } from "./lcSchedule";
|
||||
import { ccSchedule } from "./ccSchedule";
|
||||
import { cfSchedule } from "./cfSchedule";
|
||||
import { gracefulShutdown } from "node-schedule";
|
||||
import { gracefulShutdown, RecurrenceRule, scheduleJob } from "node-schedule";
|
||||
|
||||
export async function contestScheduler() {
|
||||
await gracefulShutdown();
|
||||
await lcSchedule();
|
||||
ccSchedule();
|
||||
await cfSchedule();
|
||||
console.log("Contests rescheduled!")
|
||||
}
|
||||
|
||||
export async function renewScheduler() {
|
||||
const rule = new RecurrenceRule();
|
||||
rule.tz = "Etc/UTC";
|
||||
rule.hour = 0;
|
||||
rule.minute = 0;
|
||||
scheduleJob(rule, contestScheduler);
|
||||
}
|
|
@ -5,7 +5,7 @@ config();
|
|||
import { FastifyInstance } from "fastify";
|
||||
import { startserver } from "./api/server";
|
||||
import { botInit } from "./bot/bot";
|
||||
import { contestScheduler } from "./helpers/scheduler";
|
||||
import { contestScheduler, renewScheduler } from "./helpers/scheduler";
|
||||
|
||||
if (process.env.BOT_TOKEN === undefined ||
|
||||
process.env.RUN_METHOD === undefined ||
|
||||
|
@ -23,4 +23,5 @@ startserver().then((s) => {
|
|||
server = s;
|
||||
});
|
||||
contestScheduler().catch(e => console.error(e));
|
||||
renewScheduler().catch(e => console.error(e));
|
||||
botInit().catch(e => console.error(e));
|
Loading…
Reference in a new issue