Using npm:
$ npm install sdb-schedule
To run the tests:
$ node test.js
ver 2.0.0 need node 8.9.0
This is schedule framework base node-schedule. Through a simple configuration, you can control schedule jobs. This module provides follow function:
- Cron format string config schedule
- Can dynamic control tasks on/off/update
- Config and task script can In any position
Now we implement an app sdb-schedule-ui,using admin schedules( only support redis drv ),you can download it.
- Base Electron
Clean Redis Data. You Can See document from scp-cleanRedis.
- support regex
- support clean ZSET,List
Increased module scalability, easier to use by other modules.
- Configuration modify
- logger, at root configure, unified logger output, easy to embed other components
- redis drv config
- instanse, at redis configure, Existing redis connection instances.
- sc constructor, add parModule parameter, pass in object for convenience of module, this object will pass in each Job for convenience of Job. Data of parModule can be called through sc.app in < Job >.js
const g_redis = {
ins:null // required, redis instanse
};
cfg_opt:{
instanse: g_redis
}
- use ioredis replace node-redis
- using ES6 syntax
- Fix bug: Next Run Time calc error,if job run time < 1ms
- change stopJob ,add msg parame.
- Fix bug
- Fix bug: Next run time display error.
- Sub Job
Job can add subjob ( updateSubJob ). If subJob not used,you must remove subjob(removeSubJob) by Job. Job and subJob to use the same function
- Change job export function's parame, isStop change as runStep
- 0, init job
- 1, run job
- 2, stop job
- Add nexRunTime attr in job status for redis drv.
- Add update Msg function, now job can call sc.updateMsg set the message.
- Fix Bug, when Job cron parse error, stop job should exception.
- support require module in ./node_modules Path. Using module in node_modules folder,you should only set the module's name in Fun Param.
- Implement #4,Can Edid job's config.
fixed #1
fixed using RedisDrv,Job startTime/stopTime can't record.
Add RedisDrv(Redis configuration manager module),using node-redis;
Refactor the code, separate configuration management module. Now can easy support. Are now able to support more than one type of configuration file management more easily. For example, using the File/Redis/SQL Server storage management plan task configuration File.
- Add File Config module support.
Configuration file using the json format, defined the schedules,as shown below:
{
"schedules":{
"enableRoom":{
"cron":"*/5 * * * * *",
"fun":"./sc/enableRoom.js",
"switch":true
},
"disableRoom":{
"cron":"*/5 * * * * *",
"fun":"./sc/disableRoom.js",
"switch":false
}
}
}
All job defined in schedules fields, each job define as json object,must have 3 fields:(cron,fun and switch).
- cron , define job cron string.
- fun , is the node function module, called when the time of arrival
- switch, switch,told sdb-schedules on/off this job
The cron format consists of:
* * * * * *
┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ |
│ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun)
│ │ │ │ └───── month (1 - 12)
│ │ │ └────────── day of month (1 - 31)
│ │ └─────────────── hour (0 - 23)
│ └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)
Currently, W
(nearest weekday), L
(last day of month/week), and #
(nth weekday
of the month) are not supported. Most other features supported by popular cron
implementations should work just fine.
cron-parser is used to parse crontab instructions.
Since version 1.0.3, profile management as a separate module, the default now provides a kind of configuration file management module (FileDrv), you can extend the configuration file management module based on the requirements, such as using redis configuration management module. We can create the sdb-schedule by the incoming parameters, using the configuration management module specified:
let parModule = {
test:123456,
};
let logger = {
info:(msg)=>{ console.log('--- [sche info]:',msg); },
warn:(msg)=>{ console.log('--- [sche warn]:',msg); }
};
const app = sc({
'cfg_drv':'filedrv.js',
'cfg_opt':{
'cfgFile':"./config.json"
},
logger
},parModule);
- cfg_drv,Specify the use of configuration file management module;
- cfg_opt,Specify the parameters of the configuration file management module, when construct configuration file management module,passed it as parameter.
Using file manager the configuration.
cfg_opt:
- cfgFile,Config file path;
Using Redis manager the configuration. node-redis
cfg_opt:
- host, redis server's host;
- port, redis server's port;
- keyPre, redis key's pre;
- checkInterval, check config interval, mill sec;
- instanse, for redis drv
I am schedule framework, have two part:Frame and JobPlugin.
- Frame, admin the Job Plugin.
- Job Plugin, Implement the schedule Job work.
Work flow like this:
const sc = require("sdb-schedule");
Require module sdb-schedules.const app = sc( { 'cfg_drv':'filedrv.js','cfg_opt':{} });
Construct sc object and give her ths config file path.app.run();
Call run() start work.app.stop();
Stop work.
- run(), start schedules.
- stop(), stop schedules.
- updateJob(name,scCfg), add/update schedule job.
- runJob(name), run job by name.
- stopJob(name,msg), stop job by name.
- getConfig(name), get the job's config.
- updateMsg(jobname,msg), update job'run message.
- updateSubJob(name,scCfg), add/update schedule sub job.
- removeSubJob(name), remove sub job.
Run all job that switch is true
.
No parames.
Stop all job. No parames.
updateJob(name,scCfg )
- name, Job's name, string.
- scCfg, Job's cfg.
{
"corn":<* * * * * * *>,
"fun":"",
"switch":true|false
}
Update Job,
- If cron or fun has change,and the job is running,then restart job.
- If job not run,only change the config.
- If job not exist, add new job,but can't run it ,you must manual run it( call runJob );
runJob(name)
- name, Job's name, string.
stopJob(name,msg)
- name, Job's name, string.
- msg, stop message, string.
getConfig(name)
- name, Job's name, string.
- return, json's format object.
updateMsg(jobname,msg)
- name, Job's name, string.
- msg, the string message.
updateSubJob(name,scCfg )
- name, Sub Job's name, string.
- scCfg, Job's cfg.
{
"corn":<* * * * * * *>,
"switch":true|false
"parent":<parent job's name>
}
removeSubJob(name )
- name, Sub Job's name, string.
Job Plugin,is node module, export as function has three parames.
module.exports = function(sc,job,isStop){}
- sc, {object},instance of sdb-schedule, you can call function
- job, {object},this job info
- isStop,{boolean} ,true means this is stop callback,you can clear resource and so on.
- return 'msg string', {string},function can return string msg. If you using RedisDrv,msg will record to redis,you can look it.
The following is a complete example, example demonstrates the following features:
- Dynamically change the task properties
- Stop the task
module.exports = function(sc,job,isStop){
if( isStop === true ){
return stop( sc,job );
}else{
return run( sc,job );
}
};
let g_cnt = 0;
function run( sc,job)
{
let {app} = sc;
console.log( 'run ' + 20002222, app.test );
g_cnt++;
console.log( job['name'] + " " + g_cnt +" : " + job['cron'] );
if( g_cnt > 10 ){
sc.stopJob( job['name'] ); // example stop this job
}
if( g_cnt > 3 ){
sc.updateJob( job['name'], {
"cron":"*/2 * * * * *",
"fun":"./sc/enableRoom.js",
"switch":true
});
}
return 'Run OK';
}
function stop(sc,job)
{
console.log( 'stop ' + 20002222 );
return;
}
Copyright 2016+ shudingbo
Licensed under the [MIT License].