ruby-on-rails - rails 5 : delete an ActiveJob before it gets performed

标签 ruby-on-rails rails-activejob

我有一个 ActiveJob,它在创建 X 分钟后将记录的状态字段从“待处理”更改为“实时”。它运作良好。

当用户在 X 分钟内编辑记录时,我需要推回该状态更改,即。重新启动时钟。大概我会通过取消 ActiveJob 并创建一个新的来做到这一点。 Rails Guides for ActiveJob不要提这应该怎么做。

我看到我可以将 ActiveJob 分配给这样的变量:

j = ThingLiveJob.set(wait: 1.minute).perform_later(@thing)

并使用 byebug在这一行之后(在 Controller 中),我看到它输出如下:
#<ThingLiveJob:0x000000134a5fc0 @arguments=[#<Thing id: 1095, body: "Whatever", user_id: 1, created_at: "2018-11-19 10:34:24", updated_at: "2018-11-19 10:34:24", status: "pending">], @job_id="aab28c66-f8b4-491e-9c7f-af6f27aa482e", @queue_name="default", @priority=nil, @executions=0, @scheduled_at=1542623724.674397, @provider_job_id="61c154d5-9c1b-45a1-8487-824a4412c53c">

但是,在控制台上,这些(猜测)都不起作用:
j.destroy

j.delete

那我该怎么办呢?

最佳答案

我在我的项目中解决了类似的情况,如下所示......我在每次记录更新或创建后将 active_job 排队,但我称之为:

ThingLiveJob.set(wait: 1.minute).perform_later(@thing.id, @thing.updated_at)

然后在activejob代码中...
def perform(thing_id, thing_updated_at)
  thing = Thing.find(thing_id)
  return if thing_updated_at != thing.updated_at
  ...
end

所以基本上我让事件作业运行,但事件作业检查自当前事件作业排队以来,事物对象没有更新。

关于ruby-on-rails - rails 5 : delete an ActiveJob before it gets performed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53373100/

相关文章:

ruby-on-rails - 事件作业 : How to check if same job has already been scheduled

ruby-on-rails - 使用 ActiveJob 时是否仍只传递对象 ID?

ruby-on-rails - 在 Rails 3.2.11 中的 label_tag 中渲染 html(html_safe,raw 不起作用)

ruby-on-rails - 如何在RoR中初始化Model对象?

ruby-on-rails - 使用Sidekiq进行事件作业并获取ActiveJob::DeserializationError

ruby-on-rails - Rails - 如何在 ActiveJob 中使用 ActionDispatch::Integration::Session

ruby-on-rails - rails 5 : Adding conditional custom validations

ruby-on-rails - 如何使用 XPath 检查子元素的位置

ruby-on-rails - 使用 Rails 中的按钮更新记录?

ruby-on-rails - 无法将 CollectionProxy 对象传递给 ActiveJob