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 {
|
||||
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
|
||||
});
|
||||
};
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user