Skip to content

Commit

Permalink
fix: fixed cronExpressionRepeatingEveryNMinutes
Browse files Browse the repository at this point in the history
- fixed issue where cronExpressionRepeatingEveryNMinutes generated an expression that did not handle values larger than 60 properly
  • Loading branch information
dereekb committed Oct 9, 2022
1 parent 3763fdf commit 63a7f8c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion apps/demo-api/src/app/function/model/schedule.functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { onScheduleWithDemoNestContext } from '../function';
// MARK: Example
export const demoExampleUsageOfSchedule = onScheduleWithDemoNestContext(
{
cron: '* */1 * * *' // Once every hour on the hour
cron: 60 // Once every hour on the hour
},
exampleUsageOfSchedule
);
31 changes: 28 additions & 3 deletions packages/util/src/lib/value/cron.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
import { Minutes } from '../date/date';
import { Hours, Minutes } from '../date/date';
import { Maybe } from './maybe.type';

/**
* A cron schedule expression string
*/
export type CronExpression = string;

export function cronExpressionRepeatingEveryNMinutes(minutes: Minutes): CronExpression {
return `*/${minutes} * * * *`; // every nth minute
/**
* Creates a CronExpression for the input number of minutes.
*
* Note that if the number of minutes is greater than 60, it will generate an expression that
* isn't exactly n number of minutes apart from the previous execution, but instead create
* an expression that is n/60 hours apart and take place on the n%60th minute.
*
* @param inputMinutes
* @returns
*/
export function cronExpressionRepeatingEveryNMinutes(inputMinutes: Minutes): CronExpression {
let minutes: Minutes;
let hours: Maybe<Hours>;

let expression: CronExpression;

if (inputMinutes >= 60) {
hours = Math.floor(inputMinutes / 60);
minutes = inputMinutes % 60;

expression = `${minutes} */${hours} * * *`; // every nth hour at the given minute
} else {
expression = `*/${inputMinutes} * * * *`; // every nth minute
}

return expression;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { onScheduleWithAPP_CODE_PREFIXNestContext } from '../function';
// MARK: Example
export const APP_CODE_PREFIX_LOWERExampleUsageOfSchedule = onScheduleWithAPP_CODE_PREFIXNestContext(
{
cron: '* */1 * * *' // Once every hour on the hour
cron: 60 // Once every hour on the hour
},
exampleUsageOfSchedule
);

0 comments on commit 63a7f8c

Please sign in to comment.