feat: Add delay until action for delay app

This commit is contained in:
Faruk AYDIN
2022-12-16 03:15:30 +03:00
committed by Ali BARIN
parent 5d9ed13003
commit cb58ab5fb7
6 changed files with 81 additions and 26 deletions

View File

@@ -0,0 +1,28 @@
import defineAction from '../../../../helpers/define-action';
export default defineAction({
name: 'Delay Until',
key: 'delayUntil',
description:
'Delays the execution of the next action until a specified date.',
arguments: [
{
label: 'Delay until (Date)',
key: 'delayUntil',
type: 'string' as const,
required: true,
description: 'Delay until the date. E.g. 2022-12-18',
variables: true,
},
],
async run($) {
const { delayUntil } = $.step.parameters;
const dataItem = {
delayUntil,
};
$.setActionItem({ raw: dataItem });
},
});

View File

@@ -1,3 +1,4 @@
import delayFor from './delay-for'; import delayFor from './delay-for';
import delayUntil from './delay-until';
export default [delayFor]; export default [delayFor, delayUntil];

View File

@@ -1,21 +1,25 @@
export type TDelayForUnit = 'minutes' | 'hours' | 'days' | 'weeks'; import Step from '../models/step';
import delayForAsMilliseconds, {
TDelayForUnit,
} from './delay-for-as-milliseconds';
import delayUntilAsMilliseconds from './delay-until-as-milliseconds';
const delayAsMilliseconds = ( const delayAsMilliseconds = (step: Step) => {
delayForUnit: TDelayForUnit, let delayDuration = 0;
delayForValue: number
) => { if (step.key === 'delayFor') {
switch (delayForUnit) { const { delayForUnit, delayForValue } = step.parameters;
case 'minutes':
return delayForValue * 60 * 1000; delayDuration = delayForAsMilliseconds(
case 'hours': delayForUnit as TDelayForUnit,
return delayForValue * 60 * 60 * 1000; Number(delayForValue)
case 'days': );
return delayForValue * 24 * 60 * 60 * 1000; } else if (step.key === 'delayUntil') {
case 'weeks': const { delayUntil } = step.parameters;
return delayForValue * 7 * 24 * 60 * 60 * 1000; delayDuration = delayUntilAsMilliseconds(delayUntil as string);
default:
return 0;
} }
return delayDuration;
}; };
export default delayAsMilliseconds; export default delayAsMilliseconds;

View File

@@ -0,0 +1,21 @@
export type TDelayForUnit = 'minutes' | 'hours' | 'days' | 'weeks';
const delayAsMilliseconds = (
delayForUnit: TDelayForUnit,
delayForValue: number
) => {
switch (delayForUnit) {
case 'minutes':
return delayForValue * 60 * 1000;
case 'hours':
return delayForValue * 60 * 60 * 1000;
case 'days':
return delayForValue * 24 * 60 * 60 * 1000;
case 'weeks':
return delayForValue * 7 * 24 * 60 * 60 * 1000;
default:
return 0;
}
};
export default delayAsMilliseconds;

View File

@@ -0,0 +1,8 @@
const delayUntilAsMilliseconds = (delayUntil: string) => {
const delayUntilDate = new Date(delayUntil);
const now = new Date();
return delayUntilDate.getTime() - now.getTime();
};
export default delayUntilAsMilliseconds;

View File

@@ -8,9 +8,7 @@ import {
REMOVE_AFTER_30_DAYS_OR_150_JOBS, REMOVE_AFTER_30_DAYS_OR_150_JOBS,
REMOVE_AFTER_7_DAYS_OR_50_JOBS, REMOVE_AFTER_7_DAYS_OR_50_JOBS,
} from '../helpers/remove-job-configuration'; } from '../helpers/remove-job-configuration';
import delayAsMilliseconds, { import delayAsMilliseconds from '../helpers/delay-as-milliseconds';
TDelayForUnit,
} from '../helpers/delay-as-milliseconds';
type JobData = { type JobData = {
flowId: string; flowId: string;
@@ -47,12 +45,7 @@ export const worker = new Worker(
}; };
if (step.appKey === 'delay') { if (step.appKey === 'delay') {
const { delayForUnit, delayForValue } = step.parameters; jobOptions.delay = delayAsMilliseconds(step);
jobOptions.delay = delayAsMilliseconds(
delayForUnit as TDelayForUnit,
Number(delayForValue)
);
} }
await actionQueue.add(jobName, jobPayload, jobOptions); await actionQueue.add(jobName, jobPayload, jobOptions);