从ruby脚本停止rufus-scheduler作业

时间:2014-03-07 14:45:34

标签: ruby-on-rails ruby rufus-scheduler

如何从我的脚本中停止rufus-scheduler:

@scheduler = Rufus::Scheduler.new
job = @scheduler.schedule_every '3m' do |job|
  # processing
end
job.call

我只想在第一步测试如果作业正在运行,如果真的停止了工作。 感谢

1 个答案:

答案 0 :(得分:2)

这是一个独立的脚本,显示了如何执行此操作:

require 'rufus-scheduler'

scheduler = Rufus::Scheduler.new

job = scheduler.schedule_every '1s' do
  # processing...
  sleep 3600
end

# later on...

sleep 2

p job.running?   # true

job.kill if job.running?

p job.running?   # false

文档:https://github.com/jmettraux/rufus-scheduler/#job-methods

如果您无法保留作业实例但可以保留字符串,则可以执行以下操作:

job_id = scheduler.every '3m' do
  # processing...
end

# later on...

job = scheduler.job(job_id)

文档:https://github.com/jmettraux/rufus-scheduler/#schedulerjobjob_id