Search code examples
ruby-on-railsrubyrufus-scheduler

stop rufus-scheduler job from ruby script


how can i stop rufus-scheduler from my script :

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

I just want to test in first step if the job is running if true stop the job. Thanks


Solution

  • Here is a standalone script that shows how to do it:

    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
    

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

    If you can't keep the job instance around but can keep a string, you could do:

    job_id = scheduler.every '3m' do
      # processing...
    end
    
    # later on...
    
    job = scheduler.job(job_id)
    

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