diff --git a/README.md b/README.md index 35c05c0..802050c 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,9 @@ Sidetiq.configure do |config| # Clock locking key expiration in ms (default: 1000). config.lock_expire = 100 + + # When `true` uses UTC instead of local times (default: false) + config.utc = false end ``` diff --git a/ext/sidetiq_ext/sidetiq_ext.c b/ext/sidetiq_ext/sidetiq_ext.c index a49e264..4723f25 100644 --- a/ext/sidetiq_ext/sidetiq_ext.c +++ b/ext/sidetiq_ext/sidetiq_ext.c @@ -92,6 +92,6 @@ void Init_sidetiq_ext() msidetiq = rb_define_module("Sidetiq"); esidetiq_error = rb_define_class_under(msidetiq, "Error", rb_eStandardError); csidetiq_clock = rb_define_class_under(msidetiq, "Clock", rb_cObject); - rb_define_method(csidetiq_clock, "gettime", sidetiq_gettime, 0); + rb_define_private_method(csidetiq_clock, "clock_gettime", sidetiq_gettime, 0); } diff --git a/lib/sidetiq/clock.rb b/lib/sidetiq/clock.rb index eb79268..e7e2c63 100644 --- a/lib/sidetiq/clock.rb +++ b/lib/sidetiq/clock.rb @@ -3,13 +3,14 @@ module Sidetiq config.priority = Thread.main.priority config.resolution = 1 config.lock_expire = 1000 + config.utc = false end class Clock include Singleton include MonitorMixin - START_TIME = Time.local(2010, 1, 1) + START_TIME = Sidetiq.config.utc ? Time.utc(2010, 1, 1) : Time.local(2010, 1, 1) attr_reader :schedules @@ -34,6 +35,10 @@ module Sidetiq end end + def gettime + Sidetiq.config.utc ? clock_gettime.utc : clock_gettime + end + private def enqueue(worker, time) diff --git a/lib/sidetiq/views/sidetiq.slim b/lib/sidetiq/views/sidetiq.slim index 90c9b11..e92e2df 100644 --- a/lib/sidetiq/views/sidetiq.slim +++ b/lib/sidetiq/views/sidetiq.slim @@ -15,7 +15,7 @@ header.row = worker.name td= worker.get_sidekiq_options['queue'] td - == relative_time(schedule.next_occurrence) + == relative_time(schedule.next_occurrence(@time)) td a href="#{root_path}sidetiq/#{worker.name}" Details - else diff --git a/lib/sidetiq/views/sidetiq_details.slim b/lib/sidetiq/views/sidetiq_details.slim index 860b4e4..10730b8 100644 --- a/lib/sidetiq/views/sidetiq_details.slim +++ b/lib/sidetiq/views/sidetiq_details.slim @@ -30,7 +30,7 @@ table class="table table-striped table-bordered table-white" style="width: 100%; thead th style="width: 25%" Next 10 runs th style="width: 75%" - - @schedule.next_occurrences(10).each do |time| + - @schedule.next_occurrences(10, @time).each do |time| tr td time= time.getutc diff --git a/lib/sidetiq/web.rb b/lib/sidetiq/web.rb index 814461f..8352e8b 100644 --- a/lib/sidetiq/web.rb +++ b/lib/sidetiq/web.rb @@ -13,14 +13,19 @@ module Sidetiq end app.get "/sidetiq" do - @schedules = Sidetiq::Clock.instance.schedules + clock = Sidetiq::Clock.instance + @schedules = clock.schedules + @time = clock.gettime slim :sidetiq end app.get "/sidetiq/:name" do halt 404 unless (name = params[:name]) - schedules = Sidetiq::Clock.instance.schedules + clock = Sidetiq::Clock.instance + schedules = clock.schedules + + @time = clock.gettime @worker, @schedule = schedules.select do |worker, schedule| worker.name == name diff --git a/test/test_clock.rb b/test/test_clock.rb index d5804e2..fd62ef6 100644 --- a/test/test_clock.rb +++ b/test/test_clock.rb @@ -13,6 +13,13 @@ class TestClock < Sidetiq::TestCase refute_nil clock.gettime.tv_nsec end + def test_gettime_utc + refute clock.gettime.utc? + Sidetiq.config.utc = true + assert clock.gettime.utc? + Sidetiq.config.utc = false + end + class FakeWorker; end def test_enqueues_jobs_by_schedule