Add auto-rescheduler and error handlers
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

Signed-off-by: adithyagenie <adithyagenie@gmail.com>
This commit is contained in:
adithyagenie 2024-06-19 19:34:53 +05:30
parent 80c923bf1c
commit 65590c88f2
Signed by: adithyagenie
GPG key ID: C66E41599E458D96
5 changed files with 38 additions and 13 deletions

View file

@ -35,7 +35,8 @@ export async function fetchLCContests() {
contestNames.sort(); contestNames.sort();
return contestNames; return contestNames;
} else { } else {
throw new Error("Can't access LeetCode API :("); throw new Error(`Can't access LeetCode API :(
Error: ${resp.status} - ${resp.statusText}`);
} }
} }

View file

@ -4,15 +4,21 @@ import { reminderContestCF } from "../bot/commands/contestRem";
export async function cfSchedule() { export async function cfSchedule() {
const contests = await cfFetchContests(); try {
if (contests.length === 0) { const contests = await cfFetchContests();
console.log("No codeforces contests found!"); if (contests.length === 0) {
return; console.log("No codeforces contests found!");
return;
}
for (let contest of contests) {
const contestRemTime = new Date((contest.startTime - 600) * 1000);
const contestTime = new Date(contest.startTime * 1000);
scheduleJob(`${contest.name}`, contestRemTime, reminderContestCF.bind(null, contest.name, contest.id, contestTime));
}
console.log("Codeforces contests scheduled!");
} }
for (let contest of contests) { catch (e) {
const contestRemTime = new Date((contest.startTime - 600) * 1000); console.error("Unable to fetch CodeForces API :(");
const contestTime = new Date(contest.startTime * 1000); setTimeout(cfSchedule, 3000);
scheduleJob(`${contest.name}`, contestRemTime, reminderContestCF.bind(null, contest.name, contest.id, contestTime));
} }
console.log("Codeforces contests scheduled!");
} }

View file

@ -19,7 +19,15 @@ function getNextBiweeklyDate(): Date {
export async function lcSchedule() { 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 => const biweekly = contestNames.filter(o =>
o.key.match(/^biweekly-contest-(\d+)$/) o.key.match(/^biweekly-contest-(\d+)$/)
); );

View file

@ -1,11 +1,20 @@
import { lcSchedule } from "./lcSchedule"; import { lcSchedule } from "./lcSchedule";
import { ccSchedule } from "./ccSchedule"; import { ccSchedule } from "./ccSchedule";
import { cfSchedule } from "./cfSchedule"; import { cfSchedule } from "./cfSchedule";
import { gracefulShutdown } from "node-schedule"; import { gracefulShutdown, RecurrenceRule, scheduleJob } from "node-schedule";
export async function contestScheduler() { export async function contestScheduler() {
await gracefulShutdown(); await gracefulShutdown();
await lcSchedule(); await lcSchedule();
ccSchedule(); ccSchedule();
await cfSchedule(); 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);
} }

View file

@ -5,7 +5,7 @@ config();
import { FastifyInstance } from "fastify"; import { FastifyInstance } from "fastify";
import { startserver } from "./api/server"; import { startserver } from "./api/server";
import { botInit } from "./bot/bot"; import { botInit } from "./bot/bot";
import { contestScheduler } from "./helpers/scheduler"; import { contestScheduler, renewScheduler } from "./helpers/scheduler";
if (process.env.BOT_TOKEN === undefined || if (process.env.BOT_TOKEN === undefined ||
process.env.RUN_METHOD === undefined || process.env.RUN_METHOD === undefined ||
@ -23,4 +23,5 @@ startserver().then((s) => {
server = s; server = s;
}); });
contestScheduler().catch(e => console.error(e)); contestScheduler().catch(e => console.error(e));
renewScheduler().catch(e => console.error(e));
botInit().catch(e => console.error(e)); botInit().catch(e => console.error(e));