mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-09-22 13:39:35 +00:00
Updated jobs to work without cron
This commit is contained in:
@@ -1,42 +1,42 @@
|
|||||||
import { ISecret } from "../models";
|
import { ISecret } from "../models";
|
||||||
import {
|
import {
|
||||||
createSecretReminderCron,
|
createRecurringSecretReminder,
|
||||||
deleteSecretReminderCron,
|
deleteRecurringSecretReminder,
|
||||||
updateSecretReminderCron
|
updateRecurringSecretReminder
|
||||||
} from "../queues/reminders/sendSecretReminders";
|
} from "../queues/reminders/sendSecretReminders";
|
||||||
|
|
||||||
type TPartialSecret = Pick<
|
type TPartialSecret = Pick<
|
||||||
ISecret,
|
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) => {
|
export const createReminder = async (oldSecret: TPartialSecret, newSecret: TPartialSecret) => {
|
||||||
if (oldSecret._id !== newSecret._id) {
|
if (oldSecret._id !== newSecret._id) {
|
||||||
throw new Error("Secret id's don't match");
|
throw new Error("Secret id's don't match");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!newSecret.secretReminderCron) {
|
if (!newSecret.secretReminderRepeatDays) {
|
||||||
throw new Error("No cron provided");
|
throw new Error("No repeat days provided");
|
||||||
}
|
}
|
||||||
|
|
||||||
const secretId = oldSecret._id.toString();
|
const secretId = oldSecret._id.toString();
|
||||||
const workspaceId = oldSecret.workspace.toString();
|
const workspaceId = oldSecret.workspace.toString();
|
||||||
|
|
||||||
if (oldSecret.secretReminderCron) {
|
if (oldSecret.secretReminderRepeatDays) {
|
||||||
// This will first delete the existing cron job, and then create a new one.
|
// This will first delete the existing recurring job, and then create a new one.
|
||||||
await updateSecretReminderCron({
|
await updateRecurringSecretReminder({
|
||||||
workspaceId,
|
workspaceId,
|
||||||
secretId,
|
secretId,
|
||||||
cron: newSecret.secretReminderCron,
|
repeatDays: newSecret.secretReminderRepeatDays,
|
||||||
note: newSecret.secretReminderNote
|
note: newSecret.secretReminderNote
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// This will create a new cron job.
|
// This will create a new recurring job.
|
||||||
await createSecretReminderCron({
|
await createRecurringSecretReminder({
|
||||||
workspaceId,
|
workspaceId,
|
||||||
secretId,
|
secretId,
|
||||||
cron: newSecret.secretReminderCron,
|
repeatDays: newSecret.secretReminderRepeatDays,
|
||||||
note: newSecret.secretReminderNote
|
note: newSecret.secretReminderNote
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -47,12 +47,12 @@ export const deleteReminder = async (secret: TPartialSecretDeleteReminder) => {
|
|||||||
throw new Error("No secret id provided");
|
throw new Error("No secret id provided");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!secret.secretReminderCron) {
|
if (!secret.secretReminderRepeatDays) {
|
||||||
throw new Error("No cron provided");
|
throw new Error("No repeat days provided");
|
||||||
}
|
}
|
||||||
|
|
||||||
await deleteSecretReminderCron({
|
await deleteRecurringSecretReminder({
|
||||||
secretId: secret._id.toString(),
|
secretId: secret._id.toString(),
|
||||||
cron: secret.secretReminderCron
|
repeatDays: secret.secretReminderRepeatDays
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -6,15 +6,17 @@ import { sendMail } from "../../helpers";
|
|||||||
type TSendSecretReminders = {
|
type TSendSecretReminders = {
|
||||||
workspaceId: string;
|
workspaceId: string;
|
||||||
secretId: string;
|
secretId: string;
|
||||||
cron: string;
|
repeatDays: number;
|
||||||
note: string | undefined | null;
|
note: string | undefined | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
type TDeleteSecretReminder = {
|
type TDeleteSecretReminder = {
|
||||||
secretId: string;
|
secretId: string;
|
||||||
cron: string;
|
repeatDays: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const DAY_IN_MS = 86400000;
|
||||||
|
|
||||||
export const sendSecretReminders = new Queue(
|
export const sendSecretReminders = new Queue(
|
||||||
"send-secret-reminders",
|
"send-secret-reminders",
|
||||||
process.env.REDIS_URL as string
|
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, {
|
return sendSecretReminders.add(jobDetails, {
|
||||||
|
delay: repeat,
|
||||||
repeat: {
|
repeat: {
|
||||||
cron: jobDetails.cron
|
every: repeat
|
||||||
},
|
},
|
||||||
jobId: `reminder-${jobDetails.secretId}`,
|
jobId: `reminder-${jobDetails.secretId}`,
|
||||||
removeOnComplete: true,
|
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({
|
return sendSecretReminders.removeRepeatable({
|
||||||
cron: jobDetails.cron,
|
every: repeat,
|
||||||
jobId: `reminder-${jobDetails.secretId}`
|
jobId: `reminder-${jobDetails.secretId}`
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const updateSecretReminderCron = async (jobDetails: TSendSecretReminders) => {
|
export const updateRecurringSecretReminder = async (jobDetails: TSendSecretReminders) => {
|
||||||
// We need to delete the potentially existing cron job first, or the new one won't be created.
|
// We need to delete the potentially existing reminder job first, or the new one won't be created.
|
||||||
await deleteSecretReminderCron(jobDetails);
|
await deleteRecurringSecretReminder(jobDetails);
|
||||||
|
await createRecurringSecretReminder(jobDetails);
|
||||||
await createSecretReminderCron(jobDetails);
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user