Search code examples
rubyjrubyrufus-scheduler

Is it possible to obtain "current_time" in rufus-scheduler


Supposed I scheduled a job with rufus-scheduler like below:

scheduler.cron '* * * * * UTC' do |job|
...
end

I can retrieve some useful info from 'job' variable. For example, next_time shows when the next run is scheduled at. I am wondering if a similar value is available for "current_time". Reason: for various reasons (threads, system load, etc) a job could be executed with a small amount of delay. I want to know the exact time the job is meant to have started. If I retrieve system time by Time.new, that will not be exact.

Is there a way? I tried awesome_print the job variable. It seems the value I am looking for is not available.

jruby-1.7.19 :017 > #<Rufus::Scheduler::CronJob:0x3951b84e @unscheduled_at=nil, @first_at=nil, @opts={}, @last_time=2016-07-28 01:35:00 +0800, @tags=[], @mean_work_time=0.0, @callable=#<Proc:0x2ee23f1d@(irb):14>, @last_at=nil, @count=1, @scheduled_at=2016-07-28 01:34:09 +0800, @handler=#<Proc:0x2ee23f1d@(irb):14>, @paused_at=nil, @local_mutex=#<Mutex:0x79da0f7>, @locals={}, @times=nil, @scheduler=#<Rufus::Scheduler:0x7dc7d255 @mutexes={}, @scheduler_lock=#<Rufus::Scheduler::NullLock:0x49c20af6>, @paused=false, @opts={}, @work_queue=#<Queue:0x625dc24e>, @jobs=#<Rufus::Scheduler::JobArray:0x797fc155 @mutex=#<Mutex:0x501b94b9>, @array=[#<Rufus::Scheduler::CronJob:0x3951b84e ...>]>, @trigger_lock=#<Rufus::Scheduler::NullLock:0x42c126c5>, @started_at=2016-07-28 01:33:36 +0800, @thread_key="rufus_scheduler_2068", @stderr=#<IO:fd 2>, @max_work_threads=28, @frequency=0.3, @thread=#<Thread:0x16d871c0 sleep>>, @cron_line=#<Rufus::Scheduler::CronLine:0x6156f1b0 @seconds=[0], @weekdays=nil, @hours=nil, @timezone="UTC", @days=nil, @minutes=nil, @original="* * * * * UTC", @months=nil, @monthdays=nil>, @last_work_time=0.0, @id="cron_1469640849.047_961656910", @original="* * * * * UTC", @next_time=2016-07-27 17:36:00 +0000>

Solution

  • Added a Job#previous_time

    https://github.com/jmettraux/rufus-scheduler/commit/43f1016859a43ea7f138404c1e5d864048f24959

    scheduler.every('10s') do |job|
      puts "job scheduled for #{job.previous_time} triggered at #{Time.now}"
      puts "next time will be around #{job.next_time}"
      puts "."
    end