MEANIO集群4只能运行一次预定的作业?

我想看到发生的事情就是MEAN将所有四个组合起来。 然后运行一个预定的工作,每天晚上20:30给我的客户发电子邮件。目前运行不止一次。 我如何做到这一点?

'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); }); } 

OUTPUT

 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 

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

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

你可能会考虑使用这个工作队列,而不是直接邮寄队列和从那里进程。 在addtoQueue之前,看看现有的任务是否已经存在,从而避免多个实例。 我已经使用了Kue,并且听到了关于busmq的好消息