Celery 中的持久性长 运行 任务

Persistent Long Running Tasks in Celery

我正在开发基于 Python 的系统,以便将 运行ning 任务排入队列。

任务源自生成 "token" 的外部服务,但一旦它们基于该令牌创建,它们就应该 运行 连续,并且只有在被代码明确删除时才会停止。
该任务启动 WebSocket 并在其上循环。如果套接字已关闭,它会重新打开它。基本上,任务应该不会有结果。

我设计此解决方案的目标是:

  1. 当从容地重启一个 worker(例如加载新代码)时,任务应该重新添加到队列中,并由某个 worker 拾取。
  2. 非正常关机时也会发生同样的事情。
  3. 2 个工人不应该在同一个令牌上工作。
  4. 其他进程可能会创建更多任务,这些任务应定向到处理特定令牌的同一个工作人员。这将通过将这些任务发送到以令牌命名的队列来解决,工作人员应在启动令牌的任务后开始收听。我列出此要求是为了解释为什么这里甚至需要任务引擎。
  5. 独立服务器、快速代码重新加载等 - 每个任务的停机时间最少。

我们所有的服务器端都是 Python,看起来 Celery 是它的最佳平台。 我们在这里使用正确的技术吗?我们应该考虑的任何其他架构选择?

感谢您的帮助!

根据 docs

When shutdown is initiated the worker will finish all currently executing tasks before it actually terminates, so if these tasks are important you should wait for it to finish before doing anything drastic (like sending the KILL signal).

If the worker won’t shutdown after considerate time, for example because of tasks stuck in an infinite-loop, you can use the KILL signal to force terminate the worker, but be aware that currently executing tasks will be lost (unless the tasks have the acks_late option set).

您可能会通过使用 retry or acks_late

得到您想要的东西

总的来说,我认为您需要实施一些额外的应用程序端作业控制,也许还需要一个锁定服务。

但是,是的,总的来说你可以用芹菜做到这一点。是否有更好的技术...这超出了本站的范围。