File: scheduler.rb

package info (click to toggle)
ruby-sentry-sidekiq 5.18.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 152 kB
  • sloc: ruby: 324; makefile: 10; sh: 4
file content (80 lines) | stat: -rw-r--r-- 3,093 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# frozen_string_literal: true

# Try to require sidekiq-scheduler to make sure it's loaded before the integration.
begin
  require "sidekiq-scheduler"
rescue LoadError
  return
end

# If we've loaded sidekiq-scheduler, but the API changed,
# and the Scheduler class is not there, fail gracefully.
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

        klass = config.fetch("class")
        return rufus_job unless klass

        # Constantize the job class, and fail gracefully if it could not be found
        klass_const =
          begin
            Object.const_get(klass)
          rescue NameError
            return rufus_job
          end

        # For cron, every, or interval jobs — grab their schedule.
        # Rufus::Scheduler::EveryJob stores it's frequency in seconds,
        # so we convert it to minutes before passing in to the monitor.
        monitor_config =
          case interval_type
          when "cron"
            # fugit is a second order dependency of sidekiq-scheduler via rufus-scheduler
            parsed_cron = ::Fugit.parse_cron(schedule)
            timezone = parsed_cron.timezone

            # fugit supports having the timezone part of the cron string,
            # so we need to pull that with some hacky stuff
            if timezone
              parsed_cron.instance_variable_set(:@timezone, nil)
              cron_without_timezone = parsed_cron.to_cron_s
              Sentry::Cron::MonitorConfig.from_crontab(cron_without_timezone, timezone: timezone.name)
            else
              Sentry::Cron::MonitorConfig.from_crontab(schedule)
            end
          when "every", "interval"
            Sentry::Cron::MonitorConfig.from_interval(rufus_job.frequency.to_i / 60, :minute)
          end

        # If we couldn't build a monitor config, it's either an error, or
        # it's a one-time job (interval_type is in, or at), in which case
        # we should not make a monitof for it automaticaly.
        return rufus_job if monitor_config.nil?

        # 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)
          slug = klass_const.send(:sentry_monitor_slug, name: name)
          klass_const.send(:sentry_monitor_check_ins,
                           slug: slug,
                           monitor_config: monitor_config)

          ::Sidekiq.logger.info "Injected Sentry Crons monitor checkins into #{klass}"
        end

        rufus_job
      end
    end
  end
end

Sentry.register_patch(:sidekiq_scheduler, Sentry::SidekiqScheduler::Scheduler, ::SidekiqScheduler::Scheduler)