2015-02-24 34 views
2

我想看到发生的事情是让MEAN旋转了所有四个集群。然后运行计划的工作,每天20:30以电子邮件向我的客户发送电子邮件。目前运行的次数不止一次。 我该如何做到这一点?MEANIO 4个群集只能运行一次计划的Job?

'use strict'; 
var mean = require('meanio'); 
var cluster = require('cluster'); 
var schedule = require('node-schedule'); 

var z = schedule.scheduleJob('30 20 * * *', function() { 
    console.log('test the scheduler') 
}); 
if (cluster.isMaster) { 


// Count the machine's CPUs 
var cpuCount = require('os').cpus().length; 

// Create a worker for each CPU 
for (var i = 0; i < cpuCount; i += 1) { 
    console.log ('forking ',i); 
    cluster.fork(); 
} 

// Listen for dying workers 
cluster.on('exit', function (worker) { 
    // Replace the dead worker, we're not sentimental 
    console.log('Worker ' + worker.id + ' died :('); 
    cluster.fork(); 

}); 


} else { 

var workerId = 0; 
if (!cluster.isMaster) 
{ 
    workerId = cluster.worker.id; 
} 


    mean.serve({ workerid: workerId /* more options placeholder*/ }, function (app, config) { 
     var port = config.https && config.https.port ? config.https.port : config.http.port; 
     console.log('Mean app started on port ' + port + ' (' + process.env.NODE_ENV + ') cluster.worker.id:', workerId); 
    }); 
} 

输出

forking 0 
forking 1 
forking 2 
forking 3 
Mean app started on port 3000 (development) cluster.worker.id: 1 
Mean app started on port 3000 (development) cluster.worker.id: 2 
Mean app started on port 3000 (development) cluster.worker.id: 3 
Mean app started on port 3000 (development) cluster.worker.id: 4 
test the scheduler 
test the scheduler 
test the scheduler 
test the scheduler 
test the scheduler 

回答

1

为了澄清,你问为什么你的调度程序运行一次以上?如果是这样,这是因为cluster.fork()的工作方式与unix fork十分相似,因为它创建了另一个基本上是当前副本的进程。每个流程都会在自己的参考框架中安排工作,并且每个流程都将执行该工作。您可能希望确保只通过在if(cluster.isMaster) {内移动调度来安排主进程中的作业。

通过这样做,您可以指示主人派遣工作给工人。不过,您必须自己添加负载平衡。

0

您可能会考虑使用作业队列来处理此问题,而不是直接将邮件放置在队列中,并从此处开始处理。 在addtoQueue之前,看看现有任务是否已经存在,从而避免多个实例。 我已使用Kue并听说过好东西busmq