Skip to content

Commit 9a83d6e

Browse files
authored
Fix handling of cron with tz in Cron::Job (#2530)
* Fix handling of cron with tz in Cron::Job * Update CHANGELOG
1 parent 2b2e23e commit 9a83d6e

File tree

8 files changed

+46
-12
lines changed

8 files changed

+46
-12
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## Unreleased
2+
3+
### Bug Fixes
4+
5+
- Fix handling of cron with tz in Cron::Job ([#2530](https://github.com/getsentry/sentry-ruby/pull/2530))
6+
17
## 5.22.3
28

39
### Bug Fixes

sentry-sidekiq/lib/sentry-sidekiq.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@ class Railtie < ::Rails::Railtie
4343
end
4444

4545
# patches
46+
require "sentry/sidekiq/cron/helpers"
4647
require "sentry/sidekiq/cron/job"
4748
require "sentry/sidekiq-scheduler/scheduler"

sentry-sidekiq/lib/sentry/sidekiq-scheduler/scheduler.rb

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,7 @@ def new_job(name, interval_type, config, schedule, options)
3838
monitor_config =
3939
case interval_type
4040
when "cron"
41-
# Split schedule into cron and timezone parts (if timezone exists)
42-
cron_parts = schedule.strip.split(" ")
43-
44-
if cron_parts.length > 5
45-
timezone = cron_parts.pop
46-
cron_without_timezone = cron_parts.join(" ")
47-
48-
Sentry::Cron::MonitorConfig.from_crontab(cron_without_timezone, timezone: timezone)
49-
else
50-
Sentry::Cron::MonitorConfig.from_crontab(schedule)
51-
end
41+
Sentry::Sidekiq::Cron::Helpers.monitor_config(schedule)
5242
when "every", "interval"
5343
Sentry::Cron::MonitorConfig.from_interval(rufus_job.frequency.to_i / 60, :minute)
5444
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
module Sentry
4+
module Sidekiq
5+
module Cron
6+
module Helpers
7+
# This is used by Cron::Job and Scheduler
8+
def self.monitor_config(cron)
9+
cron_parts = cron.strip.split(" ")
10+
11+
if cron_parts.length > 5
12+
timezone = cron_parts.pop
13+
cron_without_timezone = cron_parts.join(" ")
14+
15+
Sentry::Cron::MonitorConfig.from_crontab(cron_without_timezone, timezone: timezone)
16+
else
17+
Sentry::Cron::MonitorConfig.from_crontab(cron)
18+
end
19+
end
20+
end
21+
end
22+
end
23+
end

sentry-sidekiq/lib/sentry/sidekiq/cron/job.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def save
5555
klass_const.send(:include, Sentry::Cron::MonitorCheckIns)
5656
klass_const.send(:sentry_monitor_check_ins,
5757
slug: name.to_s,
58-
monitor_config: Sentry::Cron::MonitorConfig.from_crontab(parsed_cron.original))
58+
monitor_config: Sentry::Sidekiq::Cron::Helpers.monitor_config(parsed_cron.original))
5959
end
6060

6161
true

sentry-sidekiq/spec/fixtures/sidekiq-cron-schedule.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ happy:
22
cron: "* * * * *"
33
class: "HappyWorkerForCron"
44

5+
happy_with_timezone:
6+
cron: "* * * * * America/Los_Angeles"
7+
class: "HappyWorkerForCronWithTimezone"
8+
59
manual:
610
cron: "* * * * *"
711
class: "SadWorkerWithCron"

sentry-sidekiq/spec/sentry/sidekiq/cron/job_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@
6464
expect(HappyWorkerForCron.sentry_monitor_config.schedule.value).to eq('* * * * *')
6565
end
6666

67+
it 'patches HappyWorkerForCronWithTimezone' do
68+
expect(HappyWorkerForCronWithTimezone.ancestors).to include(Sentry::Cron::MonitorCheckIns)
69+
expect(HappyWorkerForCronWithTimezone.sentry_monitor_slug).to eq('happy_with_timezone')
70+
expect(HappyWorkerForCronWithTimezone.sentry_monitor_config).to be_a(Sentry::Cron::MonitorConfig)
71+
expect(HappyWorkerForCronWithTimezone.sentry_monitor_config.timezone).to eq('America/Los_Angeles')
72+
expect(HappyWorkerForCronWithTimezone.sentry_monitor_config.schedule).to be_a(Sentry::Cron::MonitorSchedule::Crontab)
73+
expect(HappyWorkerForCronWithTimezone.sentry_monitor_config.schedule.value).to eq('* * * * *')
74+
end
75+
6776
it 'patches HappyWorkerWithHumanReadableCron' do
6877
expect(HappyWorkerWithHumanReadableCron.ancestors).to include(Sentry::Cron::MonitorCheckIns)
6978
expect(HappyWorkerWithHumanReadableCron.sentry_monitor_slug).to eq('human_readable_cron')

sentry-sidekiq/spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ def perform
143143
end
144144

145145
class HappyWorkerForCron < HappyWorker; end
146+
class HappyWorkerForCronWithTimezone < HappyWorker; end
146147
class HappyWorkerForScheduler < HappyWorker; end
147148
class HappyWorkerForSchedulerWithTimezone < HappyWorker; end
148149
class EveryHappyWorker < HappyWorker; end

0 commit comments

Comments
 (0)