Nov 19
Scheduling Crons with DelayedJob
I didn't want to run & monitor a separate system to manage my periodic tasks, so I wrote a few lines of code to reschedule jobs every so often. It goes something like this.
class DjCron
def self.step(action, period, change = {})
next_at = Time.now.change(change) + period
puts "Performing #{action.to_s} and rescheduling at #{next_at.to_s}"
self.reschedule_at(next_at, action, period, change)
end
def self.reschedule_at(send_at, action, period, change = {})
method = Delayed::PerformableMethod.new(self, :step, [action,period, change])
Delayed::Job.enqueue(method, 0, send_at)
end
end
Once a single task is scheduled, it will perform the job and reschedule itself in period time. To keep it robust, rather than performing the whole job instead of the puts statement, I suggest scheduling another job. That will keep it simple and avoid any headaches with retries.

