diff options
author | Miodec <[email protected]> | 2023-03-06 15:40:53 +0100 |
---|---|---|
committer | Miodec <[email protected]> | 2023-03-06 15:40:53 +0100 |
commit | 85606bcb6c73acd3860c8b2025aff897f4aa58c9 (patch) | |
tree | 52fee84fb6a1170538f24acee4fbe979d974ce98 | |
parent | a0d7af31d8d248e93aa36b447505e2902545417e (diff) | |
download | monkeytype-85606bcb6c73acd3860c8b2025aff897f4aa58c9.tar.gz monkeytype-85606bcb6c73acd3860c8b2025aff897f4aa58c9.zip |
added strong types to later queue
closes #3966
-rw-r--r-- | backend/src/queues/later-queue.ts | 40 | ||||
-rw-r--r-- | backend/src/utils/daily-leaderboards.ts | 4 | ||||
-rw-r--r-- | backend/src/workers/later-worker.ts | 28 |
3 files changed, 39 insertions, 33 deletions
diff --git a/backend/src/queues/later-queue.ts b/backend/src/queues/later-queue.ts index 8a556a67e..56993b5ec 100644 --- a/backend/src/queues/later-queue.ts +++ b/backend/src/queues/later-queue.ts @@ -5,24 +5,36 @@ import { getCurrentDayTimestamp, getCurrentWeekTimestamp } from "../utils/misc"; const QUEUE_NAME = "later"; -type LaterTasks = "daily-leaderboard-results" | "weekly-xp-leaderboard-results"; +export type LaterTaskType = + | "daily-leaderboard-results" + | "weekly-xp-leaderboard-results"; -export interface LaterTask { - taskName: LaterTasks; - ctx: any; +export interface LaterTask<T extends LaterTaskType> { + taskName: LaterTaskType; + ctx: LaterTaskContexts[T]; } +export type LaterTaskContexts = { + "daily-leaderboard-results": { + yesterdayTimestamp: number; + modeRule: MonkeyTypes.ValidModeRule; + }; + "weekly-xp-leaderboard-results": { + lastWeekTimestamp: number; + }; +}; + const ONE_MINUTE_IN_MILLISECONDS = 1000 * 60; const ONE_DAY_IN_MILLISECONDS = 1000 * 60 * 60 * 24; -class LaterQueue extends MonkeyQueue<LaterTask> { +class LaterQueue extends MonkeyQueue<LaterTask<LaterTaskType>> { private scheduledJobCache = new LRUCache<string, boolean>({ max: 100, }); private async scheduleTask( taskName: string, - task: LaterTask, + task: LaterTask<LaterTaskType>, jobId: string, delay: number ): Promise<void> { @@ -41,9 +53,8 @@ class LaterQueue extends MonkeyQueue<LaterTask> { } async scheduleForNextWeek( - taskName: LaterTasks, - taskId: string, - taskContext?: any + taskName: LaterTaskType, + taskId: string ): Promise<void> { const currentWeekTimestamp = getCurrentWeekTimestamp(); const jobId = `${taskName}:${currentWeekTimestamp}:${taskId}`; @@ -52,10 +63,9 @@ class LaterQueue extends MonkeyQueue<LaterTask> { return; } - const task: LaterTask = { + const task: LaterTask<LaterTaskType> = { taskName, ctx: { - ...taskContext, lastWeekTimestamp: currentWeekTimestamp, }, }; @@ -70,9 +80,9 @@ class LaterQueue extends MonkeyQueue<LaterTask> { } async scheduleForTomorrow( - taskName: LaterTasks, + taskName: LaterTaskType, taskId: string, - taskContext: any + modeRule: MonkeyTypes.ValidModeRule ): Promise<void> { const currentDayTimestamp = getCurrentDayTimestamp(); const jobId = `${taskName}:${currentDayTimestamp}:${taskId}`; @@ -81,10 +91,10 @@ class LaterQueue extends MonkeyQueue<LaterTask> { return; } - const task: LaterTask = { + const task: LaterTask<LaterTaskType> = { taskName, ctx: { - ...taskContext, + modeRule, yesterdayTimestamp: currentDayTimestamp, }, }; diff --git a/backend/src/utils/daily-leaderboards.ts b/backend/src/utils/daily-leaderboards.ts index 8ae4a7860..79f5645e4 100644 --- a/backend/src/utils/daily-leaderboards.ts +++ b/backend/src/utils/daily-leaderboards.ts @@ -101,9 +101,7 @@ export class DailyLeaderboard { await LaterQueue.scheduleForTomorrow( "daily-leaderboard-results", this.leaderboardModeKey, - { - modeRule: this.modeRule, - } + this.modeRule ); } diff --git a/backend/src/workers/later-worker.ts b/backend/src/workers/later-worker.ts index dacf5c010..5857808c4 100644 --- a/backend/src/workers/later-worker.ts +++ b/backend/src/workers/later-worker.ts @@ -8,21 +8,16 @@ import { buildMonkeyMail } from "../utils/monkey-mail"; import { DailyLeaderboard } from "../utils/daily-leaderboards"; import { getCachedConfiguration } from "../init/configuration"; import { formatSeconds, getOrdinalNumberString, mapRange } from "../utils/misc"; -import LaterQueue, { LaterTask } from "../queues/later-queue"; +import LaterQueue, { + LaterTask, + LaterTaskContexts, + LaterTaskType, +} from "../queues/later-queue"; import { WeeklyXpLeaderboard } from "../services/weekly-xp-leaderboard"; import { recordTimeToCompleteJob } from "../utils/prometheus"; -interface DailyLeaderboardMailContext { - yesterdayTimestamp: number; - modeRule: MonkeyTypes.ValidModeRule; -} - -interface WeeklyXpLeaderboardResultContext { - lastWeekTimestamp: number; -} - async function handleDailyLeaderboardResults( - ctx: DailyLeaderboardMailContext + ctx: LaterTaskContexts["daily-leaderboard-results"] ): Promise<void> { const { yesterdayTimestamp, modeRule } = ctx; const { language, mode, mode2 } = modeRule; @@ -106,7 +101,7 @@ async function handleDailyLeaderboardResults( } async function handleWeeklyXpLeaderboardResults( - ctx: WeeklyXpLeaderboardResultContext + ctx: LaterTaskContexts["weekly-xp-leaderboard-results"] ): Promise<void> { const { leaderboards: { weeklyXp: weeklyXpConfig }, @@ -184,15 +179,18 @@ async function handleWeeklyXpLeaderboardResults( } async function jobHandler(job: Job): Promise<void> { - const { taskName, ctx }: LaterTask = job.data; + const { taskName, ctx }: LaterTask<LaterTaskType> = job.data; + Logger.info(`Starting job: ${taskName}`); const start = performance.now(); if (taskName === "daily-leaderboard-results") { - await handleDailyLeaderboardResults(ctx); + const taskCtx = ctx as LaterTaskContexts["daily-leaderboard-results"]; + await handleDailyLeaderboardResults(taskCtx); } else if (taskName === "weekly-xp-leaderboard-results") { - await handleWeeklyXpLeaderboardResults(ctx); + const taskCtx = ctx as LaterTaskContexts["weekly-xp-leaderboard-results"]; + await handleWeeklyXpLeaderboardResults(taskCtx); } const elapsed = performance.now() - start; |