Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cron overrun #213

Merged
merged 3 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
{
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
"plugin:@typescript-eslint/recommended"
],
"plugins": [
"@typescript-eslint",
"prettier"
"@typescript-eslint"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
Expand All @@ -18,12 +16,6 @@
"node": true
},
"rules": {
"prettier/prettier": [
"error",
{
"endOfLine": "auto"
}
],
"@typescript-eslint/ban-ts-ignore": "off",
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/no-non-null-assertion": "off",
Expand Down
2 changes: 1 addition & 1 deletion lib/common/AsyncTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class AsyncTask {
constructor(
id: string,
handler: (taskId?: string, jobId?: string) => Promise<unknown>,
errorHandler?: (err: Error) => void
errorHandler?: (err: Error) => void,
) {
this.id = id
this.handler = handler
Expand Down
2 changes: 1 addition & 1 deletion lib/common/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class Task {
constructor(
id: string,
handler: (taskId?: string, jobId?: string) => void,
errorHandler?: (err: Error) => void
errorHandler?: (err: Error) => void,
) {
this.id = id
this.handler = handler
Expand Down
24 changes: 5 additions & 19 deletions lib/engines/cron/CronJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Job, JobStatus } from '../../common/Job'
import { Task } from '../../common/Task'
import { AsyncTask } from '../../common/AsyncTask'
import { JobOptions } from '../simple-interval/SimpleIntervalJob'
import { Cron } from 'croner'

export const CRON_EVERY_SECOND = '* * * * * *'

Expand All @@ -18,11 +19,6 @@ export type CronSchedule = {
timezone?: string
}

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

export class CronJob extends Job {
private readonly schedule: CronSchedule
private readonly task: Task | AsyncTask
Expand All @@ -32,7 +28,7 @@ export class CronJob extends Job {

constructor(schedule: CronSchedule, task: Task | AsyncTask, options: JobOptions = {}) {
super(options.id)
this.preventOverrun = false
this.preventOverrun = options.preventOverrun || false
this.schedule = schedule
this.task = task
}
Expand All @@ -43,27 +39,17 @@ export class CronJob extends Job {
}

start(): void {
// lazy-require croner to avoid mandatory dependency
let croner
try {
croner = require('croner')
} catch (err) {
/* istanbul ignore next */
throw new Error(
'Please install "croner" (run "npm i croner") in case you want to use Cron jobs.'
)
}

this.cronInstance = croner.Cron(
this.cronInstance = Cron(
this.schedule.cronExpression,
{
timezone: this.schedule.timezone,
protect: false,
},
() => {
if (!this.task.isExecuting || !this.preventOverrun) {
this.task.execute(this.id)
}
}
},
)
}

Expand Down
6 changes: 3 additions & 3 deletions lib/engines/simple-interval/LongIntervalJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class LongIntervalJob extends Job {
{
milliseconds: Math.min(MAX_TIMEOUT_DURATION_MS - 1, remainingMs),
},
timeEater
timeEater,
)
this.childJob.start()
} else {
Expand All @@ -55,7 +55,7 @@ export class LongIntervalJob extends Job {
new Task('Final mile task', () => {
this.setTimeEatingJob(toMsecs(this.schedule))
return this.task.execute(this.id)
})
}),
)
this.childJob.start()
}
Expand All @@ -66,7 +66,7 @@ export class LongIntervalJob extends Job {
{
milliseconds: Math.min(MAX_TIMEOUT_DURATION_MS - 1, startingRemainingMs),
},
timeEater
timeEater,
)
this.childJob.start()
}
Expand Down
2 changes: 1 addition & 1 deletion lib/engines/simple-interval/SimpleIntervalJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class SimpleIntervalJob extends Job {
// See https://github.com/kibertoad/toad-scheduler/issues/24
if (time >= 2147483647) {
throw new Error(
'Due to setInterval limitations, no intervals longer than 24.85 days can be scheduled correctly. Please create LongIntervalJob instead.'
'Due to setInterval limitations, no intervals longer than 24.85 days can be scheduled correctly. Please create LongIntervalJob instead.',
)
}

Expand Down
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@
"format": "prettier --write \"{lib,test}/**/*.{js,ts}\" index.ts",
"prepublishOnly": "npm run build"
},
"dependencies": {
"croner": "^7.0.5"
},
"devDependencies": {
"@types/jest": "^29.5.2",
"@types/jest": "^29.5.12",
"@types/node": "^20.3.1",
"@typescript-eslint/eslint-plugin": "^5.59.11",
"@typescript-eslint/parser": "^5.59.11",
"croner": "^7.0.1",
"eslint": "^8.42.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"jasmine-core": "^5.0.1",
"jest": "^29.5.0",
"@typescript-eslint/eslint-plugin": "^6.20.0",
"@typescript-eslint/parser": "^6.20.0",
"eslint": "^8.56.0",
"jasmine-core": "^5.1.1",
"jest": "^29.7.0",
"karma": "^6.4.2",
"karma-chrome-launcher": "^3.2.0",
"karma-jasmine": "^5.1.0",
"karma-typescript": "^5.5.4",
"prettier": "^3.0.0",
"ts-jest": "^29.1.0",
"typescript": "^5.1.3"
"ts-jest": "^29.1.2",
"typescript": "5.2.2"
},
"homepage": "https://github.com/kibertoad/toad-scheduler",
"repository": {
Expand Down
24 changes: 12 additions & 12 deletions test/AsyncTask.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ describe('ToadScheduler', () => {
},
(err: Error) => {
error = err.message
}
},
)
const job = new SimpleIntervalJob(
{
seconds: 1,
runImmediately: true,
},
task
task,
)

scheduler.addSimpleIntervalJob(job)
Expand Down Expand Up @@ -68,14 +68,14 @@ describe('ToadScheduler', () => {
},
(err: Error) => {
error = err.message
}
},
)
const job = new SimpleIntervalJob(
{
seconds: 1,
runImmediately: true,
},
task
task,
)

scheduler.addSimpleIntervalJob(job)
Expand Down Expand Up @@ -107,14 +107,14 @@ describe('ToadScheduler', () => {
error = err.message
throw new Error('error while handling error')
})
}
},
)
const job = new SimpleIntervalJob(
{
seconds: 1,
runImmediately: true,
},
task
task,
)

scheduler.addSimpleIntervalJob(job)
Expand All @@ -139,13 +139,13 @@ describe('ToadScheduler', () => {
},
(err: Error) => {
error = err.message
}
},
)
const job = new SimpleIntervalJob(
{
milliseconds: 5,
},
task
task,
)

scheduler.addSimpleIntervalJob(job)
Expand All @@ -169,13 +169,13 @@ describe('ToadScheduler', () => {
return Promise.reject(new Error('kaboom2'))
})
},
() => {}
() => {},
)
const job = new SimpleIntervalJob(
{
milliseconds: 5,
},
task
task,
)

scheduler.addSimpleIntervalJob(job)
Expand All @@ -198,7 +198,7 @@ describe('ToadScheduler', () => {
Promise.resolve()
return Promise.reject(new Error('kaboom2'))
},
() => {}
() => {},
)
const job = new SimpleIntervalJob(
{
Expand All @@ -207,7 +207,7 @@ describe('ToadScheduler', () => {
task,
{
id: 'jobId',
}
},
)

scheduler.addSimpleIntervalJob(job)
Expand Down
Loading
Loading