adonis-scheduler
Advanced tools
Comparing version 3.0.0 to 3.0.1
{ | ||
"name": "adonis-scheduler", | ||
"version": "3.0.0", | ||
"version": "3.0.1", | ||
"description": "Easy task scheduling for AdonisJS", | ||
"scripts": { | ||
"test": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- -R spec test", | ||
"coverage": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha test", | ||
"eslint": "eslint ." | ||
"coverage": "nyc report --reporter=text-lcov | coveralls", | ||
"lint": "standard", | ||
"posttest": "npm run coverage", | ||
"pretest": "npm run lint", | ||
"test": "nyc npm run test:local", | ||
"test:local": "FORCE_COLOR=true node bin/index.js --local", | ||
"test:win": "set FORCE_COLOR=true && node bin/index.js --win" | ||
}, | ||
@@ -16,19 +20,31 @@ "author": "Nicholas Rempel <nbrempel@gmail.com>", | ||
"dependencies": { | ||
"@adonisjs/ace": "^4.0.4", | ||
"@adonisjs/fold": "^4.0.2", | ||
"@adonisjs/ignitor": "^1.0.6", | ||
"boxen": "^1.2.1", | ||
"debug": "^3.0.0", | ||
"@adonisjs/generic-exceptions": "^1.0.0", | ||
"debug": "^3.0.1", | ||
"lockfile": "^1.0.3", | ||
"lodash": "^4.17.4", | ||
"ms": "^2.0.0", | ||
"node-schedule": "^1.2.4", | ||
"pluralize": "^6.0.0" | ||
"pify": "^3.0.0", | ||
"pluralize": "^7.0.0" | ||
}, | ||
"devDependencies": { | ||
"chai": "^4.1.1", | ||
"@adonisjs/ace": "^4.0.5", | ||
"@adonisjs/fold": "^4.0.2", | ||
"@adonisjs/sink": "^1.0.13", | ||
"coveralls": "^2.13.1", | ||
"eslint": "^4.4.1", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^3.5.0", | ||
"mocha-lcov-reporter": "^1.3.0" | ||
"cz-conventional-changelog": "^2.0.0", | ||
"japa": "^1.0.4", | ||
"japa-cli": "^1.0.1", | ||
"nyc": "^11.1.0", | ||
"semver": "^5.4.1", | ||
"standard": "^10.0.3" | ||
}, | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"config": { | ||
"commitizen": { | ||
"path": "./node_modules/cz-conventional-changelog" | ||
} | ||
}, | ||
"repository": { | ||
@@ -47,2 +63,14 @@ "type": "git", | ||
}, | ||
"nyc": { | ||
"exclude": [ | ||
"bin", | ||
"test" | ||
] | ||
}, | ||
"standard": { | ||
"globals": [ | ||
"use", | ||
"make" | ||
] | ||
}, | ||
"files": [ | ||
@@ -49,0 +77,0 @@ "LICENSE", |
@@ -6,7 +6,44 @@ 'use strict' | ||
class SchedulerProvider extends ServiceProvider { | ||
register () { | ||
/** | ||
* Register the scheduler to the IoC container | ||
* with `Adonis/Addons/Scheduler` namespace. | ||
* | ||
* @method _registerScheduler | ||
* | ||
* @return {void} | ||
* | ||
* @private | ||
*/ | ||
_registerScheduler () { | ||
this.app.singleton('Adonis/Addons/Scheduler', () => make(require('../src/Scheduler'))) | ||
} | ||
/** | ||
* Register the task to the IoC container | ||
* with `Adonis/Addons/Task` namespace. | ||
* | ||
* @method _registerTask | ||
* | ||
* @return {void} | ||
* | ||
* @private | ||
*/ | ||
_registerTask () { | ||
this.app.bind('Adonis/Addons/Task', () => require('../src/Scheduler/Task')) | ||
this.app.alias('Adonis/Addons/Task', 'Task') | ||
} | ||
/** | ||
* Register bindings | ||
* | ||
* @method register | ||
* | ||
* @return {void} | ||
*/ | ||
register () { | ||
this._registerScheduler() | ||
this._registerTask() | ||
} | ||
} | ||
module.exports = SchedulerProvider |
# Adonis Scheduler Provider | ||
[![NPM Version][npm-image]][npm-url] | ||
[![Build Status][travis-image]][travis-url] | ||
[![Appveyor][appveyor-image]][appveyor-url] | ||
[![Coveralls][coveralls-image]][coveralls-url] | ||
This library provides an easy way to schedule recurring tasks for AdonisJS v4. | ||
> Use branch *adonis-v3* for AdonisJS version 3 | ||
> Use branch [adonis-v3](https://github.com/nrempel/adonis-scheduler/tree/adonis-v3) for AdonisJS version 3 | ||
@@ -42,3 +47,3 @@ ## Install | ||
Starting an instance of the kue listener is easy with the included ace command. Simply run `adonis run:scheduler`. | ||
Starting an instance of the kue listener is easy with the included ace command. Simply run `node ace run:scheduler`. | ||
@@ -49,3 +54,3 @@ The provider looks for jobs in the `app/Tasks` directory of your AdonisJS project and will automatically register a handler for any tasks that it finds. | ||
Jobs are easy to create. Run `adonis make:task Example`. They expose the following properties: | ||
Jobs are easy to create. Run `node ace make:task Example`. They expose the following properties: | ||
@@ -60,1 +65,14 @@ | Name | Required | Type | Static | Description | | ||
Special thanks to the creator(s) of [AdonisJS](http://adonisjs.com/) for creating such a great framework. | ||
[appveyor-image]: https://img.shields.io/appveyor/ci/nrempel/adonis-scheduler/master.svg?style=flat-square | ||
[appveyor-url]: https://ci.appveyor.com/project/nrempel/adonis-scheduler | ||
[npm-image]: https://img.shields.io/npm/v/adonis-scheduler.svg?style=flat-square | ||
[npm-url]: https://npmjs.org/package/adonis-scheduler | ||
[travis-image]: https://img.shields.io/travis/nrempel/adonis-scheduler/master.svg?style=flat-square | ||
[travis-url]: https://travis-ci.org/nrempel/adonis-scheduler | ||
[coveralls-image]: https://coveralls.io/repos/github/nrempel/adonis-scheduler/badge.svg?branch=master | ||
[coveralls-url]: https://coveralls.io/github/nrempel/adonis-scheduler |
@@ -6,3 +6,2 @@ 'use strict' | ||
class MakeTask extends Base { | ||
/** | ||
@@ -9,0 +8,0 @@ * The command signature |
@@ -6,3 +6,5 @@ 'use strict' | ||
class Scheduler extends Command { | ||
/** | ||
* @return {Array} | ||
*/ | ||
static get inject () { | ||
@@ -12,2 +14,5 @@ return ['Adonis/Addons/Scheduler'] | ||
/** | ||
* @param {Object} Scheduler | ||
*/ | ||
constructor (Scheduler) { | ||
@@ -18,2 +23,5 @@ super() | ||
/** | ||
* @return {String} | ||
*/ | ||
static get signature () { | ||
@@ -23,2 +31,5 @@ return 'run:scheduler' | ||
/** | ||
* @return {String} | ||
*/ | ||
static get description () { | ||
@@ -28,2 +39,5 @@ return 'Start the scheduler.' | ||
/** | ||
* @return {void} | ||
*/ | ||
async handle () { | ||
@@ -30,0 +44,0 @@ this.scheduler.run() |
@@ -7,2 +7,4 @@ 'use strict' | ||
const { ioc } = require('@adonisjs/fold') | ||
const CE = require('../Exceptions') | ||
const Task = require('./Task') | ||
@@ -14,3 +16,5 @@ /** | ||
class Scheduler { | ||
/** | ||
* @return {Array} | ||
*/ | ||
static get inject () { | ||
@@ -20,4 +24,7 @@ return ['Adonis/Src/Helpers'] | ||
/** | ||
* @param {Object} Helpers | ||
*/ | ||
constructor (Helpers) { | ||
this.helpers = Helpers | ||
this.Helpers = Helpers | ||
this.instance = require('node-schedule') | ||
@@ -36,3 +43,3 @@ this.registeredTasks = [] | ||
_configureTasksPath () { | ||
this.tasksPath = path.join(this.helpers.appRoot(), 'app', 'Tasks') | ||
this.tasksPath = path.join(this.Helpers.appRoot(), 'app', 'Tasks') | ||
this.tasksPath = path.normalize(this.tasksPath) | ||
@@ -42,55 +49,79 @@ } | ||
/** | ||
* Register scheduled tasks for every task found in app/Tasks | ||
* Load task file | ||
* | ||
* @public | ||
* @param {String} file | ||
* @private | ||
*/ | ||
run () { | ||
async _fetchTask (file) { | ||
const filePath = path.join(this.tasksPath, file) | ||
let task | ||
try { | ||
debug('Scan tasks path %s', this.tasksPath) | ||
task = require(filePath) | ||
} catch (e) { | ||
if (e instanceof ReferenceError) { | ||
debug('Unable to import task class <%s>. Is it a valid javascript class?', file) | ||
return | ||
} else { | ||
throw e | ||
} | ||
} | ||
const taskFiles = fs.readdirSync(this.tasksPath) | ||
taskFiles.forEach((file) => { | ||
try { | ||
const filePath = path.join(this.tasksPath, file) | ||
const Task = require(filePath) | ||
// Get instance of task class | ||
const taskInstance = ioc.make(Task) | ||
// Get instance of task class | ||
const taskInstance = ioc.make(task) | ||
// Every task must expose a schedule | ||
if (!Task.schedule) { | ||
throw new Error(`No schedule found for task: ${filePath}`) | ||
} | ||
// Every task must expose a schedule | ||
if (!('schedule' in task)) { | ||
throw CE.RuntimeException.undefinedTaskSchedule(file) | ||
} | ||
// Every task must expose a handle function | ||
if (!taskInstance.handle) { | ||
throw new Error(`No handler found for task: ${filePath}`) | ||
} | ||
// Every task must expose a handle function | ||
if (!('handle' in taskInstance)) { | ||
throw CE.RuntimeException.undefinedTaskHandle(file) | ||
} | ||
// Track currently registered tasks in memory | ||
this.registeredTasks.push(Task) | ||
if (!(taskInstance instanceof Task)) { | ||
throw CE.RuntimeException.undefinedInstanceTask(file) | ||
} | ||
// Register task handler | ||
this.instance.scheduleJob(Task.schedule, taskInstance.handle.bind(taskInstance)) | ||
} catch (e) { | ||
if (e instanceof ReferenceError) { | ||
debug('Unable to import task class <%s>. Is it a valid javascript class?', file) | ||
} else { | ||
throw e | ||
} | ||
} | ||
}) | ||
debug('scheduler running %d tasks', this.registeredTasks.length) | ||
// Track currently registered tasks in memory | ||
this.registeredTasks.push(taskInstance) | ||
// Before add task to schedule need check & unlock file if exist | ||
const locked = await taskInstance.locker.check() | ||
if (locked) { | ||
await taskInstance.locker.unlock() | ||
} | ||
// Register task handler | ||
this.instance.scheduleJob(task.schedule, taskInstance._run.bind(taskInstance)) | ||
} | ||
/** | ||
* Register scheduled tasks for every task found in app/Tasks | ||
* | ||
* @public | ||
*/ | ||
async run () { | ||
debug('Scan tasks path %s', this.tasksPath) | ||
let taskFiles | ||
try { | ||
taskFiles = fs.readdirSync(this.tasksPath) | ||
} catch (e) { | ||
// If the directory isn't found, log a message and exit gracefully | ||
if (e.code === 'ENOENT') { | ||
debug('The tasks directory <%s> does not exist. Exiting.', this.tasksPath) | ||
} else { | ||
// If it's some other error, bubble up exception | ||
throw e | ||
throw CE.RuntimeException.notFoundTask(this.tasksPath) | ||
} | ||
throw e | ||
} | ||
for (let taskFile of taskFiles) { | ||
await this._fetchTask(taskFile) | ||
} | ||
debug('scheduler running %d tasks', this.registeredTasks.length) | ||
} | ||
} | ||
module.exports = Scheduler |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
21456
14
672
76
0
10
+ Addedlockfile@^1.0.3
+ Addedms@^2.0.0
+ Addedpify@^3.0.0
+ Added@adonisjs/generic-exceptions@1.0.0(transitive)
+ Addedlockfile@1.0.4(transitive)
+ Addednode-exceptions@2.0.2(transitive)
+ Addedpluralize@7.0.0(transitive)
+ Addedupcast@1.0.4(transitive)
- Removed@adonisjs/ace@^4.0.4
- Removed@adonisjs/fold@^4.0.2
- Removed@adonisjs/ignitor@^1.0.6
- Removedboxen@^1.2.1
- Removed@adonisjs/ace@4.0.8(transitive)
- Removed@adonisjs/fold@4.1.0(transitive)
- Removed@adonisjs/generic-exceptions@2.0.1(transitive)
- Removed@adonisjs/ignitor@1.0.14(transitive)
- Removedacorn@7.4.1(transitive)
- Removedacorn-node@1.8.2(transitive)
- Removedacorn-walk@7.2.0(transitive)
- Removedansi-align@2.0.0(transitive)
- Removedansi-escapes@3.2.0(transitive)
- Removedansi-regex@3.0.1(transitive)
- Removedansi-styles@3.2.1(transitive)
- Removedboxen@1.3.0(transitive)
- Removedcaller@1.1.0(transitive)
- Removedcamelcase@4.1.0(transitive)
- Removedchalk@2.4.2(transitive)
- Removedchardet@0.4.2(transitive)
- Removedcli-boxes@1.0.0(transitive)
- Removedcli-cursor@2.1.0(transitive)
- Removedcli-table@0.3.11(transitive)
- Removedcli-width@2.2.1(transitive)
- Removedcolor-convert@1.9.3(transitive)
- Removedcolor-name@1.1.3(transitive)
- Removedcolors@1.0.3(transitive)
- Removedcommander@2.20.3(transitive)
- Removedcross-env@5.2.1(transitive)
- Removedcross-spawn@5.1.06.0.6(transitive)
- Removeddebug@4.4.0(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedexeca@0.7.0(transitive)
- Removedexit-hook@1.1.1(transitive)
- Removedexternal-editor@2.2.0(transitive)
- Removedfigures@2.0.0(transitive)
- Removedfs-extra@5.0.0(transitive)
- Removedget-stream@3.0.0(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedhas-flag@3.0.0(transitive)
- Removediconv-lite@0.4.24(transitive)
- Removedinquirer@5.2.0(transitive)
- Removedis-arrow-function@2.0.3(transitive)
- Removedis-callable@1.2.7(transitive)
- Removedis-fullwidth-code-point@2.0.0(transitive)
- Removedis-stream@1.1.0(transitive)
- Removedisexe@2.0.0(transitive)
- Removedjsonfile@4.0.0(transitive)
- Removedlru-cache@4.1.5(transitive)
- Removedmimic-fn@1.2.0(transitive)
- Removedmustache@2.3.2(transitive)
- Removedmute-stream@0.0.7(transitive)
- Removednice-try@1.0.5(transitive)
- Removednode-exceptions@3.0.0(transitive)
- Removednpm-run-path@2.0.2(transitive)
- Removedonetime@2.0.1(transitive)
- Removedos-tmpdir@1.0.2(transitive)
- Removedp-finally@1.0.0(transitive)
- Removedpath-key@2.0.1(transitive)
- Removedpluralize@6.0.0(transitive)
- Removedpseudomap@1.0.2(transitive)
- Removedrequire-stack@1.0.2(transitive)
- Removedrestore-cursor@2.0.0(transitive)
- Removedrun-async@2.4.1(transitive)
- Removedrxjs@5.5.12(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedsemver@5.7.2(transitive)
- Removedshebang-command@1.2.0(transitive)
- Removedshebang-regex@1.0.0(transitive)
- Removedstring-width@2.1.1(transitive)
- Removedstrip-ansi@4.0.0(transitive)
- Removedstrip-eof@1.0.0(transitive)
- Removedsupports-color@5.5.0(transitive)
- Removedsymbol-observable@1.0.1(transitive)
- Removedsyntax-error@1.4.0(transitive)
- Removedterm-size@1.2.0(transitive)
- Removedthrough@2.3.8(transitive)
- Removedtmp@0.0.33(transitive)
- Removeduniversalify@0.1.2(transitive)
- Removedupcast@2.1.2(transitive)
- Removedwhich@1.3.1(transitive)
- Removedwidest-line@2.0.1(transitive)
- Removedxtend@4.0.2(transitive)
- Removedyallist@2.1.2(transitive)
Updateddebug@^3.0.1
Updatedpluralize@^7.0.0