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();
return contestNames;
} 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,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!");
@ -16,3 +17,8 @@ export async function cfSchedule() {
}
console.log("Codeforces contests scheduled!");
}
catch (e) {
console.error("Unable to fetch CodeForces API :(");
setTimeout(cfSchedule, 3000);
}
}

View file

@ -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+)$/)
);

View file

@ -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);
}

View file

@ -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));