通过活动作业处理相关销毁 [英] Handle dependent destroy via active jobs

查看:69
本文介绍了通过活动作业处理相关销毁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个有很多孩子的模特.依赖破坏变得非常严重.任何人都知道将依赖破坏与活动工作联系起来的方法吗?或者,我唯一的选择是通过父模型上的回调来移除依赖的销毁和角色我自己的作业吗?

I have a couple models with many children. Dependent destroy has gotten really heavy. Anyone know of a way to tie dependent destroy into active jobs? Or, is my only option to remove dependent destroy and role my own jobs via callbacks on the parent model?

推荐答案

您可以创建一个工作线程来异步销毁模型并将其删除.类似的东西:

You can create a worker to destroy the models async and enqueue it's deletion. Something like:

class ComplexModelDeletion < ActiveJob::Base
  def perform(model)
    model.destroy!
  end
end

模型可能是这样的:

class Model < ActiveRecord::Base
  def destroy_later
    ComplexModelDeletion.enqueue(self)
  end
end

然后,每当您需要终止此模型的实例时,您可以调用 Model#destroy_later 将其加入队列.您甚至可以在将对象加入队列之前将其标记为 deleted 以防止它在实际被杀死之前从数据库中检索.

Then, whenever you need to kill an instance of this model, you could call Model#destroy_later to enqueue it. You could even mark the object as deleted before enqueuing it just to prevent it from being retrieve from DB before it is actually killed.

不确定这是否会照原样,但只是为了让您了解您可以做什么.

Not sure if this would work as is, but just to give you an idea of what you could do.

这篇关于通过活动作业处理相关销毁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆