Skip to content

Commit

Permalink
Prevent task being scheduled again if .start() called while task is…
Browse files Browse the repository at this point in the history
… running (Fixes #8)
  • Loading branch information
jaclarke committed Jul 4, 2020
1 parent 2d19a9b commit 0fc3675
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 9 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [1.6.1] - 04 Jul 2020
### Fixed
- Fix bug when `task.start()` is called on a running task

## [1.6.0] - 17 Apr 2020
### Added
- `CronosExpression` now has `warnings` property that lists possible errors in the expression. Currently supports detecting cases where increment value is larger than the valid (or supplied) range for a field
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cronosjs",
"version": "1.6.0",
"version": "1.6.1",
"description": "A cron based task scheduler for node and the browser, with extended syntax and timezone support.",
"keywords": [
"cron",
Expand Down
18 changes: 11 additions & 7 deletions src/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,21 @@ export class CronosTask {
}

start() {
this._updateTimestamp()
addTask(this)
runScheduledTasks()
if (this.isRunning) this._emit('started')
if (!this.isRunning) {
this._updateTimestamp()
addTask(this)
runScheduledTasks()
if (this.isRunning) this._emit('started')
}
return this
}

stop() {
this._timestamp = undefined
removeTask(this)
this._emit('stopped')
if (this.isRunning) {
this._timestamp = undefined
removeTask(this)
this._emit('stopped')
}
return this
}

Expand Down
54 changes: 54 additions & 0 deletions tests/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,60 @@ describe('Scheduling tests', () => {
expect(task.isRunning).toEqual(false)
})

test('Calling .start() while task running', () => {
const runCallback = jest.fn()
const startedCallback = jest.fn()
const stoppedCallback = jest.fn()

const fromDate = '2020-07-04T12:00:00Z'
mockDate(fromDate)

const task = new CronosTask(
CronosExpression.parse('0/5 * * * * *', {
timezone: 0
})
)

task
.on('started', startedCallback)
.on('run', runCallback)
.on('stopped', stoppedCallback)
.start()

for (let i = 1; i <= 6; i++) {
mockDate(task.nextRun)
jest.runOnlyPendingTimers()

expect(runCallback).toHaveBeenLastCalledWith(
1593864000000 + (i*5000)
)
}

expect(runCallback).toHaveBeenCalledTimes(6)

// second start call
task.start()

for (let i = 1; i <= 6; i++) {
mockDate(task.nextRun)
jest.runOnlyPendingTimers()

expect(runCallback).toHaveBeenLastCalledWith(
1593864030000 + (i*5000)
)
}

expect(runCallback).toHaveBeenCalledTimes(12)

expect(startedCallback).toHaveBeenCalledTimes(1)
expect(stoppedCallback).toHaveBeenCalledTimes(0)

task.stop()
task.stop()

expect(stoppedCallback).toHaveBeenCalledTimes(1)
})

test('CronosTask with array of dates', () => {
const startedCallback = jest.fn()
const runCallback = jest.fn()
Expand Down

0 comments on commit 0fc3675

Please sign in to comment.