toad-scheduler
Advanced tools
Comparing version 1.6.1 to 2.0.0
export declare class AsyncTask { | ||
isExecuting: boolean; | ||
private readonly id; | ||
@@ -3,0 +4,0 @@ private readonly handler; |
@@ -11,5 +11,8 @@ "use strict"; | ||
this.errorHandler = errorHandler || (0, Logger_1.defaultErrorHandler)(this.id); | ||
this.isExecuting = false; | ||
} | ||
execute() { | ||
this.handler().catch((err) => { | ||
this.isExecuting = true; | ||
this.handler() | ||
.catch((err) => { | ||
const errorHandleResult = this.errorHandler(err); | ||
@@ -20,2 +23,5 @@ if ((0, Utils_1.isPromise)(errorHandleResult)) { | ||
} | ||
}) | ||
.finally(() => { | ||
this.isExecuting = false; | ||
}); | ||
@@ -22,0 +28,0 @@ } |
export declare class Task { | ||
isExecuting: boolean; | ||
private readonly id; | ||
@@ -3,0 +4,0 @@ private readonly handler; |
@@ -11,4 +11,6 @@ "use strict"; | ||
this.errorHandler = errorHandler || (0, Logger_1.defaultErrorHandler)(this.id); | ||
this.isExecuting = false; | ||
} | ||
execute() { | ||
this.isExecuting = true; | ||
try { | ||
@@ -24,2 +26,3 @@ this.handler(); | ||
} | ||
this.isExecuting = false; | ||
} | ||
@@ -26,0 +29,0 @@ } |
import { AsyncTask } from '../../common/AsyncTask'; | ||
import { Job, JobStatus } from '../../common/Job'; | ||
import { Task } from '../../common/Task'; | ||
import { JobOptions } from './SimpleIntervalJob'; | ||
import { SimpleIntervalSchedule } from './SimpleIntervalSchedule'; | ||
@@ -10,3 +11,4 @@ export declare class LongIntervalJob extends Job { | ||
private readonly task; | ||
constructor(schedule: SimpleIntervalSchedule, task: Task | AsyncTask, id?: string); | ||
private readonly preventOverrun; | ||
constructor(schedule: SimpleIntervalSchedule, task: Task | AsyncTask, options?: JobOptions); | ||
private setTimeEatingJob; | ||
@@ -13,0 +15,0 @@ start(): void; |
@@ -10,4 +10,6 @@ "use strict"; | ||
class LongIntervalJob extends Job_1.Job { | ||
constructor(schedule, task, id) { | ||
super(id); | ||
constructor(schedule, task, options = {}) { | ||
var _a; | ||
super(options.id); | ||
this.preventOverrun = (_a = options.preventOverrun) !== null && _a !== void 0 ? _a : true; | ||
this.schedule = schedule; | ||
@@ -70,3 +72,5 @@ this.task = task; | ||
this.timer = setInterval(() => { | ||
this.task.execute(); | ||
if (!this.task.isExecuting || !this.preventOverrun) { | ||
this.task.execute(); | ||
} | ||
}, time); | ||
@@ -73,0 +77,0 @@ } |
@@ -5,2 +5,6 @@ import { AsyncTask } from '../../common/AsyncTask'; | ||
import { SimpleIntervalSchedule } from './SimpleIntervalSchedule'; | ||
export declare type JobOptions = { | ||
preventOverrun?: boolean; | ||
id?: string; | ||
}; | ||
export declare class SimpleIntervalJob extends Job { | ||
@@ -10,3 +14,4 @@ private timer?; | ||
private readonly task; | ||
constructor(schedule: SimpleIntervalSchedule, task: Task | AsyncTask, id?: string); | ||
private readonly preventOverrun; | ||
constructor(schedule: SimpleIntervalSchedule, task: Task | AsyncTask, options?: JobOptions); | ||
start(): void; | ||
@@ -13,0 +18,0 @@ stop(): void; |
@@ -7,4 +7,6 @@ "use strict"; | ||
class SimpleIntervalJob extends Job_1.Job { | ||
constructor(schedule, task, id) { | ||
super(id); | ||
constructor(schedule, task, options = {}) { | ||
var _a; | ||
super(options.id); | ||
this.preventOverrun = (_a = options.preventOverrun) !== null && _a !== void 0 ? _a : true; | ||
this.schedule = schedule; | ||
@@ -27,3 +29,5 @@ this.task = task; | ||
this.timer = setInterval(() => { | ||
this.task.execute(); | ||
if (!this.task.isExecuting || !this.preventOverrun) { | ||
this.task.execute(); | ||
} | ||
}, time); | ||
@@ -30,0 +34,0 @@ } |
{ | ||
"name": "toad-scheduler", | ||
"version": "1.6.1", | ||
"version": "2.0.0", | ||
"license": "MIT", | ||
"description": "In-memory Node.js job scheduler", | ||
"description": "In-memory Node.js and browser job scheduler", | ||
"maintainers": [ | ||
@@ -24,18 +24,18 @@ { | ||
"devDependencies": { | ||
"@types/jest": "^27.5.1", | ||
"@types/node": "^17.0.33", | ||
"@typescript-eslint/eslint-plugin": "^5.24.0", | ||
"@typescript-eslint/parser": "^5.24.0", | ||
"eslint": "^8.15.0", | ||
"@types/jest": "^28.1.8", | ||
"@types/node": "^18.8.4", | ||
"@typescript-eslint/eslint-plugin": "^5.40.0", | ||
"@typescript-eslint/parser": "^5.40.0", | ||
"eslint": "^8.25.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"jasmine-core": "^4.1.1", | ||
"jest": "^27.5.1", | ||
"karma": "^6.3.20", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"jasmine-core": "^4.4.0", | ||
"jest": "^28.1.3", | ||
"karma": "^6.4.1", | ||
"karma-chrome-launcher": "^3.1.1", | ||
"karma-jasmine": "^5.0.1", | ||
"karma-jasmine": "^5.1.0", | ||
"karma-typescript": "^5.5.3", | ||
"prettier": "^2.6.2", | ||
"ts-jest": "^27.1.4", | ||
"typescript": "4.6.4" | ||
"prettier": "^2.7.1", | ||
"ts-jest": "^28.0.8", | ||
"typescript": "4.8.4" | ||
}, | ||
@@ -51,2 +51,3 @@ "homepage": "https://github.com/kibertoad/toad-scheduler", | ||
"in-memory", | ||
"browser", | ||
"job", | ||
@@ -53,0 +54,0 @@ "task" |
@@ -8,4 +8,6 @@ # toad-scheduler | ||
In-memory Node.js job scheduler that repeatedly executes given tasks within specified intervals of time (e. g. "each 20 seconds"). | ||
In-memory TypeScript job scheduler that repeatedly executes given tasks within specified intervals of time (e. g. "each 20 seconds"). | ||
Node.js 12+ and modern browsers are supported | ||
## Getting started | ||
@@ -65,2 +67,28 @@ | ||
## Preventing task run overruns | ||
In case you want to prevent second instance of a task from being fired up while first one is still executing, you can use `preventOverrun` options: | ||
```js | ||
import { ToadScheduler, SimpleIntervalJob, Task } from 'toad-scheduler'; | ||
const scheduler = new ToadScheduler(); | ||
const task = new Task('simple task', () => { | ||
// if this task runs long, second one won't be started until this one concludes | ||
console.log('Task triggered'); | ||
}); | ||
const job = new SimpleIntervalJob( | ||
{ seconds: 20, runImmediately: true }, | ||
task, | ||
{ | ||
id: 'id_1', | ||
preventOverrun: true, | ||
} | ||
); | ||
//create and start jobs | ||
scheduler.addSimpleIntervalJob(job); | ||
``` | ||
## Using IDs and ES6-style imports | ||
@@ -82,3 +110,3 @@ | ||
task, | ||
'id_1' | ||
{ id: 'id_1' } | ||
); | ||
@@ -89,3 +117,3 @@ | ||
task, | ||
'id_2' | ||
{ id: 'id_2' } | ||
); | ||
@@ -92,0 +120,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
37149
466
164