Ruby-on-rails 销毁 Rails 中的所有延迟工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9683050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:02:13  来源:igfitidea点击:

Destroying all delayed job in rails

ruby-on-railsrubydelayed-job

提问by Rahul Tapali

I am using collectiveidea for rails 2.3.8. I am creating array of delayed jobs to perform some tasks, after some time I want to destroy all the delayed jobs which are running. If anyone know the way to do this please help me.

我正在为 rails 2.3.8 使用集体想法。我正在创建一系列延迟作业来执行一些任务,一段时间后我想销毁所有正在运行的延迟作业。如果有人知道如何做到这一点,请帮助我。

回答by Lee Jarvis

You can invoke rake jobs:clearto delete all jobs in the queue.

您可以调用rake jobs:clear以删除队列中的所有作业。

回答by Ryan Brunner

In addition to the rake task, DelayedJob jobs are just a normal ActiveRecord model, so if you're in Ruby code you can do what you like with them:

除了 rake 任务之外,DelayedJob 作业只是一个普通的 ActiveRecord 模型,因此如果您使用 Ruby 代码,您可以对它们执行您喜欢的操作:

Delayed::Job.destroy_all
Delayed::Job.delete_all
Delayed::Job.find(4).destroy
# etc.

回答by betamatt

Sounds like you've got a parent process that wants to timeout if its set of jobs doesn't complete within a certain time. Instead of hanging on to references to the jobs themselves, set a flag on a model that indicates that the process has given up. Jobs can check for that flag and short circuit if they're not needed anymore. (Your Job class should also wrap the contents of its #perform method in a timeout.)

听起来您有一个父进程想要​​超时,如果其作业集未在特定时间内完成。与其坚持对作业本身的引用,不如在模型上设置一个标志,表明该过程已放弃。如果不再需要,乔布斯可以检查该标志并短路。(您的 Job 类还应该在超时中包装其 #perform 方法的内容。)

It's almost always a bad idea to try to hang on to references to DJ objects as you seem to be suggesting.

像您似乎建议的那样尝试保留对 DJ 对象的引用几乎总是一个坏主意。