A package help to manage the transcations scheduled in node.js.
This frame is based on node-schedule,which thanks for the grate work the team of.
npm install @evanpatchouli/scheduler
v1.1.3:
- Fix 1 bug and improve doc.
v1.1.2:
- Refactored by TypeScript.
v1.1.1:
- Task.LogOn: You can deside whether the task need to log.
- Task.cacheOn: You can deside whethre the task need to store histories in memory.
- Plan class: you can bind a group of task into a plan, by manage its tasks by plan
- PlanTool module: a tool to manage the plans in plan pool
- ScheTool module: a tool to help use Scheduler easier.
v1.1.0:
- Since this version, the class Job and JobHelper have been refactored to Task and TaskHelper.
- Besides, there is a new mechanism with a new class Plan, where you can put several related Tasks into it. Altought it is in development so it is of useless at this version, but it will works at next version.
- Fix 1 bug in JobHelper(now named TaskHelper).
const { Task } = require("@evanpatchouli/scheduler");
By Task constructor, you should give 4 params:
- name : Task name
- rule : the rule which Task is scheduled by, and it's same as the rule in "node-schedule"
- todo : the things Task going to do
const task = new Task("HelloTask", "* * * * * *", ()=>{
console.log("Hello, there is Scheduler");
})
- options : it can contain
logOn
andcacheOn
const task = new Task("HelloTask", "* * * * * *", ()=>{
console.log("Hello, there is Scheduler", {logOn: false, cacheOn:false});
})
If you don't give the options of options only have all attributes, true will be default logOn and false will be default cacheOn.
Look at the following example, this task.logOn will be true and its cacheOn will be false.
const task = new Task("HelloTask", "* * * * * *", ()=>{
console.log("Hello, there is Scheduler", {cacheOn:false});
})
Although Task has many other methods, please do not use them directly by Task. You should use Scheduler or Plan or PlanTool or ScheTool to manage your tasks unless you want to manage tasks yourself without Scheduler.
You can put a group of task into a plan and manage them by plan. Before you really start a plan, you must use PlanTool.addPlan to add this plan into Scheduler.plan_pool.
const { Plan } = require('@evanpatchouli/scheduler');
Create a plan without tasks
let plan = new Plan("plan1");
Create a plan with unstarted tasks
let plan = new Plan("plan1",[task1,task2]);
the param should be of Task
plan.addTask(task1);
plan.addTasks([task1,task2]);
Plan.getTask("task1");
the param could be of Task or Task.name
plan.removeTask(task1);
plan.removeTask("task1"); //"task1" is the name of task1
plan.removeTasks([task1,task2]);
Start all tasks of this plan
plan.start();
Stop all tasks of this plan
plan.stop();
Only start one certain task in plan.The task could be Task.name or Task
plan.startTask(task1);
Only start some tasks in plan
plan.startTasks([task1,"task2"]);
Only stop one certain task in plan.The task could be Task.name or Task
plan.stopTask(task1);
Only stop some tasks in plan
plan.stopTasks([task1,"task2"]);
Stop all tasks in this plan and restart them.
plan.restart();
Only restart one certain task in plan.The task could be Task.name or Task
plan.startTask(task1);
Only start some tasks in plan
plan.startTasks([task1,"task2"]);
Restart one certain task in plan. The usage is similar to Scheduler.rescheduleTask
plan.startTask(task1,"* * * * * *");
A module to manage your plans.
const { PlanTool } = require('@evanpatchouli/scheduler');
Before you really start a plan, you must use PlanTool.addPlan to add this plan into Scheduler.plan_pool.
PlanTool.addPlan(plan);
Stop a plan though PlanTool, the param chould be name or Plan
PlanTool.startPlan(plan);
Stop a plan though PlanTool, the param chould be name or Plan
PlanTool.stopPlan(plan);
Stop and remove a plan though PlanTool, the param chould be name or Plan
PlanTool.removePlan(plan);
Stop and remove all plans though PlanTool
PlanTool.dumpPlans();
You can get the Plan object by its name
let plan1 = PlanTool.findPlan("plan1");
const { Scheduler } = require("@evanpatchouli/scheduler");
The manager, a singleton, for you to manage your tasks like add, start, stop, restart, remove...
let scheduler = Scheduler.getInstance();
Add a new task into task pool, the name of task must be unique and does not ends with "-helper"
const task = new Task("HelloTask", "* * * * * *", ()=>{
console.log("Hello, there is Scheduler");
})
scheduler.addTask(task);
It is just be add into task pool without being started. If you want to start the task as soon as it is added, do like this:
scheduler.addTask(task, true);
You can use addTasks to add a series of Tasks into task pool
const task1 = new Task("HelloTask1", "* * * * * *", ()=>{
console.log("Hello, there is Scheduler");
})
const task2 = new Task("HelloTask2", "* * * * * *", ()=>{
console.log("Hello, there is Scheduler");
})
scheduler.addTasks([task1,task2]);
You can also give the second param as true to make all of them to start at once.
In future, this method may support to appoint every task whether to start or not.
scheduler.getTask("task1");
If a task is not running(hasn't been started or has been stoped), use startTask can start it at once.
scheduler.start("HelloTask");
Or
const task = new Task("HelloTask", "* * * * * *", ()=>{
console.log("Hello, there is Scheduler");
})
scheduler.start(task);
Both name and Task are allowed.
A method to start a series of tasks at once, and both Array of name and Task are allowed.
A method to start all tasks in task pool.
A method to stop a task schedule, and its usage is similar to addTask.
A method to stop a series of tasks schedule, and its usage is similar to addTasks.
A method to stop all tasks in task pool.
A method to remove a task from pool, and its usage is similar to addTask. If the task is alive, it will be canceld first by Scheduler and then be removed.
A method to remove a series of tasks, and its usage is similar to addTasks.
A method to restart a task, and its usage is similar to addTask.
A method to restart a series of tasks schedule, and its usage is similar to addTasks.
A method to reschedule a task(Only reset rule, will not reset todo).
You can use it like:
scheduler.rescheduleTask("HelloTask","*\5 * * * * *");
Or
const task = new Task("HelloTask", "*\5 * * * * *", ()=>{})
scheduler.rescheduleTask(task);
Or
const task = new Task("HelloTask", "*\5 * * * * *", ()=>{})
scheduler.rescheduleTask({task: task});
Or
const task = new Task("HelloTask", "* * * * * *", ()=>{})
scheduler.rescheduleTask({task: task, rule: "*\5 * * * * *"});
Or
scheduler.rescheduleTask({task: "HelloTask", rule: "* * * * * *"});
When you both give task and rule ( rescheduleTask({task,rule}) or rescheduleTask(task,rule) ), if rule is null or undefind, Scheduler will use task.rule to reschedule it.
(Unaccomplished) A method to reschedule a series of tasks schedule.
const { ScheTool } = require('@evanpatchouli/scheduler');
Remove all tasks without care about dumpPlans
ScheTool.dumpTaskPool();
Every task added into pool, Scheduler will auto rigister an one-to-one Task named ${task.name}-helper
to help manage this task, release cache regularly...
And TaskHelper's logOn and cacheOn will be same with the task it helps.
const { Starter } = require("@evanpatchouli/scheduler");
A module provides packaged functions to init and start Scheduler and it can print Scheduler
Logo in consle at fitst time.
Starter.run(isLogo, todo, params):
- isLogo : whether to ptint
Scheduler
Logo, if you don't want to print Logo, you can givefalse
or not give any param. - a function packaged with the things you want to do
- if todo needs params, please make them to
k-v
object
Example
let params = {
plan,task1,task2
}
Starter.run(true, todo, params);
function todo(params) {
setTimeout(()=>{PlanTool.startPlan(params.plan);},5000);
setTimeout(()=>{Scheduler.stopAll();},20000);
}
if you start task or plan in todo,must avoid to do actions of start/stop... by Scheduler/PlanTool/ScheTool at once, because js is single thread, if you have to do, please use setTimeout to create enough time delay between Starter and your outer star/stop/dump action code.