javascript 使用 Node JS 运行连续的后台作业

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19349162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:17:01  来源:igfitidea点击:

Run continuous Background Job with Node JS

javascriptnode.js

提问by user2874299

Consider there is a task A and other n tasks. I wan to run a task A in parallel to other n tasks. Task A is just fetching data from queue in every 5 seconds.

考虑有一个任务 A 和其他 n 个任务。我想与其他 n 个任务并行运行任务 A。任务 A 只是每 5 秒从队列中获取数据。

I am new to Node JS. Is there any way to run this task/job A in background or is there any solution ??

我是 Node JS 的新手。有没有办法在后台运行这个任务/作业 A 或者有什么解决方案?

回答by Zeke Alexandre Nierenberg

Depends a lot on what the tasks are. If I understand your question, you can do this two ways: 1, run a function with a timer, and 2, spawn a child process.

很大程度上取决于任务是什么。如果我理解您的问题,您可以通过两种方式执行此操作:1,使用计时器运行函数,以及 2,生成子进程。

1

1

function taskA(){...}

setInterval(taskA,5000);

2

2

//same code as 1, but in a child process
var spawn = require('child_process').spawn,
ls    = spawn('taskA.js');
//taskA.js has the code from example 1

You might prefer 2 to 1 if you are doing a lot of other things in the main process, because node is single threaded. It should also be noted that there are likely better ways to do this in certain circumstances. For example, in a cloud-based webapp, I might rely on the PAAS's services to run the background task. You also might want to look into https://github.com/nodejitsu/forever-monitor

如果您在主进程中做很多其他事情,您可能更喜欢 2 比 1,因为节点是单线程的。还应该注意的是,在某些情况下可能有更好的方法来做到这一点。例如,在基于云的 web 应用程序中,我可能依赖 PAAS 的服务来运行后台任务。您可能还想查看https://github.com/nodejitsu/forever-monitor

Here's a great article on how to handle background jobs in webapps. https://devcenter.heroku.com/articles/background-jobs-queueingIt isn't node specific, however. It is also specific to the Heroku platform.

这是一篇关于如何在 webapps 中处理后台作业的好文章。https://devcenter.heroku.com/articles/background-jobs-queueing然而,它不是特定于节点的。它也特定于 Heroku 平台。

回答by Markus

You can use the standard setTimeout()method.

您可以使用标准setTimeout()方法。

function task() {
  console.log("Timer");
  setTimeout(task, 5000);
}

task();

回答by Luke H

Have a look at Kue - https://github.com/LearnBoost/kueThis is a node message queue system that's easy to use. Basically you'd have one node program that puts things onto the queue, and other processes that deal with the queue.

看看Kue - https://github.com/LearnBoost/kue这是一个易于使用的节点消息队列系统。基本上,您将拥有一个将事物放入队列的节点程序,以及处理队列的其他进程。

So, Task A would be run every 5 seconds (cron if you like, or another system). It would look at the Queue and deal with relevant items. The other nworkers would share config for Kue but would only push events on.

因此,任务 A 将每 5 秒运行一次(如果您愿意,可以使用 cron 或其他系统)。它将查看队列并处理相关项目。其他n 个工人将共享 Kue 的配置,但只会推送事件。