Skip to content

Commit 546864f

Browse files
andyjeffriesrosa
authored andcommitted
Automatically use Rails or a default timezone for the schedule
Rather than have SolidQueue act on the server configured time, this change allows you to use either the Rails application's current timezone or a specified default.
1 parent e66e435 commit 546864f

7 files changed

Lines changed: 94 additions & 4 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,9 @@ production:
724724

725725
Tasks are specified as a hash/dictionary, where the key will be the task's key internally. Each task needs to either have a `class`, which will be the job class to enqueue, or a `command`, which will be eval'ed in the context of a job (`SolidQueue::RecurringJob`) that will be enqueued according to its schedule, in the `solid_queue_recurring` queue.
726726

727-
Each task needs to have also a schedule, which is parsed using [Fugit](https://github.com/floraison/fugit), so it accepts anything [that Fugit accepts as a cron](https://github.com/floraison/fugit?tab=readme-ov-file#fugitcron). You can optionally supply the following for each task:
727+
Each task needs to have also a schedule, which is parsed using [Fugit](https://github.com/floraison/fugit), so it accepts anything [that Fugit accepts as a cron](https://github.com/floraison/fugit?tab=readme-ov-file#fugitcron). Schedules can include a time zone (e.g. `0 9 * * * America/New_York` or `every day at 9am America/New_York`). When a schedule doesn't specify one, it's interpreted in the application's configured time zone (`config.time_zone`) by default. You can change or disable this default with `config.solid_queue.time_zone`; setting it to `nil` falls back to the system's local time.
728+
729+
You can optionally supply the following for each task:
728730
- `args`: the arguments to be passed to the job, as a single argument, a hash, or an array of arguments that can also include kwargs as the last element in the array.
729731

730732
The job in the example configuration above will be enqueued every second as:

‎UPGRADING.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# Upgrading to version 1.5.x
2+
Recurring schedules that don't specify a time zone are now interpreted in your application's configured time zone (`config.time_zone`) by default, instead of the system's local time. This only affects schedules without an explicit time zone (e.g. `every day at 9am`); schedules that already include one (e.g. `0 9 * * * America/New_York`) are unaffected.
3+
4+
If your `config.time_zone` differs from the system time where your processes run, recurring jobs may fire at a different wall-clock time than before. To keep the previous behavior, set:
5+
6+
```ruby
7+
config.solid_queue.time_zone = nil
8+
```
9+
110
# Upgrading to version 1.x
211
The value returned for `enqueue_after_transaction_commit?` has changed to `true`, and it's no longer configurable. If you want to change this, you need to use Active Job's configuration options.
312

‎app/models/solid_queue/recurring_task.rb‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ def create_or_update_all(tasks)
5858

5959

6060
def next_time_after(time)
61-
parsed_schedule.next_time(time).utc
61+
parsed_schedule_with_time_zone.next_time(time).utc
6262
end
6363

6464
def next_time
65-
parsed_schedule.next_time.utc
65+
parsed_schedule_with_time_zone.next_time.utc
6666
end
6767

6868
def previous_time
69-
parsed_schedule.previous_time.utc
69+
parsed_schedule_with_time_zone.previous_time.utc
7070
end
7171

7272
def last_enqueued_time
@@ -169,17 +169,34 @@ def arguments_with_kwargs
169169
end
170170
end
171171

172+
def parsed_schedule_with_time_zone
173+
@parsed_schedule_with_time_zone ||= apply_default_time_zone_to(parsed_schedule)
174+
end
172175

173176
def parsed_schedule
174177
@parsed_schedule ||= Fugit.parse(schedule, multi: :fail)
175178
end
176179

180+
def apply_default_time_zone_to(schedule)
181+
if schedule.respond_to?(:zone) && schedule.zone.nil? && default_time_zone.present?
182+
Fugit.parse("#{schedule.to_cron_s} #{default_time_zone}", multi: :fail)
183+
else
184+
schedule
185+
end
186+
rescue ArgumentError
187+
schedule
188+
end
189+
177190
def job_class
178191
@job_class ||= class_name.present? ? class_name.safe_constantize : self.class.default_job_class
179192
end
180193

181194
def enqueue_options
182195
{ queue: queue_name, priority: priority }.compact
183196
end
197+
198+
def default_time_zone
199+
SolidQueue.time_zone
200+
end
184201
end
185202
end

‎lib/solid_queue.rb‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ module SolidQueue
4141
mattr_accessor :clear_finished_jobs_after, default: 1.day
4242
mattr_accessor :default_concurrency_control_period, default: 3.minutes
4343

44+
mattr_reader :time_zone
45+
46+
def time_zone=(zone)
47+
@@time_zone = if zone
48+
resolved = zone.respond_to?(:tzinfo) ? zone : ActiveSupport::TimeZone[zone]
49+
resolved&.tzinfo&.name || zone.to_s
50+
end
51+
end
52+
4453
delegate :on_start, :on_stop, :on_exit, to: Supervisor
4554

4655
def schedule_recurring_task(key, **options)

‎lib/solid_queue/engine.rb‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ class Engine < ::Rails::Engine
1616
end
1717
end
1818

19+
initializer "solid_queue.time_zone" do |app|
20+
unless config.solid_queue.key?(:time_zone)
21+
SolidQueue.time_zone = app.config.time_zone
22+
end
23+
end
24+
1925
initializer "solid_queue.app_executor", before: :run_prepare_callbacks do |app|
2026
config.solid_queue.app_executor ||= app.executor
2127
config.solid_queue.on_thread_error ||= ->(exception) { Rails.error.report(exception, handled: false) }

‎test/models/solid_queue/recurring_task_test.rb‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,50 @@ def perform
272272
end
273273
end
274274

275+
test "schedule without a time zone uses the configured default time zone" do
276+
with_time_zone "America/New_York" do
277+
task = recurring_task_with(class_name: "JobWithoutArguments", schedule: "every day at 9am")
278+
assert_equal Fugit.parse("0 9 * * * America/New_York").next_time.utc, task.next_time
279+
end
280+
end
281+
282+
test "schedule with an explicit time zone ignores the configured default time zone" do
283+
with_time_zone "America/New_York" do
284+
task = recurring_task_with(class_name: "JobWithoutArguments", schedule: "0 9 * * * Europe/Madrid")
285+
assert_equal Fugit.parse("0 9 * * * Europe/Madrid").next_time.utc, task.next_time
286+
end
287+
end
288+
289+
test "schedule falls back to local time when no default time zone is configured" do
290+
with_time_zone nil do
291+
task = recurring_task_with(class_name: "JobWithoutArguments", schedule: "every day at 9am")
292+
assert_equal Fugit.parse("0 9 * * *").next_time.utc, task.next_time
293+
end
294+
end
295+
296+
test "configured default time zone accepts Rails time zone names" do
297+
with_time_zone "Eastern Time (US & Canada)" do
298+
task = recurring_task_with(class_name: "JobWithoutArguments", schedule: "every day at 9am")
299+
assert_equal Fugit.parse("0 9 * * * America/New_York").next_time.utc, task.next_time
300+
end
301+
end
302+
303+
test "default time zone doesn't change the displayed schedule" do
304+
with_time_zone "America/New_York" do
305+
task = recurring_task_with(class_name: "JobWithoutArguments", schedule: "every day at 9am")
306+
assert task.to_s.ends_with? "[ 0 9 * * * ]"
307+
end
308+
end
309+
275310
private
311+
def with_time_zone(zone)
312+
previous = SolidQueue.time_zone
313+
SolidQueue.time_zone = zone
314+
yield
315+
ensure
316+
SolidQueue.time_zone = previous
317+
end
318+
276319
def enqueue_and_assert_performed_with_result(task, result)
277320
assert_difference [ -> { SolidQueue::Job.count }, -> { SolidQueue::ReadyExecution.count } ], +1 do
278321
task.enqueue(at: Time.now)

‎test/solid_queue_test.rb‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ class SolidQueueTest < ActiveSupport::TestCase
44
test "it has a version number" do
55
assert SolidQueue::VERSION
66
end
7+
8+
test "time_zone defaults to the application's configured time zone, normalized to its IANA name" do
9+
assert_equal "Etc/UTC", SolidQueue.time_zone
10+
end
711
end

0 commit comments

Comments
 (0)