简体   繁体   English

是否可以立即运行延迟作业但仍异步运行?

[英]Is it possible to run a delayed job immediately but still asynchronously?

We are using DelayedJob to run tasks in the background because they could take a while, and also because if an error is thrown, we still want the web request to succeed.我们使用DelayedJob在后台运行任务,因为它们可能需要一段时间,也因为如果抛出错误,我们仍然希望 Web 请求成功。

The issue is, sometimes the job could be really big (changing hundreds or thousands of database rows) and sometimes it could be really small (like 5 db rows).问题是,有时工作可能非常大(更改数百或数千个数据库行),有时可能非常小(如 5 db 行)。 In the case of the small ones, we'd still like to have it run as a delayed job so that the error handling can work the same way, but we'd love to not have to wait roughly 5 seconds for DJ to pick up the job.对于较小的情况,我们仍然希望将其作为延迟作业运行,以便错误处理可以以相同的方式工作,但我们希望不必等待大约 5 秒钟让 DJ 接听工作。

Is there a way to queue the job so it runs in the background, but then immediately run it so we don't have to wait for the worker to execute 5 seconds later?有没有办法将作业排队以便它在后台运行,然后立即运行它,这样我们就不必等待工作人员在 5 秒后执行?

Edit: Yes, this is Ruby on Rails :-)编辑:是的,这是 Ruby on Rails :-)

Delayed Job polls the database for new dj records at a set interval.延迟作业以设定的时间间隔轮询数据库以获取新的 dj 记录。 You can reconfigure this interval in an initializer:您可以在初始化程序中重新配置此间隔:

# config/delayed_job.rb
Delayed::Worker.sleep_delay = 2 # or 1 if you're feeling wild.

This will affect DJ globally.这将在全球范围内影响 DJ。

How about怎么样

SomeJob.set(
    wait: 0,
    queue: "queue_name",
  ).perform_later

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

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