简体   繁体   English

Node.js集群模块困境

[英]Node.js cluster module dilemma

I understand what scaling is and what the node.js cluster module is supposed to do by taking advantage of the remaining cores to improve concurrency. 我了解扩展的含义以及应该通过利用其余核心来提高并发性来实现node.js集群模块的功能。

But here is my question: If I have a certain function to run at, lets say 6pm daily, how do I prevent all 8 workers from running the function 8 times at 6pm instead of once? 但是,这是我的问题:如果我有某个特定的功能要运行,比如说每天下午6点,如何防止所有8位工作人员在下午6点运行8次而不是一次?

You can set an interval in master process and pick a random worker to do the job every time. 您可以在主流程中设置时间间隔,并每次选择一个随机的工作人员来完成工作。 You can use worker.send(message) function to let the worker know about the new task. 您可以使用worker.send(message)函数让工作人员知道新任务。

Another way is to prepare an API method that your app exposes. 另一种方法是准备应用程序公开的API方法。 This method will trigger task execution when received. 收到此方法将触发任务执行。 To call this API method, you can use a regular cron. 要调用此API方法,可以使用常规cron。 That way, your app will be aware of the work it does, but not of the schedule. 这样,您的应用程序将知道它所做的工作,而不是时间表。

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

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