javascript Meteor 中的后台任务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11703010/
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-26 14:03:23  来源:igfitidea点击:

Background tasks in Meteor

javascriptmeteornpm

提问by Leonid Bugaev

I'm wondering, is there is way to implement background taks, maybe with workers pool. Can you show me direction, i'm thinking about writing package for this?

我想知道,有没有办法实现后台任务,也许是工作人员池。你能告诉我方向吗,我正在考虑为此编写包?

回答by Dan Dascalescu

2019 update

2019年更新

Before thinking about writing a package for anything, first look if there are existing packages that do what you need. In the Meteor world, this means looking on Atmosphere for "job/queue/task/worker management/scheduling" packages, then on npm for the same search terms. You also need to define your requirements more precisely:

在考虑为任何东西编写包之前,首先看看是否有现有的包可以满足您的需求。在 Meteor 世界中,这意味着在 Atmosphere 上查找“作业/队列/任务/工人管理/调度”包,然后在 npm 上查找相同的搜索词。您还需要更精确地定义您的要求:

  • do you want persistence, or would an in-memory solution work?
  • do you want to be able to distribute jobs to different machines?
  • 你想要持久性,还是内存中的解决方案有效?
  • 您是否希望能够将作业分配到不同的机器?

Meteor-specific

流星专用

  • job-collection- reliable (I used it in 2014 in production at a startup), but currently in maintenance mode. Lets you schedule persistent jobs to be run anywhere (servers, clients).
  • SteveJobs- actively maintained by Max Savin, the author of several powerful Meteor tools
  • littledata:synced-cron- "A simple cron system for Meteor. It supports syncronizing jobs between multiple processes."
  • 作业收集- 可靠(我在 2014 年在启动时在生产中使用它),但目前处于维护模式。让您安排持久作业在任何地方(服务器、客户端)运行。
  • SteveJobs- 由 Max Savin 积极维护,他是几个强大的 Meteor 工具的作者
  • littledata:synced-cron- “流星的一个简单的 cron 系统。它支持在多个进程之间同步作业。”

Abandoned packages:

废弃的包裹:

Npm packages

npm 包

Meteor has been able to use npm packages directly for several years now, so this question amounts to finding job/worker/queue management packageson NPM. If you don't care about persistence:

Meteor 已经能够直接使用 npm 包好几年了,所以这个问题相当于在 NPM 上查找作业/工人/队列管理包。如果你不在乎持久性:

  • Async"provides around 70 functions that include the usual 'functional' suspects (map, reduce, filter, each...) as well as some common patterns for asynchronous control flow (parallel, series, waterfall...)"
  • d3-queue- minimalistic, written by D3 author Mike Bostock
  • 异步“提供了大约 70 个函数,其中包括通常的‘功能性’可疑对象 ( map, reduce, filter, each...) 以及异步控制流的一些常见模式 ( parallel, series, waterfall...)”
  • d3-queue- 简约,由 D3 作者 Mike Bostock 编写

If you do want persistence, since Meteor uses MongoDB already, it may be advantageous to use a job scheduling package with persistence to MongoDb. The most powerful and popular seems to be Agenda, but unfortunately it hasn't been maintained in months, and it has a significant backlog of issues.

如果您确实想要持久性,因为 Meteor 已经使用了 MongoDB,使用具有持久性的作业调度包可能对 MongoDb 有利。最强大和最流行的似乎是Agenda,但不幸的是它几个月没有维护,并且它有大量积压的问题

If you're willing to add a dependency backed by redisto your project, there are more choices:

如果您愿意在项目中添加由redis支持的依赖项,还有更多选择:

Like MongoDB, Redis can also provide high-availability (via Redis Sentinel), and if you want to distribute jobs among multiple worker machines, you can point them all at the same Redis server.

和 MongoDB 一样,Redis 也可以提供高可用性(通过 Redis Sentinel),如果你想在多台工作机器之间分配作业,你可以将它们都指向同一个 Redis 服务器

回答by Max Savin

If you are looking for something that is specific to Meteor, I am happy to share there is a new package called Steve Jobs. It makes running background jobs as easy as calling a Method.

如果您正在寻找特定于 Meteor 的东西,我很高兴与大家分享一个名为 Steve Jobs 的新软件包。它使运行后台作业就像调用方法一样简单。

It has all the standard features you would expect, such as running a job only once, retrying failed jobs, and so on. You can learn more about it on GitHub:

它具有您期望的所有标准功能,例如仅运行一次作业、重试失败的作业等。您可以在 GitHub 上了解更多信息:

http://github.com/msavin/stevejobs

http://github.com/msavin/stevejobs

回答by miketucker

There is a package based on Cron jobs which can be used to schedule tasks on certain intervals or dates. Here is the package: https://atmosphere.meteor.com/package/cron

有一个基于 Cron 作业的包,可用于在特定时间间隔或日期安排任务。这是包:https: //atmosphere.meteor.com/package/cron

And if you happen to look into the source of that package, you'll notice they're simply using:

如果您碰巧查看该包的来源,您会注意到它们只是在使用:

Meteor.setInterval( ... , delay );

Meteor.setInterval( ... , delay );

So if you save your tasks in a database, then load them into intervals during startup, then you'll probably be on the right track.

因此,如果您将任务保存在数据库中,然后在启动期间将它们加载到间隔中,那么您可能会走上正确的轨道。

回答by Tom Coleman

I'm guessing proper support is on their roadmap, but in the meantime I've managed to get it going in a hacky way via setInterval. See the cron-tickpackage.

我猜他们的路线图上有适当的支持,但与此同时,我已经设法通过setInterval. 查看cron-tick包。