From aee91a9558387eea46aa7be59c003f7809dedd2d Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Tue, 28 Nov 2023 13:50:10 +0400 Subject: [PATCH] Updated jobs to work without cron --- backend/src/helpers/reminder.ts | 36 +++++++++---------- .../queues/reminders/sendSecretReminders.ts | 28 +++++++++------ 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/backend/src/helpers/reminder.ts b/backend/src/helpers/reminder.ts index aebb8cbd4..d896a2f3d 100644 --- a/backend/src/helpers/reminder.ts +++ b/backend/src/helpers/reminder.ts @@ -1,42 +1,42 @@ import { ISecret } from "../models"; import { - createSecretReminderCron, - deleteSecretReminderCron, - updateSecretReminderCron + createRecurringSecretReminder, + deleteRecurringSecretReminder, + updateRecurringSecretReminder } from "../queues/reminders/sendSecretReminders"; type TPartialSecret = Pick< ISecret, - "_id" | "secretReminderCron" | "secretReminderNote" | "workspace" + "_id" | "secretReminderRepeatDays" | "secretReminderNote" | "workspace" >; -type TPartialSecretDeleteReminder = Pick; +type TPartialSecretDeleteReminder = Pick; export const createReminder = async (oldSecret: TPartialSecret, newSecret: TPartialSecret) => { if (oldSecret._id !== newSecret._id) { throw new Error("Secret id's don't match"); } - if (!newSecret.secretReminderCron) { - throw new Error("No cron provided"); + if (!newSecret.secretReminderRepeatDays) { + throw new Error("No repeat days provided"); } const secretId = oldSecret._id.toString(); const workspaceId = oldSecret.workspace.toString(); - if (oldSecret.secretReminderCron) { - // This will first delete the existing cron job, and then create a new one. - await updateSecretReminderCron({ + if (oldSecret.secretReminderRepeatDays) { + // This will first delete the existing recurring job, and then create a new one. + await updateRecurringSecretReminder({ workspaceId, secretId, - cron: newSecret.secretReminderCron, + repeatDays: newSecret.secretReminderRepeatDays, note: newSecret.secretReminderNote }); } else { - // This will create a new cron job. - await createSecretReminderCron({ + // This will create a new recurring job. + await createRecurringSecretReminder({ workspaceId, secretId, - cron: newSecret.secretReminderCron, + repeatDays: newSecret.secretReminderRepeatDays, note: newSecret.secretReminderNote }); } @@ -47,12 +47,12 @@ export const deleteReminder = async (secret: TPartialSecretDeleteReminder) => { throw new Error("No secret id provided"); } - if (!secret.secretReminderCron) { - throw new Error("No cron provided"); + if (!secret.secretReminderRepeatDays) { + throw new Error("No repeat days provided"); } - await deleteSecretReminderCron({ + await deleteRecurringSecretReminder({ secretId: secret._id.toString(), - cron: secret.secretReminderCron + repeatDays: secret.secretReminderRepeatDays }); }; diff --git a/backend/src/queues/reminders/sendSecretReminders.ts b/backend/src/queues/reminders/sendSecretReminders.ts index 6ec117b78..0cf4a79a4 100644 --- a/backend/src/queues/reminders/sendSecretReminders.ts +++ b/backend/src/queues/reminders/sendSecretReminders.ts @@ -6,15 +6,17 @@ import { sendMail } from "../../helpers"; type TSendSecretReminders = { workspaceId: string; secretId: string; - cron: string; + repeatDays: number; note: string | undefined | null; }; type TDeleteSecretReminder = { secretId: string; - cron: string; + repeatDays: number; }; +const DAY_IN_MS = 86400000; + export const sendSecretReminders = new Queue( "send-secret-reminders", process.env.REDIS_URL as string @@ -49,10 +51,13 @@ sendSecretReminders.process(async (job: Job) => { }); }); -export const createSecretReminderCron = (jobDetails: TSendSecretReminders) => { +export const createRecurringSecretReminder = (jobDetails: TSendSecretReminders) => { + const repeat = jobDetails.repeatDays * DAY_IN_MS; + return sendSecretReminders.add(jobDetails, { + delay: repeat, repeat: { - cron: jobDetails.cron + every: repeat }, jobId: `reminder-${jobDetails.secretId}`, removeOnComplete: true, @@ -62,16 +67,17 @@ export const createSecretReminderCron = (jobDetails: TSendSecretReminders) => { }); }; -export const deleteSecretReminderCron = (jobDetails: TDeleteSecretReminder) => { +export const deleteRecurringSecretReminder = (jobDetails: TDeleteSecretReminder) => { + const repeat = jobDetails.repeatDays * DAY_IN_MS; + return sendSecretReminders.removeRepeatable({ - cron: jobDetails.cron, + every: repeat, jobId: `reminder-${jobDetails.secretId}` }); }; -export const updateSecretReminderCron = async (jobDetails: TSendSecretReminders) => { - // We need to delete the potentially existing cron job first, or the new one won't be created. - await deleteSecretReminderCron(jobDetails); - - await createSecretReminderCron(jobDetails); +export const updateRecurringSecretReminder = async (jobDetails: TSendSecretReminders) => { + // We need to delete the potentially existing reminder job first, or the new one won't be created. + await deleteRecurringSecretReminder(jobDetails); + await createRecurringSecretReminder(jobDetails); };