rubyrufus-scheduler

Rufus-Scheduler Running Only Once


I am using Rufus Scheduler to run another Ruby script every two minutes. The script runs once, but never again. Is there an error in my code? HTOP shows that "launcher.rb" continues to exist as a process after the "script.rb" code runs once.

#launcher.rb

require "rubygems"
require "rufus-scheduler"

scheduler = Rufus::Scheduler.new    

scheduler.every("2m")do
    require "/home/pi/Bots/script.rb"
end

scheduler.join

Solution

  • You could try with

    #launcher.rb
    
    require "rubygems"
    require "rufus-scheduler"
    
    scheduler = Rufus::Scheduler.new
    
    scheduler.every("2m")do
      #require "/home/pi/Bots/script.rb"
      load "/home/pi/Bots/script.rb"
    end
    
    scheduler.join
    

    It may work ("may" because I don't know exactly what's in your script.rb). require will not load and run again whatever behind your "home/pi/Bots/script.rb" while load will load and run each time.

    What happens with your current launcher is that require is called every 2 minutes, but only the first time does it load and run the script.rb.

    Please note that the following might be better:

    # script.rb
    
    module MyBot
      def self.call
        # do the job...
      end
    end
    
    
    # launcher.rb
    
    require 'rubygems'
    require 'rufus-scheduler'
    require '/home/pi/Bots/script.rb'
    
    scheduler = Rufus::Scheduler.new
    
    scheduler.every('2m') do
      MyBot.call
    end
    
    scheduler.join
    

    See Kernel#load and Kernel#require