From bc3de4269c2983e8d5f5871141de7dcc6d249b21 Mon Sep 17 00:00:00 2001 From: Natik Gadzhi Date: Thu, 23 Nov 2023 04:31:30 -0800 Subject: [PATCH] (fix) crons: require sidekiq-cron early (#2173) --- sentry-sidekiq/lib/sentry/sidekiq/cron/job.rb | 8 +++++++- sentry-sidekiq/spec/sentry/sidekiq/cron/job_spec.rb | 5 ++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/sentry-sidekiq/lib/sentry/sidekiq/cron/job.rb b/sentry-sidekiq/lib/sentry/sidekiq/cron/job.rb index 6e0abaeed..87a108899 100644 --- a/sentry-sidekiq/lib/sentry/sidekiq/cron/job.rb +++ b/sentry-sidekiq/lib/sentry/sidekiq/cron/job.rb @@ -1,6 +1,12 @@ # frozen_string_literal: true -return unless defined?(::Sidekiq::Cron::Job) +# Try requiring sidekiq-cron to ensure it's loaded before the integration. +# If sidekiq-cron is not available, do nothing. +begin + require "sidekiq-cron" +rescue LoadError + return +end module Sentry module Sidekiq diff --git a/sentry-sidekiq/spec/sentry/sidekiq/cron/job_spec.rb b/sentry-sidekiq/spec/sentry/sidekiq/cron/job_spec.rb index 5da97a692..2496cb3b8 100644 --- a/sentry-sidekiq/spec/sentry/sidekiq/cron/job_spec.rb +++ b/sentry-sidekiq/spec/sentry/sidekiq/cron/job_spec.rb @@ -10,7 +10,10 @@ 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') + # 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. + Sidekiq::Cron::Job.load_from_hash!(schedule) end it 'patches class' do