Skip to content

Commit

Permalink
Improve sidekiq-cron patch
Browse files Browse the repository at this point in the history
  • Loading branch information
adamzapasnik committed Aug 20, 2024
1 parent 22f2a3a commit c661c57
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions sentry-sidekiq/lib/sentry/sidekiq/cron/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def save
unless klass_const.send(:ancestors).include?(Sentry::Cron::MonitorCheckIns)
klass_const.send(:include, Sentry::Cron::MonitorCheckIns)
klass_const.send(:sentry_monitor_check_ins,
slug: name,
monitor_config: Sentry::Cron::MonitorConfig.from_crontab(cron))
slug: name.to_s,
monitor_config: Sentry::Cron::MonitorConfig.from_crontab(parsed_cron.original))
end

true
Expand Down
4 changes: 4 additions & 0 deletions sentry-sidekiq/spec/fixtures/sidekiq-cron-schedule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ manual:
cron: "* * * * *"
class: "SadWorkerWithCron"

human_readable_cron:
cron: "every 5 minutes"
class: HappyWorkerWithHumanReadableCron

invalid_cron:
cron: "not a crontab"
class: "ReportingWorker"
18 changes: 18 additions & 0 deletions sentry-sidekiq/spec/sentry/sidekiq/cron/job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
before do
schedule_file = 'spec/fixtures/sidekiq-cron-schedule.yml'
schedule = Sidekiq::Cron::Support.load_yaml(ERB.new(IO.read(schedule_file)).result)
schedule = schedule.merge(symbol_name: { cron: '* * * * *', class: HappyWorkerWithSymbolName })
# sidekiq-cron 2.0+ accepts second argument to `load_from_hash!` with options,
# such as {source: 'schedule'}, but sidekiq-cron 1.9.1 (last version to support Ruby 2.6) does not.
# Since we're not using the source option in our code anyway, it's safe to not pass the 2nd arg.
Expand Down Expand Up @@ -48,6 +49,23 @@
expect(HappyWorkerForCron.sentry_monitor_config.schedule.value).to eq('* * * * *')
end

it 'patches HappyWorkerWithHumanReadableCron' do
expect(HappyWorkerWithHumanReadableCron.ancestors).to include(Sentry::Cron::MonitorCheckIns)
expect(HappyWorkerWithHumanReadableCron.sentry_monitor_slug).to eq('human_readable_cron')
expect(HappyWorkerWithHumanReadableCron.sentry_monitor_config).to be_a(Sentry::Cron::MonitorConfig)
expect(HappyWorkerWithHumanReadableCron.sentry_monitor_config.schedule).to be_a(Sentry::Cron::MonitorSchedule::Crontab)
expect(HappyWorkerWithHumanReadableCron.sentry_monitor_config.schedule.value).to eq('*/5 * * * *')
end

it 'patches HappyWorkerWithSymbolName' do
expect(HappyWorkerWithSymbolName.ancestors).to include(Sentry::Cron::MonitorCheckIns)
expect(HappyWorkerWithSymbolName.sentry_monitor_slug).to eq('symbol_name')
expect(HappyWorkerWithSymbolName.sentry_monitor_config).to be_a(Sentry::Cron::MonitorConfig)
expect(HappyWorkerWithSymbolName.sentry_monitor_config.schedule).to be_a(Sentry::Cron::MonitorSchedule::Crontab)
expect(HappyWorkerWithSymbolName.sentry_monitor_config.schedule.value).to eq('* * * * *')
end


it 'does not override SadWorkerWithCron manually set values' do
expect(SadWorkerWithCron.ancestors).to include(Sentry::Cron::MonitorCheckIns)
expect(SadWorkerWithCron.sentry_monitor_slug).to eq('failed_job')
Expand Down
2 changes: 2 additions & 0 deletions sentry-sidekiq/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ class HappyWorkerForCron < HappyWorker; end
class HappyWorkerForScheduler < HappyWorker; end
class HappyWorkerForSchedulerWithTimezone < HappyWorker; end
class EveryHappyWorker < HappyWorker; end
class HappyWorkerWithHumanReadableCron < HappyWorker; end
class HappyWorkerWithSymbolName < HappyWorker; end

class HappyWorkerWithCron < HappyWorker
include Sentry::Cron::MonitorCheckIns
Expand Down

0 comments on commit c661c57

Please sign in to comment.