简体   繁体   English

我可以在node.js中将变量传递给cluster.fork吗?

[英]Can I pass variables to cluster.fork in node.js?

So right now I have a bunch of chat bots, and I use a wrapper to start each one as a child process. 因此,现在我有一堆聊天机器人,并且使用包装器将每个机器人作为一个子进程启动。 It forks a config file. 它派生一个配置文件。 However, since each bot has a webiste, it means that express has to run on an unique port for each bot. 但是,由于每个bot都有一个webiste,这意味着express必须在每个bot的唯一端口上运行。 I would like to have all the bots run the webserver on the same port. 我希望所有漫游器都在同一端口上运行网络服务器。 Which led me to look at cluster rather than child. 这导致我去看集群而不是孩子。

I am currently using child.fork(config.js); 我目前正在使用child.fork(config.js); to spawn the bots, and it's working as well as can be expected. 生成机器人,它的工作效果可想而知。

But Is there any way to pass the config variables to cluster.fork so each worker can connect to its own chat room with its own login credentials? 但是,是否有任何方法可以将config变量传递给cluster.fork以便每个工作人员都可以使用自己的登录凭据连接到自己的聊天室?

And if it is possible, is there a way to kill/restart a worker like a child process? 并且,如果有可能,有没有办法像子进程那样杀死/重新启动工人?

Check out the documentation 查看文档

You could augment your process.env , and then use that in the forked process. 您可以扩展process.env ,然后在分支过程中使用它。

var _ = require('lodash');
var child = require('child_process');
var env = _.clone(process.env);

env.BOT_ID = 'dalton';
child.fork('./config.js', { env: env });

I'd suggest you keep the smallest amount of info in env , and then just use that to grab the configuration from a JSON file, or something. 我建议您在env保留最少量的信息,然后仅使用它从JSON文件或其他内容中获取配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM