Search code examples
ruby-on-railsdelayed-jobrufus-scheduler

Why is Rufus scheduling the first job twice?


I have a Rails app that uses Rufus Scheduler combined with Delayed jobs to execute background jobs. There are another jobs, but the one I'm having trouble with is scheduled in a controller using this code:

  def create
    @harvest_plan = HarvestPlan.new(resource_params)
    @harvest_plan.start_date = Time.parse(resource_params[:start_date])
    if @harvest_plan.save
      ApplicationController.new.insert_in_messages_list(session, :success, 'Harvest plan created')
      schedule_harvest
      redirect_to farms_path
    end
  end

  private

  def schedule_harvest
    Rufus::Scheduler.singleton.every "#{@harvest_plan.hours_between}h",
      :times => @harvest_plan.repetitions, :first_at => @harvest_plan.start_date do
      CreateHarvestFromPlanJob.perform_later
    end
  end

The job is supposed to be scheduled according to the harvest plan model, which indicates how many hours must past between jobs, when is the first one supposed to be scheduled and how many repetitions must occur. Everything works perfect except for the first job, which does happen at the time specified with first_at but it is scheduled twice for some reason, delayed jobs then executes the job twice. I tried using the mutex, blocking and overlap options, but it did nothing different. After the first job (scheduled twice) everything works fine. The next jobs are scheduled on time and just once. I have just one delayed jobs worker

Why is this happening?

I am running Rails 4.2.4, Ruby 2.2.2 and Rufus 3.3.2. Since the error happens both with passenger and webrick I assume this has nothing to do with the problem.


Solution

  • Why is Rufus scheduling the first job twice?

    because of a bug you found: https://github.com/jmettraux/rufus-scheduler/issues/231

    Thanks a lot!