forked from getsentry/sentry-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Crons: add sidekiq-scheduler zero config support.
- Adds support for `sidekiq-scheduler` instrumentation without any configuration from the user. - Based on getsentry#2170.
- Loading branch information
1 parent
c7c8e5b
commit 0794804
Showing
6 changed files
with
100 additions
and
0 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
41 changes: 41 additions & 0 deletions
41
sentry-sidekiq/lib/sentry/sidekiq/sidekiq-scheduler/scheduler.rb
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,41 @@ | ||
# frozen_string_literal: true | ||
|
||
return unless defined?(::SidekiqScheduler::Scheduler) | ||
|
||
module Sentry | ||
module SidekiqScheduler | ||
module Scheduler | ||
def new_job(name, interval_type, config, schedule, options) | ||
# Schedule the job upstream first | ||
# SidekiqScheduler does not validate schedules | ||
# It will fail with an error if the schedule in the config is invalid. | ||
# If this errors out, let it fall through. | ||
rufus_job = super(name, interval_type, config, schedule, options) | ||
|
||
# Constantize the job class, and fail gracefully if it could not be found | ||
klass_const = | ||
begin | ||
config.fetch("class").constantize | ||
rescue NameError | ||
return rufus_job | ||
end | ||
|
||
monitor_config = Sentry::Cron::MonitorConfig.from_crontab(schedule) | ||
|
||
# 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: monitor_config) | ||
|
||
::Sidekiq.logger.info "Injected Sentry Crons monitor checkins into #{config.fetch("class")}" | ||
end | ||
|
||
return rufus_job | ||
end | ||
end | ||
end | ||
end | ||
|
||
Sentry.register_patch(:sidekiq_scheduler, Sentry::SidekiqScheduler::Scheduler, ::SidekiqScheduler::Scheduler) |
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,9 @@ | ||
enabled: true | ||
dynamic: true | ||
:schedule: | ||
happy: | ||
cron: "* * * * *" | ||
class: "HappyWorkerDup" | ||
manual: | ||
cron: "* * * * *" | ||
class: "SadWorkerWithCron" |
44 changes: 44 additions & 0 deletions
44
sentry-sidekiq/spec/sentry/sidekiq/sidekiq-scheduler/scheduler_spec.rb
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,44 @@ | ||
require 'spec_helper' | ||
|
||
return unless defined?(SidekiqScheduler::Scheduler) | ||
|
||
RSpec.describe Sentry::SidekiqScheduler::Scheduler do | ||
before do | ||
perform_basic_setup { |c| c.enabled_patches += [:sidekiq_scheduler] } | ||
end | ||
|
||
before do | ||
schedule_file = 'spec/fixtures/sidekiq-scheduler-schedule.yml' | ||
sidekiq_config = ::Sidekiq::Config.new({scheduler: YAML.load_file(schedule_file)}) | ||
|
||
# Sidekiq::Scheduler merges it's config with Sidekiq. | ||
# To grab a config for it to start, we need to pass sidekiq configuration | ||
# (defaults should be fine though). | ||
scheduler_config = SidekiqScheduler::Config.new(sidekiq_config: sidekiq_config) | ||
|
||
# Making and starting a Manager instance will load the jobs | ||
schedule_manager = SidekiqScheduler::Manager.new(scheduler_config) | ||
schedule_manager.start | ||
end | ||
|
||
it 'patches class' do | ||
expect(SidekiqScheduler::Scheduler.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 | ||
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