Rails 5:在 ActiveJob 执行之前删除它
Rails 5: delete an ActiveJob before it gets performed
我有一个 ActiveJob,它在记录创建 X 分钟后将记录的状态字段从 "pending" 更改为 "live"。效果不错。
当用户在 X 分钟内编辑记录时,我需要将状态更改推回,即。重新启动时钟。大概我会通过取消 ActiveJob 并创建一个新的来做到这一点。 Rails Guides for ActiveJob 没有提到应该如何完成。
我看到我可以将 ActiveJob 分配给这样的变量:
j = ThingLiveJob.set(wait: 1.minute).perform_later(@thing)
并在这一行之后(在控制器中)立即使用 byebug
,我看到它的输出如下:
#<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
所以基本上我让活动作业 运行,但是活动作业检查自当前活动作业排队以来事物对象没有更新。
我有一个 ActiveJob,它在记录创建 X 分钟后将记录的状态字段从 "pending" 更改为 "live"。效果不错。
当用户在 X 分钟内编辑记录时,我需要将状态更改推回,即。重新启动时钟。大概我会通过取消 ActiveJob 并创建一个新的来做到这一点。 Rails Guides for ActiveJob 没有提到应该如何完成。
我看到我可以将 ActiveJob 分配给这样的变量:
j = ThingLiveJob.set(wait: 1.minute).perform_later(@thing)
并在这一行之后(在控制器中)立即使用 byebug
,我看到它的输出如下:
#<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
所以基本上我让活动作业 运行,但是活动作业检查自当前活动作业排队以来事物对象没有更新。