I am trying to configure rufus-scheduler for a distributed set of hosts for my Rails application. For ensuring that only one host picks up a job at a time (while the server process runs on all the hosts), I have a DatabaseSemaphore model, with the table structure as:
create_table :database_semaphores do |t|
t.string :name
t.datetime :locked_at
t.datetime :unlocked_at
t.datetime :completed_at
t.integer :lock_duration, :default => 30
t.timestamps
end
I have DatabaseSemaphore.open? and DatabaseSemaphore.close? methods that set the attribute values for an instance specific to each job for acquiring and releasing lock, respectively. I call DatabaseSemaphore.open? inside the overridden on_pre_trigger() and DatabaseSemaphore.close? inside the on_post_trigger() methods, respectively.
Here, lock_duration has a default value of 30 seconds. However, it should be alterable, for each job. So, what I intend is to be able to pass a value for it inside the scheduler block and set it as the value for 'lock_duration' attribute for that job instance inside on_pre_trigger().
Something like:
scheduler.in '1m', tag: 'hello' do |job|
job.params = {lock_duration: 20}
puts "Hello World!"
end
Is there a functionality to do that?
Let's try to iterate towards what you really need.
For that, I want to set some entries in database for each job before the job is executed
How about simply doing
require 'rufus-scheduler'
scheduler = Rufus::Scheduler.new
scheduler.every '1h' do
# set entries in db
2.times { DB[:entries].insert(tstamp: Time.now) }
# do the job
p 1 + 1
end
?
It's not exactly "before the job gets executed", it's more "at the beginning of the job execution".
UPDATE
Would be great if rufus-scheduler's 'job' structure could allow an additional hash as a parameter, perhaps
Maybe this example could help:
require 'rufus-scheduler'
s = Rufus::Scheduler.new
s.every '5s', msg: 'hello world' do |job|
p job.opts
end
s.join
It prints "{:msg => 'hello world'}" every five seconds.
It's documented in the readme under opts.