有没有办法将额外的参数传递给 rufus-scheduler call() 或 on_pre_trigger() 方法?

Is there a way to pass extra parameters to rufus-scheduler call() or on_pre_trigger() methods?

我正在尝试为我的 Rails 应用程序的一组分布式主机配置 rufus-scheduler。为了确保一次只有一台主机选择一个作业(当服务器进程在所有主机上运行时),我有一个 DatabaseSemaphore 模型,其中 table 结构为:

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

我有 DatabaseSemaphore.open?DatabaseSemaphore.close? 方法来设置实例特定的属性值每个作业分别获取和释放锁。我在覆盖的 on_pre_trigger()DatabaseSemaphore.close? 分别在 on_post_trigger() 方法中。

这里,lock_duration默认值为30秒。但是,对于每项工作,它应该是可变的。因此,我打算能够在调度程序块内为其传递一个值,并将其设置为 on_pre_trigger().

内该作业实例的 'lock_duration' 属性的值

类似于:

scheduler.in '1m', tag: 'hello' do |job|
    job.params = {lock_duration: 20}
    puts "Hello World!"
end

是否有这样做的功能?

让我们尝试迭代您真正需要的东西。

For that, I want to set some entries in database for each job before the job is executed

简单地做如何

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

?

不完全是 "before the job gets executed",更 "at the beginning of the job execution"。

更新

Would be great if rufus-scheduler's 'job' structure could allow an additional hash as a parameter, perhaps

也许这个例子可以帮助:

require 'rufus-scheduler'

s = Rufus::Scheduler.new

s.every '5s', msg: 'hello world' do |job|
  p job.opts
end

s.join

它每五秒打印一次“{:msg => 'hello world'}”。

它记录在 opts 下的自述文件中。

您还可以查看 handlers and co