Ruby on rails 重新创建作业,如何停止运行作业
我的工人阶级Ruby on rails 重新创建作业,如何停止运行作业,ruby-on-rails,delayed-job,resque,Ruby On Rails,Delayed Job,Resque,我的工人阶级 class WebWorker @queue = :jobs_queue def self.perform(id) //bunch of code here end end 我从队列中删除了这样的某个作业 Resque.dequeue(WebWorker,id) 但我想停止运行作业并重新启动,我该怎么做 按如下方式尝试取消注册\u worker: Resque.workers.each(&:unregister_worker) 此命令将停止工作并将
class WebWorker
@queue = :jobs_queue
def self.perform(id)
//bunch of code here
end
end
我从队列中删除了这样的某个作业Resque.dequeue(WebWorker,id)
但我想停止运行作业并重新启动,我该怎么做 按如下方式尝试
取消注册\u worker
:
Resque.workers.each(&:unregister_worker)
此命令将停止工作并将其设置为失败
如果您想杀死/停止正在进行的作业而不将其标记为失败的作业,您可以使用此黑客
class Task
@queue = :queue_name
def self.perform(parameters)
pid = Process.fork do
Task.work parameters
end
#puts "parent, pid #{Process.pid}, waiting on child pid #{pid}"
Process.wait
end
def self.work(parameters)
#Your perform code here
end
end
现在,在您想要停止此代码的地方,只需联系当前正在执行您想要停止的工作的resque worker。并杀死其子进程的子进程。有点像杀死工人的孙子
说明:
辅助进程派生一个子进程,执行函数在其中运行其代码。如果我们直接杀死这个子进程,那么这个作业将停止,但它将被标记为失败的作业。这就是为什么我们在self.perform中分叉了另一个子进程,现在杀死perform的子进程将停止此正在运行的作业,并且不会将其标记为失败。现在我们可以再次将作业排队。因此,我们的任务是如何接触到我们需要停止的工作的工人
我为基于UNIX的系统编写了以下代码:
Resque.workers.each do |worker|
#If condition : "current worker is working and is working on the job that we want to stop and is operating on the queue that we require " and please change this condition as per your requirement
if worker.working? and worker.queues.include?("queue_name") and worker.job["payload"]["args"].first==post_id
victim = %x[pgrep -P #{worker.pid}] #Getting child's PID
victim.strip!
victim = %x[pgrep -P #{victim}] #Getting grandchild's PID of worker
victim.strip!
%x[kill -9 #{victim.to_i}]
end
end
这不仅仅是停止一个正在运行的作业,而是永久性地停止整个工作进程,没有好的恢复方法(只有重新启动服务器才能将其恢复)。