|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Try to require sidekiq-scheduler to make sure it's loaded before the integration. |
| 4 | +begin |
| 5 | + require "sidekiq-scheduler" |
| 6 | +rescue LoadError |
| 7 | + return |
| 8 | +end |
| 9 | + |
| 10 | +# If we've loaded sidekiq-scheduler, but the API changed, |
| 11 | +# and the Scheduler class is not there, fail gracefully. |
| 12 | +return unless defined?(::SidekiqScheduler::Scheduler) |
| 13 | + |
| 14 | +module Sentry |
| 15 | + module SidekiqScheduler |
| 16 | + module Scheduler |
| 17 | + def new_job(name, interval_type, config, schedule, options) |
| 18 | + # Schedule the job upstream first |
| 19 | + # SidekiqScheduler does not validate schedules |
| 20 | + # It will fail with an error if the schedule in the config is invalid. |
| 21 | + # If this errors out, let it fall through. |
| 22 | + rufus_job = super |
| 23 | + |
| 24 | + klass = config.fetch("class") |
| 25 | + return rufus_job unless klass |
| 26 | + |
| 27 | + # Constantize the job class, and fail gracefully if it could not be found |
| 28 | + klass_const = |
| 29 | + begin |
| 30 | + Object.const_get(klass) |
| 31 | + rescue NameError |
| 32 | + return rufus_job |
| 33 | + end |
| 34 | + |
| 35 | + # For cron, every, or interval jobs — grab their schedule. |
| 36 | + # Rufus::Scheduler::EveryJob stores it's frequency in seconds, |
| 37 | + # so we convert it to minutes before passing in to the monitor. |
| 38 | + monitor_config = case interval_type |
| 39 | + when "cron" |
| 40 | + Sentry::Cron::MonitorConfig.from_crontab(schedule) |
| 41 | + when "every", "interval" |
| 42 | + Sentry::Cron::MonitorConfig.from_interval(rufus_job.frequency.to_i / 60, :minute) |
| 43 | + end |
| 44 | + |
| 45 | + # If we couldn't build a monitor config, it's either an error, or |
| 46 | + # it's a one-time job (interval_type is in, or at), in which case |
| 47 | + # we should not make a monitof for it automaticaly. |
| 48 | + return rufus_job if monitor_config.nil? |
| 49 | + |
| 50 | + # only patch if not explicitly included in job by user |
| 51 | + unless klass_const.send(:ancestors).include?(Sentry::Cron::MonitorCheckIns) |
| 52 | + klass_const.send(:include, Sentry::Cron::MonitorCheckIns) |
| 53 | + klass_const.send(:sentry_monitor_check_ins, |
| 54 | + slug: name, |
| 55 | + monitor_config: monitor_config) |
| 56 | + |
| 57 | + ::Sidekiq.logger.info "Injected Sentry Crons monitor checkins into #{klass}" |
| 58 | + end |
| 59 | + |
| 60 | + rufus_job |
| 61 | + end |
| 62 | + end |
| 63 | + end |
| 64 | +end |
| 65 | + |
| 66 | +Sentry.register_patch(:sidekiq_scheduler, Sentry::SidekiqScheduler::Scheduler, ::SidekiqScheduler::Scheduler) |
0 commit comments