-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement support for Cron expressions (#161)
- Loading branch information
Showing
8 changed files
with
421 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { Job, JobStatus } from '../../common/Job' | ||
import { Task } from '../../common/Task' | ||
import { AsyncTask } from '../../common/AsyncTask' | ||
import { JobOptions } from '../simple-interval/SimpleIntervalJob' | ||
|
||
export const CRON_EVERY_SECOND = '* * * * * *' | ||
|
||
export const CRON_EVERY_30_SECONDS = '*/30 * * * * *' | ||
|
||
export const CRON_EVERY_MINUTE = '* * * * *' | ||
|
||
export const CRON_EVERY_30_MINUTES = '*/30 * * * *' | ||
|
||
export const CRON_EVERY_HOUR = '0 * * * *' | ||
|
||
export type CronSchedule = { | ||
cronExpression: string | ||
timezone?: string | ||
} | ||
|
||
export type Cron = { | ||
running(): boolean | ||
stop(): void | ||
} | ||
|
||
export class CronJob extends Job { | ||
private readonly schedule: CronSchedule | ||
private readonly task: Task | AsyncTask | ||
private readonly preventOverrun: boolean | ||
|
||
private cronInstance: Cron | undefined | ||
|
||
constructor(schedule: CronSchedule, task: Task | AsyncTask, options: JobOptions = {}) { | ||
super(options.id) | ||
this.preventOverrun = options.preventOverrun ?? true | ||
this.schedule = schedule | ||
this.task = task | ||
} | ||
|
||
getStatus(): JobStatus { | ||
return this.cronInstance?.running() ? JobStatus.RUNNING : JobStatus.STOPPED | ||
} | ||
|
||
start(): void { | ||
// lazy-require croner to avoid mandatory dependency | ||
const croner = require('croner') | ||
if (!croner) { | ||
throw new Error( | ||
'Please install "croner" (run "npm i croner") in case you want to use Cron jobs.' | ||
) | ||
} | ||
|
||
this.cronInstance = croner.Cron( | ||
this.schedule.cronExpression, | ||
{ | ||
timezone: this.schedule.timezone, | ||
}, | ||
() => { | ||
if (!this.task.isExecuting || !this.preventOverrun) { | ||
this.task.execute() | ||
} | ||
} | ||
) | ||
} | ||
|
||
stop(): void { | ||
this.cronInstance?.stop() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { SchedulerEngine } from '../../common/SchedulerEngine' | ||
import { CronJob } from './CronJob' | ||
|
||
export class CronJobEngine extends SchedulerEngine<CronJob> { | ||
private readonly jobs: CronJob[] | ||
|
||
constructor() { | ||
super() | ||
this.jobs = [] | ||
} | ||
|
||
add(job: CronJob): void { | ||
this.jobs.push(job) | ||
job.start() | ||
} | ||
|
||
stop(): void { | ||
for (const job of this.jobs) { | ||
job.stop() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.