Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

Updated jobs to work without cron

Daniel Hougaardaee91a955838
diff --git a/backend/src/helpers/reminder.ts b/backend/src/helpers/reminder.ts
index aebb8cbd4c..d896a2f3d8 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<ISecret, "_id" | "secretReminderCron">;
+type TPartialSecretDeleteReminder = Pick<ISecret, "_id" | "secretReminderRepeatDays">;
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 6ec117b780..0cf4a79a4b 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<TSendSecretReminders>) => {
});
});
-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);
};