diff options
author | Andrew White <andrew.white@unboxed.co> | 2018-02-22 14:14:42 +0000 |
---|---|---|
committer | Andrew White <andrew.white@unboxed.co> | 2018-02-22 14:14:42 +0000 |
commit | a9d1167b1fdae6f5f5496738b3e7d1e05949dcd0 (patch) | |
tree | 8a46f129aaed18c7c48b35aa0e48f9a19c1bc219 /activejob/lib | |
parent | 9c0c90979a759a41628e0cd9d73821b0b34d03fc (diff) | |
download | rails-a9d1167b1fdae6f5f5496738b3e7d1e05949dcd0.tar.gz rails-a9d1167b1fdae6f5f5496738b3e7d1e05949dcd0.tar.bz2 rails-a9d1167b1fdae6f5f5496738b3e7d1e05949dcd0.zip |
Add support for timezones to Active Job
Record what was the current timezone in effect when the job was
enqueued and then restore when the job is executed in same way
that the current locale is recorded and restored.
Diffstat (limited to 'activejob/lib')
-rw-r--r-- | activejob/lib/active_job/base.rb | 2 | ||||
-rw-r--r-- | activejob/lib/active_job/core.rb | 7 | ||||
-rw-r--r-- | activejob/lib/active_job/timezones.rb | 13 |
3 files changed, 21 insertions, 1 deletions
diff --git a/activejob/lib/active_job/base.rb b/activejob/lib/active_job/base.rb index 6194f89956..2b2a59e969 100644 --- a/activejob/lib/active_job/base.rb +++ b/activejob/lib/active_job/base.rb @@ -9,6 +9,7 @@ require "active_job/execution" require "active_job/callbacks" require "active_job/exceptions" require "active_job/logging" +require "active_job/timezones" require "active_job/translation" module ActiveJob #:nodoc: @@ -68,6 +69,7 @@ module ActiveJob #:nodoc: include Callbacks include Exceptions include Logging + include Timezones include Translation ActiveSupport.run_load_hooks(:active_job, self) diff --git a/activejob/lib/active_job/core.rb b/activejob/lib/active_job/core.rb index 879746fc01..da841ae45b 100644 --- a/activejob/lib/active_job/core.rb +++ b/activejob/lib/active_job/core.rb @@ -31,6 +31,9 @@ module ActiveJob # I18n.locale to be used during the job. attr_accessor :locale + + # Timezone to be used during the job. + attr_accessor :timezone end # These methods will be included into any Active Job object, adding @@ -87,7 +90,8 @@ module ActiveJob "priority" => priority, "arguments" => serialize_arguments(arguments), "executions" => executions, - "locale" => I18n.locale.to_s + "locale" => I18n.locale.to_s, + "timezone" => Time.zone.try(:name) } end @@ -125,6 +129,7 @@ module ActiveJob self.serialized_arguments = job_data["arguments"] self.executions = job_data["executions"] self.locale = job_data["locale"] || I18n.locale.to_s + self.timezone = job_data["timezone"] || Time.zone.try(:name) end private diff --git a/activejob/lib/active_job/timezones.rb b/activejob/lib/active_job/timezones.rb new file mode 100644 index 0000000000..83f459aea5 --- /dev/null +++ b/activejob/lib/active_job/timezones.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module ActiveJob + module Timezones #:nodoc: + extend ActiveSupport::Concern + + included do + around_perform do |job, block, _| + Time.use_zone(job.timezone, &block) + end + end + end +end |