toad-scheduler
Advanced tools
Comparing version 1.5.0 to 1.6.0
@@ -7,2 +7,3 @@ export { ToadScheduler } from './lib/toadScheduler'; | ||
export { SimpleIntervalJob } from './lib/engines/simple-interval/SimpleIntervalJob'; | ||
export { LongIntervalJob } from './lib/engines/simple-interval/LongIntervalJob'; | ||
export type { SimpleIntervalSchedule } from './lib/engines/simple-interval/SimpleIntervalSchedule'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.SimpleIntervalJob = exports.Job = exports.Task = exports.AsyncTask = exports.ToadScheduler = void 0; | ||
exports.LongIntervalJob = exports.SimpleIntervalJob = exports.Job = exports.Task = exports.AsyncTask = exports.ToadScheduler = void 0; | ||
var toadScheduler_1 = require("./lib/toadScheduler"); | ||
@@ -14,1 +14,3 @@ Object.defineProperty(exports, "ToadScheduler", { enumerable: true, get: function () { return toadScheduler_1.ToadScheduler; } }); | ||
Object.defineProperty(exports, "SimpleIntervalJob", { enumerable: true, get: function () { return SimpleIntervalJob_1.SimpleIntervalJob; } }); | ||
var LongIntervalJob_1 = require("./lib/engines/simple-interval/LongIntervalJob"); | ||
Object.defineProperty(exports, "LongIntervalJob", { enumerable: true, get: function () { return LongIntervalJob_1.LongIntervalJob; } }); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.AsyncTask = void 0; | ||
function defaultErrorHandler(id) { | ||
return (err) => { | ||
console.error(`Error while handling task ${id}: ${err.message}`); | ||
}; | ||
} | ||
const Logger_1 = require("./Logger"); | ||
const Utils_1 = require("./Utils"); | ||
class AsyncTask { | ||
@@ -13,8 +10,14 @@ constructor(id, handler, errorHandler) { | ||
this.handler = handler; | ||
this.errorHandler = errorHandler || defaultErrorHandler(this.id); | ||
this.errorHandler = errorHandler || (0, Logger_1.defaultErrorHandler)(this.id); | ||
} | ||
execute() { | ||
this.handler().catch(this.errorHandler); | ||
this.handler().catch((err) => { | ||
const errorHandleResult = this.errorHandler(err); | ||
if ((0, Utils_1.isPromise)(errorHandleResult)) { | ||
// If we fail while handling an error, oh well | ||
errorHandleResult.catch((0, Logger_1.loggingErrorHandler)(err)); | ||
} | ||
}); | ||
} | ||
} | ||
exports.AsyncTask = AsyncTask; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Task = void 0; | ||
function defaultErrorHandler(id) { | ||
return (err) => { | ||
console.error(`Error while handling task ${id}: ${err.message}`); | ||
}; | ||
} | ||
const Logger_1 = require("./Logger"); | ||
const Utils_1 = require("./Utils"); | ||
class Task { | ||
@@ -13,3 +10,3 @@ constructor(id, handler, errorHandler) { | ||
this.handler = handler; | ||
this.errorHandler = errorHandler || defaultErrorHandler(this.id); | ||
this.errorHandler = errorHandler || (0, Logger_1.defaultErrorHandler)(this.id); | ||
} | ||
@@ -21,3 +18,7 @@ execute() { | ||
catch (err) { | ||
this.errorHandler(err); | ||
const errorHandleResult = this.errorHandler(err); | ||
if ((0, Utils_1.isPromise)(errorHandleResult)) { | ||
// If we fail while handling an error, oh well | ||
errorHandleResult.catch((0, Logger_1.loggingErrorHandler)(err)); | ||
} | ||
} | ||
@@ -24,0 +25,0 @@ } |
import { SchedulerEngine } from '../../common/SchedulerEngine'; | ||
import { SimpleIntervalJob } from './SimpleIntervalJob'; | ||
export declare class SimpleIntervalEngine extends SchedulerEngine<SimpleIntervalJob> { | ||
import { LongIntervalJob } from './LongIntervalJob'; | ||
import { Job } from '../../common/Job'; | ||
export declare class SimpleIntervalEngine extends SchedulerEngine<SimpleIntervalJob | LongIntervalJob> { | ||
private readonly jobs; | ||
constructor(); | ||
add(job: SimpleIntervalJob): void; | ||
add(job: Job): void; | ||
stop(): void; | ||
} |
@@ -13,6 +13,6 @@ "use strict"; | ||
start() { | ||
const time = SimpleIntervalSchedule_1.toMsecs(this.schedule); | ||
const time = (0, SimpleIntervalSchedule_1.toMsecs)(this.schedule); | ||
// 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. toad-scheduler will eventually include a workaround for this, but for now your schedule is likely to break.'); | ||
throw new Error('Due to setInterval limitations, no intervals longer than 24.85 days can be scheduled correctly. Please create LongIntervalJob instead.'); | ||
} | ||
@@ -19,0 +19,0 @@ // Avoid starting duplicates and leaking previous timers |
import { SimpleIntervalJob } from './engines/simple-interval/SimpleIntervalJob'; | ||
import { Job } from './common/Job'; | ||
import { LongIntervalJob } from './engines/simple-interval/LongIntervalJob'; | ||
export declare class ToadScheduler { | ||
@@ -7,2 +8,4 @@ private readonly engines; | ||
constructor(); | ||
addIntervalJob(job: SimpleIntervalJob | LongIntervalJob): void; | ||
addLongIntervalJob(job: LongIntervalJob): void; | ||
addSimpleIntervalJob(job: SimpleIntervalJob): void; | ||
@@ -9,0 +12,0 @@ stop(): void; |
@@ -10,3 +10,3 @@ "use strict"; | ||
} | ||
addSimpleIntervalJob(job) { | ||
addIntervalJob(job) { | ||
if (!this.engines.simpleIntervalEngine) { | ||
@@ -23,2 +23,8 @@ this.engines.simpleIntervalEngine = new SimpleIntervalEngine_1.SimpleIntervalEngine(); | ||
} | ||
addLongIntervalJob(job) { | ||
return this.addIntervalJob(job); | ||
} | ||
addSimpleIntervalJob(job) { | ||
return this.addIntervalJob(job); | ||
} | ||
stop() { | ||
@@ -25,0 +31,0 @@ for (const engine of Object.values(this.engines)) { |
{ | ||
"name": "toad-scheduler", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"license": "MIT", | ||
@@ -18,3 +18,3 @@ "description": "In-memory Node.js job scheduler", | ||
"test:coverage": "jest --config=jest.config.json --coverage", | ||
"lint": "eslint --format codeframe \"lib/**/*.ts\" \"test/**/*.ts\"", | ||
"lint": "eslint \"lib/**/*.ts\" \"test/**/*.ts\"", | ||
"prettier": "prettier --write \"{lib,test}/**/*.{js,ts}\" index.ts", | ||
@@ -24,13 +24,13 @@ "prepublishOnly": "npm run build" | ||
"devDependencies": { | ||
"@types/jest": "^26.0.24", | ||
"@types/node": "^16.3.2", | ||
"@typescript-eslint/eslint-plugin": "^4.28.3", | ||
"@typescript-eslint/parser": "^4.28.3", | ||
"eslint": "^7.30.0", | ||
"@types/jest": "^27.0.2", | ||
"@types/node": "^10.17.60", | ||
"@typescript-eslint/eslint-plugin": "^5.2.0", | ||
"@typescript-eslint/parser": "^5.2.0", | ||
"eslint": "^8.1.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-prettier": "^3.4.0", | ||
"jest": "^27.0.6", | ||
"prettier": "^2.3.2", | ||
"ts-jest": "^27.0.3", | ||
"typescript": "4.3.5" | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"jest": "^27.3.1", | ||
"prettier": "^2.4.1", | ||
"ts-jest": "^27.0.7", | ||
"typescript": "4.4.4" | ||
}, | ||
@@ -37,0 +37,0 @@ "homepage": "https://github.com/kibertoad/toad-scheduler", |
@@ -34,2 +34,3 @@ # toad-scheduler | ||
## Usage with async tasks | ||
@@ -59,2 +60,49 @@ | ||
## Asynchronous error handling | ||
Note that your error handlers can be asynchronous and return a promise. In such case an additional catch block will be attached to them, and should | ||
there be an error while trying to resolve that promise, and logging error will be logged using the default error handler (`console.error`). | ||
## Using IDs and ES6-style imports | ||
You can attach IDs to tasks to identify them later. This is helpful in projects that run a lot of tasks and especially if you want to target some of the tasks specifically (e. g. in order to stop or restart them, or to check their status). | ||
```js | ||
import { ToadScheduler, SimpleIntervalJob, Task } from 'toad-scheduler'; | ||
const scheduler = new ToadScheduler(); | ||
const task = new Task('simple task', () => { | ||
console.log('Task triggered'); | ||
}); | ||
const job1 = new SimpleIntervalJob( | ||
{ seconds: 20, runImmediately: true }, | ||
task, | ||
'id_1' | ||
); | ||
const job2 = new SimpleIntervalJob( | ||
{ seconds: 15, runImmediately: true }, | ||
task, | ||
'id_2' | ||
); | ||
//create and start jobs | ||
scheduler.addSimpleIntervalJob(job1); | ||
scheduler.addSimpleIntervalJob(job2); | ||
// stop job with ID: id_2 | ||
scheduler.stopById('id_2'); | ||
// remove job with ID: id_1 | ||
scheduler.removeById('id_1'); | ||
// check status of jobs | ||
console.log(scheduler.getById('id_1').getStatus()); // returns Error (job not found) | ||
console.log(scheduler.getById('id_2').getStatus()); // returns "stopped" and can be started again | ||
``` | ||
## API for schedule | ||
@@ -78,2 +126,4 @@ | ||
* `addSimpleIntervalJob(job: SimpleIntervalJob): void` - registers and starts a new job; | ||
* `addLongIntervalJob(job: SimpleIntervalJob): void` - registers and starts a new job with support for intervals longer than 24.85 days; | ||
* `addIntervalJob(job: SimpleIntervalJob | LongIntervalJob): void` - registers and starts new interval-based job; | ||
* `stop(): void` - stops all jobs, registered in the scheduler; | ||
@@ -80,0 +130,0 @@ * `getById(id: string): Job` - returns the job with a given id. |
24260
27
438
136