gpt4 book ai didi

ruby-on-rails - 使用销毁的对象将 ActiveJob 任务排入队列

转载 作者:行者123 更新时间:2023-12-04 01:11:14 24 4
gpt4 key购买 nike

在 Controller 的操作中,我销毁了一条记录,然后将其作为参数传递给 ActiveJob。

def destroy
post = Post.find params[:id]
post.destroy
CleanUpJob.perform_later post
end

在我的工作执行中,我需要对被破坏的记录执行一些清理操作。
def perform(post)
log_destroyed_content post.id, post.title
end

当我用 .perform_later 称它为延迟时 - 它根本不执行。但是当我更改为 .perform_now 时 - 它按预期工作。这个 Job 需要处理销毁和持久化的记录。

我正在使用lates Rails,具有默认异步事件作业适配器的开发环境。

最佳答案

当您调用 .perform_laterActiveRecord对象,ActiveJob将尝试将其序列化为 global id

您正在从数据库中删除您的记录,这意味着您的作业在运行时将找不到它。

您可以传递包含所有属性的哈希:CleanUpJob.perform_later(post.attributes)
或者,您可以将模型标记为删除,并在实际完成记录后在作业中调用 destroy 。将其视为首先软删除记录:

# in the controller
def destroy
post = Post.find params[:id]
post.update(state: :archived) # or whatever makes more sense for your application
CleanUpJob.perform_later(post.id, post.title)
end

# in the job
def perform(post_id, post_title)
log_destroyed_content(post_id, post_title)
post.destroy
end

您需要确保从面向用户的查询中排除“软删除”记录。

关于ruby-on-rails - 使用销毁的对象将 ActiveJob 任务排入队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55435513/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com