我想要看到的是让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);
});
}
输出
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
答案 0 :(得分:1)
澄清一下,您是否在问为什么您的调度程序运行多次?如果是这样,那是因为cluster.fork()
的工作原理与unix fork完全相同,因为它创建了另一个进程,它实际上是当前进程的副本。每个流程都将在其自己的参考框架中安排一个工作,每个工作都将执行该工作。您可能希望确保仅通过移动if(cluster.isMaster) {
内的计划来安排主进程中的作业。
通过这样做,您可以指示主人将工作分派给工人。但是,您必须自己添加负载平衡。
答案 1 :(得分:0)