The Application
I am writing an application that runs the scheduler and fetches the records after someone saves an entry into the database.
initializers/scheduler.rb
require 'rufus-scheduler'
SCHEDULER = Rufus::Scheduler.new(:lockfile => ".rufus-scheduler.lock")
unless SCHEDULER.down?
# SCHEDULER.every '3m', :tags => 121 do
# Rails.logger.info "Ping!!"
# Rails.logger.flush
# end
end
controllers/abc_controller.rb
class AbcController < ApplicationController
def new
obj = ModelClass.new(params)
if obj.save
SCHEDULER.every obj.frequency do
# run a job
end
end
end
end
Problem
The job is started from the controller, but the problem is when a server is restarted the job is lost.
I have read the rufus-scheduler's readme file and it is mentioned that it is a non-feature. But still I want to know if there is any solution to this (persisting the jobs after a server reboot) or do I have to change my approach?
P.S. I searched for it on stack overflow but I am sorry if I missed anything or missed a similar question.
EDIT
For all those who want to run the scheduler from the controller itself using params, think of a strategy to save the data in the db(temp table or something) if possible and then bind the create event with scheduling. Re-schedule the jobs on scheduler initialization, i.e, server reboot as mentioned in the answer by @jmettraux. Hope this helps.
You are right, rufus-scheduler is not tied to any persistence.
Since you're already tying records to schedules (by scheduling a job for each object getting created), why don't you simply reschedule when your application restarts?
Something like (pseudo-code aligned on yours):
require 'rufus-scheduler'
SCHEDULER = Rufus::Scheduler.new(:lockfile => ".rufus-scheduler.lock")
unless SCHEDULER.down?
ModelClass.all.each do |obj|
SCHEDULER.every obj.frequency do
# the job
end
end
end