-
-
Notifications
You must be signed in to change notification settings - Fork 501
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
sidekiq-cron
patch for automatic monitoring of jobs listed in the
schedule * optional patch under `sidekiq_cron` * patch the `Sidekiq::Cron::Job#save` method and auto inject the Sentry::MonitorCheckIns module and turn monitoring on part of #2134
- Loading branch information
1 parent
a418b54
commit 1ff8687
Showing
8 changed files
with
108 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
return unless defined?(::Sidekiq::Cron::Job) | ||
|
||
module Sentry | ||
module Sidekiq | ||
module Cron | ||
module Job | ||
def save | ||
# validation failed, do nothing | ||
return false unless super | ||
|
||
# fail gracefully if can't find class | ||
klass_const = | ||
begin | ||
::Sidekiq::Cron::Support.constantize(klass.to_s) | ||
rescue NameError | ||
return true | ||
end | ||
|
||
# only patch if not explicitly included in job by user | ||
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)) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
Sentry.register_patch(:sidekiq_cron, Sentry::Sidekiq::Cron::Job, ::Sidekiq::Cron::Job) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
happy: | ||
cron: "* * * * *" | ||
class: "HappyWorkerDup" | ||
|
||
manual: | ||
cron: "* * * * *" | ||
class: "SadWorkerWithCron" | ||
|
||
invalid_cron: | ||
cron: "not a crontab" | ||
class: "ReportingWorker" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
require 'spec_helper' | ||
|
||
return unless defined?(Sidekiq::Cron::Job) | ||
|
||
RSpec.describe Sentry::Sidekiq::Cron::Job do | ||
before do | ||
perform_basic_setup { |c| c.enabled_patches += [:sidekiq_cron] } | ||
end | ||
|
||
before do | ||
schedule_file = 'spec/fixtures/schedule.yml' | ||
schedule = Sidekiq::Cron::Support.load_yaml(ERB.new(IO.read(schedule_file)).result) | ||
Sidekiq::Cron::Job.load_from_hash!(schedule, source: 'schedule') | ||
end | ||
|
||
it 'patches class' do | ||
expect(Sidekiq::Cron::Job.ancestors).to include(described_class) | ||
end | ||
|
||
it 'patches HappyWorker' do | ||
expect(HappyWorkerDup.ancestors).to include(Sentry::Cron::MonitorCheckIns) | ||
expect(HappyWorkerDup.sentry_monitor_slug).to eq('happy') | ||
expect(HappyWorkerDup.sentry_monitor_config).to be_a(Sentry::Cron::MonitorConfig) | ||
expect(HappyWorkerDup.sentry_monitor_config.schedule).to be_a(Sentry::Cron::MonitorSchedule::Crontab) | ||
expect(HappyWorkerDup.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') | ||
expect(SadWorkerWithCron.sentry_monitor_config).to be_a(Sentry::Cron::MonitorConfig) | ||
expect(SadWorkerWithCron.sentry_monitor_config.schedule).to be_a(Sentry::Cron::MonitorSchedule::Crontab) | ||
expect(SadWorkerWithCron.sentry_monitor_config.schedule.value).to eq('5 * * * *') | ||
end | ||
|
||
it 'does not patch ReportingWorker because of invalid schedule' do | ||
expect(ReportingWorker.ancestors).not_to include(Sentry::Cron::MonitorSchedule) | ||
end | ||
|
||
it 'does not raise error on invalid class' do | ||
expect do | ||
Sidekiq::Cron::Job.create(name: 'invalid_class', cron: '* * * * *', class: 'UndefinedClass') | ||
end.not_to raise_error | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters