I am looking for a good solution on fire different javascript files with nodejs at a given time on a Server running on Ubuntu.
我正在寻找一个很好的解决方案,在Ubuntu上运行的服务器上,在给定时间使用nodejs触发不同的javascript文件。
I have about 10 different scripts and each should be fired 15 times a day! My first approach was to use at but this gets really messy with that much events.
我有大约10个不同的脚本,每天应该被解雇15次!我的第一种方法是使用at,但这对于那么多事件都非常混乱。
Now I thought about using Node-Schedule what some of you guys here on SO suggested but I'm not sure if that's the best solution for my task ...
现在我想到使用Node-Schedule,你们有些人在这里建议但是我不确定这是否是我的任务的最佳解决方案......
If Node-Schedule should still be the best approach - what structure would you use?
如果节点计划仍然是最好的方法 - 您将使用什么结构?
var schedule = require('node-schedule');
var d1 = new Date(2015, 10, 20, 18, 55, 0);
var d2 = new Date(2015, 10, 20, 18, 58, 0);
var xd1 = schedule.scheduleJob(d1, function(){
test.js;
});
var xd2 = schedule.scheduleJob(d2, function(){
test.js;
});
Doesn't seems that DRY ... ;)
好像不干......;)
2 个解决方案
#1
1
I wouldn't create a node program just to schedule another node program. Use plain cron.
我不会创建一个节点程序只是为了安排另一个节点程序。使用普通的cron。
If you can run your job 16 times a day instead of 15 times, it would be every 90 minutes which you can schedule with these two cron expressions:
如果你可以每天运行16次而不是15次,那么你可以用这两个cron表达式计划每90分钟:
' 0 0/3 * * * node test.js'
'30 1/3 * * * node test.js'
If it has to be 15 times, this schedule has pretty good distribution (at most once an hour, at least every other hour, 15 times per day):
如果它必须是15次,这个时间表有相当好的分布(最多每小时一次,至少每隔一小时一次,每天15次):
'0 0/2,1,9,17 * * * node test.js'
Any difficulty understanding this schedule, use this site. If you need your 15 invocations to be spaces equally (every 96 minutes), I'm afraid you'd need to break them into 5 schedules:
理解此计划有任何困难,请使用本网站。如果你需要15个调用空间(每96分钟一次),我恐怕你需要将它们分成5个时间表:
0 0,8,16 * * * node test.js
36 1,9,17 * * * node test.js
12 3,11,19 * * * node test.js
48 4,12,20 * * * node test.js
24 6,14,22 * * * node test.js
#2
1
If you need to run your scripts on a daily basis, this date based scheduling is not the best solution. The cron style scheduling is a better fit.
如果您需要每天运行脚本,则基于日期的日程安排不是最佳解决方案。 cron样式调度更适合。
For example
var xd1 = schedule.scheduleJob('0 * * * *', function(){
test.js;
});
will run your function every hour (0:00, 1:00, 2:00 and so on = 24 times a day).
将每小时运行您的功能(0:00,1:00,2:00,等等=每天24次)。
And
var xd1 = schedule.scheduleJob('0 */2 * * *', function(){
test.js;
});
will run your function every two hours (0:00, 2:00, 4:00 and so on = 12 times a day).
将每两小时运行一次你的功能(0:00,2:00,4:00,等等=每天12次)。
Is the 15 times a day a hard requirement? You can't define this kind of interval very easy with the cron syntax, without splitting it up into different cronjobs (= define more job with the same function call).
一天15次是硬性要求吗?你不能用cron语法很容易地定义这种间隔,而不是将它分成不同的cronjobs(=使用相同的函数调用定义更多的作业)。
This example would run 15 times a day, but it is not balanced throughout the day:
这个例子每天运行15次,但在一天中不平衡:
var xd1 = schedule.scheduleJob('*/25 */5 * * *', function(){
test.js;
});
#1
1
I wouldn't create a node program just to schedule another node program. Use plain cron.
我不会创建一个节点程序只是为了安排另一个节点程序。使用普通的cron。
If you can run your job 16 times a day instead of 15 times, it would be every 90 minutes which you can schedule with these two cron expressions:
如果你可以每天运行16次而不是15次,那么你可以用这两个cron表达式计划每90分钟:
' 0 0/3 * * * node test.js'
'30 1/3 * * * node test.js'
If it has to be 15 times, this schedule has pretty good distribution (at most once an hour, at least every other hour, 15 times per day):
如果它必须是15次,这个时间表有相当好的分布(最多每小时一次,至少每隔一小时一次,每天15次):
'0 0/2,1,9,17 * * * node test.js'
Any difficulty understanding this schedule, use this site. If you need your 15 invocations to be spaces equally (every 96 minutes), I'm afraid you'd need to break them into 5 schedules:
理解此计划有任何困难,请使用本网站。如果你需要15个调用空间(每96分钟一次),我恐怕你需要将它们分成5个时间表:
0 0,8,16 * * * node test.js
36 1,9,17 * * * node test.js
12 3,11,19 * * * node test.js
48 4,12,20 * * * node test.js
24 6,14,22 * * * node test.js
#2
1
If you need to run your scripts on a daily basis, this date based scheduling is not the best solution. The cron style scheduling is a better fit.
如果您需要每天运行脚本,则基于日期的日程安排不是最佳解决方案。 cron样式调度更适合。
For example
var xd1 = schedule.scheduleJob('0 * * * *', function(){
test.js;
});
will run your function every hour (0:00, 1:00, 2:00 and so on = 24 times a day).
将每小时运行您的功能(0:00,1:00,2:00,等等=每天24次)。
And
var xd1 = schedule.scheduleJob('0 */2 * * *', function(){
test.js;
});
will run your function every two hours (0:00, 2:00, 4:00 and so on = 12 times a day).
将每两小时运行一次你的功能(0:00,2:00,4:00,等等=每天12次)。
Is the 15 times a day a hard requirement? You can't define this kind of interval very easy with the cron syntax, without splitting it up into different cronjobs (= define more job with the same function call).
一天15次是硬性要求吗?你不能用cron语法很容易地定义这种间隔,而不是将它分成不同的cronjobs(=使用相同的函数调用定义更多的作业)。
This example would run 15 times a day, but it is not balanced throughout the day:
这个例子每天运行15次,但在一天中不平衡:
var xd1 = schedule.scheduleJob('*/25 */5 * * *', function(){
test.js;
});