jmettraux/rufus-scheduler

Time zone not passed to worker threads

KaneMorgan opened this issue · 2 comments

I've noticed something that was surprising to me. It seems as though setting the Timezone for the scheduler is not persisted to the child threads.

This should illustrate the issue

require 'rufus-scheduler'
require 'active_support/core_ext/time/zones'

Time.zone = 'Asia/Shanghai'
Rufus::Scheduler.new.in "2s" do
  puts "Timezone is: #{Time.zone} EtOrbi zone: #{EtOrbi.zone}"
end

The output of this is Timezone is: EtOrbi zone: Europe - Belfast

In the parent the timezone is Asia/Shanghai

irb(main):009:0> EtOrbi.zone
=> #<TZInfo::DataTimezone: Asia/Shanghai>

I'd expected for the thread to have the same timezone as the scheduler.

For now I'm explicitly setting the timezone in the do block, but just wondered whether the above was intended

We are on rufus-scheduler 3.8.2

Hello,

this is not really intended. The scheduler is intended to trigger at the right time, not necessarily with the right timezone.

I refrained looking too much into the issue as the core Ruby was moving towards support for explicit zones, and haven't looked much since.

I am closing the issue but not the conversation.

Kind regards.

Thanks @jmettraux for responding so quickly.

I've done some more digging and I think the source of my confusion is actually in active_support.

It looks like Time.zone= actually only sets the TimeZone for the current thread
https://github.com/rails/rails/blob/v6.1.7.3/activesupport/lib/active_support/core_ext/time/zones.rb#L41

This is probably a good idea in the context of Rails in general but wasn't obvious to me,

Given the above I don't think it's the responsibility of this gem to maintain the timezone, but it may be worth a note in the Timezones section of the readme

Thank you for your help