Skip to content

Commit

Permalink
Adjust croner integration to 6.x. Add version in README.md.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Mar 5, 2023
1 parent 4e35f8a commit 56edbd5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ You can use CronJob instances for handling Cron-style scheduling:
scheduler.addCronJob(job)
```

Note that you need to install "croner" library for this to work. Run `npm i croner` in order to install this dependency.
Note that you need to install "croner" library for this to work. Run `npm i croner@6` in order to install this dependency.

## Usage in clustered environments

Expand Down
11 changes: 4 additions & 7 deletions lib/engines/cron/CronJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type CronSchedule = {
}

export type Cron = {
running(): boolean
isRunning(): boolean
stop(): void
}

Expand All @@ -39,7 +39,7 @@ export class CronJob extends Job {

/* istanbul ignore next */
getStatus(): JobStatus {
return this.cronInstance?.running() ? JobStatus.RUNNING : JobStatus.STOPPED
return this.cronInstance?.isRunning() ? JobStatus.RUNNING : JobStatus.STOPPED
}

start(): void {
Expand All @@ -56,12 +56,9 @@ export class CronJob extends Job {
this.schedule.cronExpression,
{
timezone: this.schedule.timezone,
protect: this.preventOverrun
},
() => {
if (!this.task.isExecuting || !this.preventOverrun) {
this.task.execute()
}
}
async () => await this.task.execute()
)
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@types/node": "^18.11.17",
"@typescript-eslint/eslint-plugin": "^5.47.0",
"@typescript-eslint/parser": "^5.47.0",
"croner": "^5.3.5",
"croner": "^6.0.2",
"eslint": "^8.30.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
Expand Down
33 changes: 21 additions & 12 deletions test/CronJob.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('ToadScheduler', () => {
expect(job.getStatus()).toBe('stopped')
})

it('correctly processes CronJob', () => {
it('correctly processes CronJob', async () => {
let counter = 0
const scheduler = new ToadScheduler()
const task = new Task('simple task', () => {
Expand All @@ -63,12 +63,16 @@ describe('ToadScheduler', () => {
resetTime()
expect(counter).toBe(0)
advanceTimersByTime(19999)
await Promise.resolve(undefined)
expect(counter).toBe(0)
advanceTimersByTime(1)
await Promise.resolve(undefined)
expect(counter).toBe(1)
advanceTimersByTime(19999)
await Promise.resolve(undefined)
expect(counter).toBe(1)
advanceTimersByTime(1)
await Promise.resolve(undefined)
expect(counter).toBe(2)

scheduler.stop()
Expand Down Expand Up @@ -126,7 +130,7 @@ describe('ToadScheduler', () => {
it('allows enabling CronJob execution overrun', async () => {
let counter = 0
const scheduler = new ToadScheduler()
const task = new AsyncTask('simple task', () => {
const task = new AsyncTask('simple task', async () => {
counter++
advanceTimersByTime(5000)
return Promise.resolve(undefined)
Expand All @@ -152,13 +156,13 @@ describe('ToadScheduler', () => {
await Promise.resolve() // this allows promises to play nice with mocked timers
await Promise.resolve()
await Promise.resolve()
expect(counter).toBe(4)
expect(counter).toBe(3)
advanceTimersByTime(1000)
advanceTimersByTime(1000)
advanceTimersByTime(1000)
advanceTimersByTime(1000)
advanceTimersByTime(1000)
expect(counter).toBe(9)
expect(counter).toBe(8)
await Promise.resolve()
await Promise.resolve()
await Promise.resolve()
Expand All @@ -168,14 +172,14 @@ describe('ToadScheduler', () => {
advanceTimersByTime(1000)
advanceTimersByTime(1000)
await Promise.resolve()
expect(counter).toBe(14)
expect(counter).toBe(13)
scheduler.stop()
})

it('allows preventing CronJob execution overrun with async tasks', async () => {
let counter = 0
const scheduler = new ToadScheduler()
const task = new AsyncTask('simple task', () => {
const task = new AsyncTask('simple task', async () => {
counter++
advanceTimersByTime(5000)
return Promise.resolve(undefined)
Expand All @@ -199,10 +203,11 @@ describe('ToadScheduler', () => {
advanceTimersByTime(1)
await Promise.resolve()
expect(counter).toBe(1)
advanceTimersByTime(2000)
await Promise.resolve()
advanceTimersByTime(999)
expect(counter).toBe(1)
advanceTimersByTime(1)
advanceTimersByTime(6000)
await Promise.resolve()
expect(counter).toBe(2)
scheduler.stop()
})
Expand All @@ -214,7 +219,7 @@ describe('ToadScheduler', () => {
let result3 = 0

const scheduler = new ToadScheduler()
const task = new AsyncTask('simple task', () => {
const task = new AsyncTask('simple task', async () => {
counter++
advanceTimersByTime(5000)
const promise1 = Promise.resolve().then(() => {
Expand Down Expand Up @@ -255,11 +260,11 @@ describe('ToadScheduler', () => {
expect(result3).toBe(1)
expect(counter).toBe(1)
await Promise.resolve()
advanceTimersByTime(999)
advanceTimersByTime(1999)
expect(counter).toBe(1)
await Promise.resolve()
await Promise.resolve()
advanceTimersByTime(1)
advanceTimersByTime(2000)
await Promise.resolve()
expect(counter).toBe(2)
scheduler.stop()
Expand All @@ -268,7 +273,7 @@ describe('ToadScheduler', () => {
expect(result3).toBe(2)
})

it('correctly handles adding job twice', () => {
it('correctly handles adding job twice', async () => {
let counter = 0
const scheduler = new ToadScheduler()
const task = new Task('simple task', () => {
Expand All @@ -286,12 +291,16 @@ describe('ToadScheduler', () => {
resetTime()
expect(counter).toBe(0)
advanceTimersByTime(19999)
await Promise.resolve()
expect(counter).toBe(0)
advanceTimersByTime(1)
await Promise.resolve()
expect(counter).toBe(2)
advanceTimersByTime(19999)
await Promise.resolve()
expect(counter).toBe(2)
advanceTimersByTime(1)
await Promise.resolve()
expect(counter).toBe(4)

scheduler.stop()
Expand Down

0 comments on commit 56edbd5

Please sign in to comment.