From 65590c88f27a3d0b1756bd2ce0018a7b24451618 Mon Sep 17 00:00:00 2001 From: adithyagenie Date: Wed, 19 Jun 2024 19:34:53 +0530 Subject: [PATCH] Add auto-rescheduler and error handlers Signed-off-by: adithyagenie --- src/api/lcFetch.ts | 3 ++- src/helpers/cfSchedule.ts | 24 +++++++++++++++--------- src/helpers/lcSchedule.ts | 10 +++++++++- src/helpers/scheduler.ts | 11 ++++++++++- src/index.ts | 3 ++- 5 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/api/lcFetch.ts b/src/api/lcFetch.ts index 353f8c9..8cdd567 100644 --- a/src/api/lcFetch.ts +++ b/src/api/lcFetch.ts @@ -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}`); } } diff --git a/src/helpers/cfSchedule.ts b/src/helpers/cfSchedule.ts index 2e0645f..8af8566 100644 --- a/src/helpers/cfSchedule.ts +++ b/src/helpers/cfSchedule.ts @@ -4,15 +4,21 @@ import { reminderContestCF } from "../bot/commands/contestRem"; export async function cfSchedule() { - const contests = await cfFetchContests(); - if (contests.length === 0) { - console.log("No codeforces contests found!"); - return; + try { + const contests = await cfFetchContests(); + if (contests.length === 0) { + 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) { - 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)); + catch (e) { + console.error("Unable to fetch CodeForces API :("); + setTimeout(cfSchedule, 3000); } - console.log("Codeforces contests scheduled!"); } \ No newline at end of file diff --git a/src/helpers/lcSchedule.ts b/src/helpers/lcSchedule.ts index e93cabd..074484f 100644 --- a/src/helpers/lcSchedule.ts +++ b/src/helpers/lcSchedule.ts @@ -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+)$/) ); diff --git a/src/helpers/scheduler.ts b/src/helpers/scheduler.ts index 0e05d73..b7898d0 100644 --- a/src/helpers/scheduler.ts +++ b/src/helpers/scheduler.ts @@ -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); } \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index e54626d..4281600 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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)); \ No newline at end of file