diff --git a/sentry-rails/lib/sentry/rails.rb b/sentry-rails/lib/sentry/rails.rb index 462a035fe..55f625fdc 100644 --- a/sentry-rails/lib/sentry/rails.rb +++ b/sentry-rails/lib/sentry/rails.rb @@ -9,6 +9,7 @@ require "sentry/rails/structured_logging" require "sentry/rails/engine" require "sentry/rails/railtie" +require "sentry/rails/good_job_cron" require "sentry/rails/solid_queue_recurring" module Sentry diff --git a/sentry-rails/lib/sentry/rails/configuration.rb b/sentry-rails/lib/sentry/rails/configuration.rb index ecf899352..9eaab51cd 100644 --- a/sentry-rails/lib/sentry/rails/configuration.rb +++ b/sentry-rails/lib/sentry/rails/configuration.rb @@ -183,6 +183,11 @@ class Configuration # Defaults to true. Set to false to opt out. attr_accessor :active_job_propagate_traces + # GoodJob cron keys to skip when the :good_job_cron patch is enabled. + # Strings and Symbols match the key exactly; Regexps are matched against it. + # @return [Array] + attr_accessor :good_job_exclude_cron_jobs + # Solid Queue recurring task keys to skip when the :solid_queue_recurring patch is enabled. # Strings match the key exactly; Regexps are matched against it. # @return [Array] @@ -210,6 +215,7 @@ def initialize @active_support_logger_subscription_items = Sentry::Rails::ACTIVE_SUPPORT_LOGGER_SUBSCRIPTION_ITEMS_DEFAULT.dup @active_job_report_on_retry_error = false @active_job_propagate_traces = true + @good_job_exclude_cron_jobs = [] @solid_queue_exclude_recurring_tasks = [] @structured_logging = StructuredLoggingConfiguration.new end diff --git a/sentry-rails/lib/sentry/rails/good_job_cron.rb b/sentry-rails/lib/sentry/rails/good_job_cron.rb new file mode 100644 index 000000000..25f729ac5 --- /dev/null +++ b/sentry-rails/lib/sentry/rails/good_job_cron.rb @@ -0,0 +1,152 @@ +# frozen_string_literal: true + +module Sentry + module Rails + # Sends Crons check-ins for GoodJob cron entries. + # Enable with `config.enabled_patches << :good_job_cron`. + module GoodJobCron + EVENT_STATE = :sentry_good_job_cron + MINIMUM_GOOD_JOB_VERSION = Gem::Version.new("4.0") + + class << self + def excluded?(key) + return false unless Sentry.initialized? + + key = key.to_s + Array(Sentry.configuration.rails.good_job_exclude_cron_jobs).any? do |pattern| + (pattern.is_a?(Symbol) ? pattern.to_s : pattern) === key + end + rescue StandardError => e + Sentry.sdk_logger.error(Sentry::LOGGER_PROGNAME) { "Could not check good_job_exclude_cron_jobs for #{key}: #{e.class}: #{e.message}" } + false + end + + # The persisted record identifies the cron run, including retries and callable classes. + def monitor_for(job) + key = job.cron_key + return unless key && Sentry.initialized? + return if excluded?(key) + + entry = ::GoodJob::CronEntry.find(key) + job_class = job.job_class.safe_constantize + return if job_class && job_class < Sentry::Cron::MonitorCheckIns::Patch + + slug = Sentry::Cron::Slug.from(entry.key) + return unless slug + + config = schedule_for(entry) + unless config + Sentry.sdk_logger.debug(Sentry::LOGGER_PROGNAME) { "Not monitoring GoodJob cron entry #{entry.key}: its schedule can't be expressed as a Crons schedule." } + return + end + return if slug_shared?(entry.key, slug) + + [slug, config] + rescue StandardError => e + Sentry.sdk_logger.error(Sentry::LOGGER_PROGNAME) { "Not monitoring GoodJob cron entry #{key}: #{e.class}: #{e.message}" } + nil + end + + # Evented subscribers receive start before execution and finish afterwards. + # State belongs to the notification payload, so nested and concurrent runs are independent. + def start(_name, _id, payload) + return unless Sentry.initialized? && Sentry.configuration.enabled_patches.include?(:good_job_cron) + + monitor = monitor_for(payload[:job]) + return unless monitor + + slug, schedule = monitor + cron_config = Sentry.configuration.cron + config = Sentry::Cron::MonitorConfig.from_crontab( + schedule.schedule.value, + timezone: schedule.timezone, + checkin_margin: cron_config.default_checkin_margin, + max_runtime: cron_config.default_max_runtime + ) + check_in_id = Sentry.capture_check_in(slug, :in_progress, monitor_config: config) + payload[EVENT_STATE] = { slug: slug, check_in_id: check_in_id, started_at: monotonic_now } + rescue StandardError => e + log_error(e) + end + + def finish(_name, _id, payload) + state = payload.delete(EVENT_STATE) + return unless state && Sentry.initialized? + + error = payload[:handled_error] || payload[:unhandled_error] || payload[:error] || payload[:exception] + Sentry.capture_check_in( + state[:slug], error ? :error : :ok, + check_in_id: state[:check_in_id], duration: monotonic_now - state[:started_at] + ) + rescue StandardError => e + log_error(e) + end + + def patch + return if @subscriber + + @subscriber = ActiveSupport::Notifications.subscribe("perform_job.good_job", self) + end + + private + + def log_error(error) + Sentry.sdk_logger.error(Sentry::LOGGER_PROGNAME) { "Could not send GoodJob cron check-in: #{error.class}: #{error.message}" } + end + + def monotonic_now + Process.clock_gettime(Process::CLOCK_MONOTONIC) + end + + # Proc schedules can change between runs. Match the scheduler's local-zone + # resolution: EtOrbi prefers ENV['TZ'], then the Rails app's default zone. + # A request's temporary Time.use_zone must not change the monitor's zone. + def schedule_for(entry) + cron = entry.params[:cron] + return if cron.respond_to?(:call) + + Time.use_zone(Time.zone_default) { Sentry::Cron::FugitSchedule.from_cron(::Fugit.parse(cron)) } + end + + # Different keys can share a slug, like long keys cut at the same length. + # One monitor can't follow two schedules, so none of them are monitored. + def slug_shared?(key, slug) + colliding = ::GoodJob::CronEntry.all.select do |other| + other.key.to_s != key.to_s && Sentry::Cron::Slug.from(other.key) == slug && monitored?(other) + end.map(&:key) + return false if colliding.empty? + + @slug_warnings ||= Concurrent::Map.new + if @slug_warnings.put_if_absent(key.to_s, true).nil? + Sentry.sdk_logger.warn(Sentry::LOGGER_PROGNAME) do + "Not monitoring GoodJob cron entry #{key}: its monitor slug #{slug} is also used by #{colliding.join(", ")}. Rename or exclude one of them." + end + end + true + end + + def monitored?(entry) + return false if excluded?(entry.key) || !schedule_for(entry) + + job_class = entry.job_class + job_class = job_class.safe_constantize if job_class.is_a?(String) + !(job_class.is_a?(Class) && job_class < Sentry::Cron::MonitorCheckIns::Patch) + end + end + end + end +end + +Sentry.register_patch(:good_job_cron) do |config| + if defined?(::GoodJob) && defined?(::Rails.application) && ::Rails.application + if Gem::Version.new(::GoodJob::VERSION) < Sentry::Rails::GoodJobCron::MINIMUM_GOOD_JOB_VERSION + config.sdk_logger.warn(Sentry::LOGGER_PROGNAME) do + "The GoodJob cron integration requires good_job #{Sentry::Rails::GoodJobCron::MINIMUM_GOOD_JOB_VERSION} or later, but #{::GoodJob::VERSION} is installed." + end + else + Sentry::Rails::GoodJobCron.patch + end + else + config.sdk_logger.warn(Sentry::LOGGER_PROGNAME) { "You tried to enable the GoodJob cron integration but the `good_job` gem was not detected." } + end +end diff --git a/sentry-rails/spec/active_job/good_job_cron_spec.rb b/sentry-rails/spec/active_job/good_job_cron_spec.rb new file mode 100644 index 000000000..746e4729b --- /dev/null +++ b/sentry-rails/spec/active_job/good_job_cron_spec.rb @@ -0,0 +1,542 @@ +# frozen_string_literal: true + +require "spec_helper" + +begin + require "good_job" +rescue LoadError +end + +if defined?(::GoodJob) + RSpec.describe Sentry::Rails::GoodJobCron, type: :job do + before(:all) do + skip "GoodJob cron specs run the real GoodJob adapter, which ActiveJob::TestHelper supports from Rails 7.1" if RAILS_VERSION < 7.1 + end + + include_context "active_job backend harness", adapter: :good_job + include_context "good_job database" + + let(:excluded_jobs) { [] } + let(:cron) { {} } + let(:configure_sentry) do + proc do |config| + config.enabled_patches << :good_job_cron + config.rails.good_job_exclude_cron_jobs = excluded_jobs + end + end + + def queue_adapter_for_test + @queue_adapter_for_test ||= ::ActiveJob::QueueAdapters::GoodJobAdapter.new(execution_mode: :external) + end + + def boot_adapter(_adapter) + Sentry::Rails::Test::GoodJobDatabase.connect + end + + def reset_adapter(_adapter) + GoodJob::Execution.delete_all + GoodJob::Job.delete_all + end + + def drain + lock_id = SecureRandom.uuid + loop do + result = GoodJob::Job.perform_with_advisory_lock(lock_id: lock_id) + break if result.nil? + raise result.unhandled_error if result.unhandled_error + end + end + + around do |example| + configuration = GoodJob.configuration + GoodJob.configuration = GoodJob::Configuration.new({ cron: cron }) + example.run + ensure + GoodJob.configuration = configuration + end + + def run_entry(key, **params) + cron[key] = params unless params.empty? + GoodJob::CronEntry.find(key).enqueue(Time.now) + drain + end + + def check_ins + sentry_events.select { |event| event.is_a?(Sentry::CheckInEvent) } + end + + let(:report_job) do + job_fixture("ReportJob") do + def perform(*); end + end + end + + before { report_job } + + it "sends in_progress and ok check-ins with the cron key and schedule" do + run_entry(:nightly_report, class: "ReportJob", cron: "0 9 * * 1-5 Europe/Vienna") + + in_progress, ok = check_ins + expect(check_ins.size).to eq(2) + expect(in_progress.monitor_slug).to eq("nightly_report") + expect(in_progress.status).to eq(:in_progress) + expect(in_progress.monitor_config.to_h).to eq( + schedule: { type: :crontab, value: "0 9 * * 1-5" }, + timezone: "Europe/Vienna" + ) + expect(ok.status).to eq(:ok) + expect(ok.check_in_id).to eq(in_progress.check_in_id) + expect(ok.monitor_config).to be_nil + end + + it "sends an error check-in when the job fails" do + job_fixture("FailingReportJob") do + def perform + raise "boom" + end + end + + expect { run_entry(:failing_report, class: "FailingReportJob", cron: "every hour") }.to raise_error("boom") + expect(check_ins.map(&:status)).to eq([:in_progress, :error]) + expect(check_ins.first.monitor_config.to_h[:schedule]).to eq(type: :crontab, value: "0 * * * *") + expect(check_ins.last.monitor_config).to be_nil + end + + it "checks in again when a failed run is retried" do + job_fixture("FlakyReportJob") do + retry_on RuntimeError, wait: 0, attempts: 2 + + def perform + raise "boom" if executions == 1 + end + end + + run_entry(:flaky_report, class: "FlakyReportJob", cron: "0 9 * * *") + drain + + expect(check_ins.map(&:status)).to eq([:in_progress, :error, :in_progress, :ok]) + expect(check_ins.map(&:monitor_slug).uniq).to eq(["flaky_report"]) + expect(check_ins[2].monitor_config.to_h[:schedule]).to eq(type: :crontab, value: "0 9 * * *") + end + + it "uses each entry's key and schedule when entries share a job class" do + run_entry(:report_eu, class: "ReportJob", args: ["eu"], cron: "0 9 * * * Europe/Berlin") + run_entry(:report_us, class: ReportJob, args: ["us"], cron: "0 9 * * * America/New_York") + + in_progress = check_ins.select { |check_in| check_in.status == :in_progress } + expect(in_progress.map { |check_in| [check_in.monitor_slug, check_in.monitor_config.timezone] }).to eq( + [["report_eu", "Europe/Berlin"], ["report_us", "America/New_York"]] + ) + end + + context "with an app time zone" do + around do |example| + zone_default = Time.zone_default + tz = ENV.delete("TZ") + Time.zone_default = Time.find_zone!("Asia/Tokyo") + example.run + ensure + Time.zone_default = zone_default + ENV["TZ"] = tz + end + + it "uses it for zoneless schedules, whatever zone the enqueue runs in" do + Time.use_zone("America/Chicago") do + run_entry(:tokyo_report, class: "ReportJob", cron: "0 9 * * *") + end + + expect(check_ins.first.monitor_config.timezone).to eq("Asia/Tokyo") + end + end + + context "when the process and Rails time zones differ" do + around do |example| + previous_tz = ENV["TZ"] + previous_zone = Time.zone_default + ENV["TZ"] = "America/Los_Angeles" + Time.zone_default = Time.find_zone!("Asia/Tokyo") + example.run + ensure + ENV["TZ"] = previous_tz + Time.zone_default = previous_zone + end + + it "matches GoodJob's scheduler timezone rather than overriding ENV TZ" do + Time.use_zone("America/Chicago") do + run_entry(:daily_report, class: "ReportJob", cron: "0 9 * * *") + end + + timezone = check_ins.first.monitor_config.timezone + # Scheduler threads have the app's default zone, independent of the request's zone. + scheduled_at = Thread.new { GoodJob::CronEntry.find(:daily_report).next_at }.value + + expect(timezone).to eq("America/Los_Angeles") + expect(scheduled_at.in_time_zone(timezone).hour).to eq(9) + end + + it "keeps an explicit cron timezone ahead of both process and Rails zones" do + run_entry(:daily_report, class: "ReportJob", cron: "0 9 * * * Europe/Vienna") + + timezone = check_ins.first.monitor_config.timezone + scheduled_at = Thread.new { GoodJob::CronEntry.find(:daily_report).next_at }.value + + expect(timezone).to eq("Europe/Vienna") + expect(scheduled_at.in_time_zone(timezone).hour).to eq(9) + end + end + + it "monitors entries whose schedule is an already parsed Fugit cron" do + run_entry(:parsed_report, class: "ReportJob", cron: Fugit.parse("0 9 * * 1-5 Europe/Vienna")) + + expect(check_ins.map(&:status)).to eq([:in_progress, :ok]) + expect(check_ins.first.monitor_config.to_h).to eq( + schedule: { type: :crontab, value: "0 9 * * 1-5" }, + timezone: "Europe/Vienna" + ) + end + + it "skips schedules Crons cannot express" do + run_entry(:every_ten_seconds, class: "ReportJob", cron: "every 10 seconds") + + expect(check_ins).to be_empty + end + + it "skips proc schedules" do + run_entry(:dynamic_report, class: "ReportJob", cron: ->(_last_ran) { "0 9 * * *" }) + + expect(check_ins).to be_empty + end + + it "leaves jobs that include Sentry::Cron::MonitorCheckIns alone" do + job_fixture("MonitoredJob") do + include Sentry::Cron::MonitorCheckIns + sentry_monitor_check_ins slug: "own-slug" + + def perform; end + end + + run_entry(:monitored, class: "MonitoredJob", cron: "0 9 * * *") + + expect(check_ins.map(&:monitor_slug)).to eq(["own-slug", "own-slug"]) + end + + it "monitors jobs that include Sentry::Cron::MonitorCheckIns without configuring it" do + job_fixture("UnconfiguredJob") do + include Sentry::Cron::MonitorCheckIns + + def perform; end + end + + run_entry(:unconfigured, class: "UnconfiguredJob", cron: "0 9 * * *") + + expect(check_ins.map(&:monitor_slug)).to eq(["unconfigured", "unconfigured"]) + end + + it "monitors entries whose class is callable" do + skip "GoodJob #{GoodJob::VERSION} has no callable classes" if Gem::Version.new(GoodJob::VERSION) < Gem::Version.new("4.19.3") + + run_entry(:callable_report, class: -> { ReportJob }, cron: "0 9 * * *") + + expect(check_ins.map(&:monitor_slug)).to eq(["callable_report", "callable_report"]) + end + + it "monitors only the cron record GoodJob persists when a callback enqueues another job" do + job_fixture("AuditJob") do + def perform; end + end + job_fixture("AuditedReportJob") do + before_enqueue { AuditJob.perform_later } + + def perform; end + end + + run_entry(:audited_report, class: "AuditedReportJob", cron: "0 9 * * *") + + # GoodJob gives the nested job the cron key and its unique cron slot; + # the parent enqueue is rejected as a duplicate. Sentry follows that persisted run. + expect(GoodJob::Job.pluck(:job_class, :cron_key)).to eq([["AuditJob", "audited_report"]]) + expect(check_ins.map(&:monitor_slug)).to eq(["audited_report", "audited_report"]) + end + + it "uses the persisted cron record for callable classes without reevaluating the callable" do + skip "GoodJob #{GoodJob::VERSION} has no callable classes" if Gem::Version.new(GoodJob::VERSION) < Gem::Version.new("4.19.3") + + job_fixture("AuditJob") do + def perform; end + end + job_fixture("AuditedReportJob") do + before_enqueue { AuditJob.perform_later } + + def perform; end + end + job_class = double("callable job class") + expect(job_class).to receive(:call).once.and_return(AuditedReportJob) + + run_entry(:audited_report, class: job_class, cron: "0 9 * * *") + + # GoodJob gives the nested job the cron key and its unique cron slot; + # the parent enqueue is rejected as a duplicate. Sentry follows that persisted run. + expect(GoodJob::Job.pluck(:job_class, :cron_key)).to eq([["AuditJob", "audited_report"]]) + expect(check_ins.map(&:monitor_slug)).to eq(["audited_report", "audited_report"]) + expect(check_ins.map(&:status)).to eq([:in_progress, :ok]) + end + + it "still enqueues the job when monitoring it fails" do + string_io = StringIO.new + Sentry.configuration.sdk_logger = ::Logger.new(string_io) + + # find raises for an entry missing from GoodJob's configuration. + GoodJob::CronEntry.new(key: :unlisted_report, class: "ReportJob", cron: "0 9 * * *").enqueue(Time.now) + + expect(GoodJob::Job.count).to eq(1) + drain + expect(check_ins).to be_empty + expect(string_io.string).to include("Not monitoring GoodJob cron entry unlisted_report") + end + + context "when GoodJob runs jobs inline" do + def queue_adapter_for_test + @inline_adapter ||= ::ActiveJob::QueueAdapters::GoodJobAdapter.new(execution_mode: :inline) + end + + it "does not monitor a job the cron job enqueues of its own class" do + job_fixture("ChainedReportJob") do + def perform(again = true) + self.class.perform_later(false) if again + end + end + + run_entry(:chained_report, class: "ChainedReportJob", cron: "0 9 * * *") + + expect(check_ins.map(&:status)).to eq([:in_progress, :ok]) + end + end + + it "does not monitor jobs enqueued outside a cron entry" do + report_job.perform_later + drain + + expect(check_ins).to be_empty + end + + it "keeps running the job if sending the initial check-in fails" do + performed = false + job_fixture("SafeReportJob") { define_method(:perform) { performed = true } } + allow(Sentry).to receive(:capture_check_in).and_raise("transport failed") + + expect { run_entry(:safe_report, class: "SafeReportJob", cron: "0 9 * * *") }.not_to raise_error + expect(performed).to be(true) + expect(GoodJob::Job.first.error).to be_nil + end + + it "does not fail a successful job if sending the final check-in fails" do + allow(Sentry).to receive(:capture_check_in).and_call_original + allow(Sentry).to receive(:capture_check_in).with(anything, :ok, any_args).and_raise("transport failed") + + expect { run_entry(:safe_report, class: "ReportJob", cron: "0 9 * * *") }.not_to raise_error + expect(GoodJob::Job.first.error).to be_nil + end + + it "does not attach Sentry cron metadata to serialized jobs" do + run_entry(:daily_report, class: "ReportJob", cron: "0 9 * * *") + + expect(GoodJob::Job.first.serialized_params).not_to have_key("_sentry_good_job_cron") + end + + it "excludes nothing by default" do + expect(Sentry.configuration.rails.good_job_exclude_cron_jobs).to eq([]) + end + + context "with excluded jobs" do + let(:excluded_jobs) { [:clear_cache, "purge", /\Atmp_/] } + + it "skips entries whose key matches a symbol or string" do + run_entry(:clear_cache, class: "ReportJob", cron: "0 9 * * *") + run_entry(:purge, class: "ReportJob", cron: "0 9 * * *") + + expect(check_ins).to be_empty + end + + it "skips entries whose key matches a regexp" do + run_entry(:tmp_cleanup, class: "ReportJob", cron: "0 9 * * *") + + expect(check_ins).to be_empty + end + + it "monitors entries that do not match" do + run_entry(:clear_cache_daily, class: "ReportJob", cron: "0 9 * * *") + + expect(check_ins.map(&:monitor_slug)).to eq(["clear_cache_daily", "clear_cache_daily"]) + end + end + + context "with cron keys that share a slug" do + let(:long_key) { "sync_salesforce_accounts_with_the_billing_provider" } + let(:cron) do + { + "#{long_key}_daily": { class: "ReportJob", cron: "0 9 * * *" }, + "#{long_key}_weekly": { class: "ReportJob", cron: "0 9 * * 1" }, + nightly_report: { class: "ReportJob", cron: "0 2 * * *" } + } + end + + it "skips all of them and warns" do + string_io = StringIO.new + Sentry.configuration.sdk_logger = ::Logger.new(string_io) + + run_entry(:"#{long_key}_daily") + run_entry(:"#{long_key}_weekly") + run_entry(:nightly_report) + + expect(check_ins.map(&:monitor_slug).uniq).to eq(["nightly_report"]) + expect(string_io.string).to include("Not monitoring GoodJob cron entry #{long_key}_daily", "#{long_key}_weekly") + end + + context "when the others sharing its slug aren't monitored" do + let(:excluded_jobs) { [:"#{long_key}_weekly"] } + let(:cron) do + { + "#{long_key}_daily": { class: "ReportJob", cron: "0 9 * * *" }, + "#{long_key}_weekly": { class: "ReportJob", cron: "0 9 * * 1" }, + "#{long_key}_fast": { class: "ReportJob", cron: "every 10 seconds" }, + "#{long_key}_dynamic": { class: "ReportJob", cron: ->(_last_ran) { "0 9 * * *" } }, + "#{long_key}_self": { class: "SelfMonitoredJob", cron: "0 9 * * *" } + } + end + + before do + job_fixture("SelfMonitoredJob") do + include Sentry::Cron::MonitorCheckIns + sentry_monitor_check_ins slug: "self-monitored" + + def perform; end + end + end + + it "monitors it and doesn't warn about the others" do + string_io = StringIO.new + Sentry.configuration.sdk_logger = ::Logger.new(string_io) + + run_entry(:"#{long_key}_daily") + run_entry(:"#{long_key}_fast") + + expect(check_ins.map(&:monitor_slug)).to eq([long_key, long_key]) + expect(string_io.string).not_to include("Rename or exclude") + end + end + + context "when one of them is excluded" do + let(:excluded_jobs) { [:"#{long_key}_weekly"] } + + it "monitors the other" do + run_entry(:"#{long_key}_daily") + + expect(check_ins.map(&:monitor_slug).uniq).to eq([long_key]) + end + end + end + + context "with no excluded jobs set" do + let(:excluded_jobs) { nil } + + it "monitors the entry" do + run_entry(:daily_report, class: "ReportJob", cron: "0 9 * * *") + + expect(check_ins.map(&:monitor_slug)).to eq(["daily_report", "daily_report"]) + end + end + + context "with an exclusion pattern that raises" do + let(:excluded_jobs) { [->(_key) { raise "boom" }] } + + it "still enqueues and monitors the job" do + run_entry(:daily_report, class: "ReportJob", cron: "0 9 * * *") + + expect(check_ins.map(&:monitor_slug)).to eq(["daily_report", "daily_report"]) + end + end + end + + if RAILS_VERSION >= 7.1 + RSpec.describe "Sentry::Rails::GoodJobCron on a GoodJob database", type: :job do + include_context "active_job backend harness", adapter: :good_job + include_context "good_job database" + + let(:cron) { {} } + let(:configure_sentry) do + proc { |config| config.enabled_patches << :good_job_cron } + end + + def queue_adapter_for_test + @queue_adapter_for_test ||= ::ActiveJob::QueueAdapters::GoodJobAdapter.new(execution_mode: :external) + end + + def boot_adapter(_adapter) + Sentry::Rails::Test::GoodJobDatabase.connect + end + + def reset_adapter(_adapter) + GoodJob::Execution.delete_all + GoodJob::Job.delete_all + end + + around do |example| + configuration = GoodJob.configuration + GoodJob.configuration = GoodJob::Configuration.new({ cron: cron }) + example.run + ensure + GoodJob.configuration = configuration + end + + def drain + lock_id = SecureRandom.uuid + + loop do + result = GoodJob::Job.perform_with_advisory_lock(lock_id: lock_id) + break if result.nil? + raise result.unhandled_error if result.unhandled_error + end + end + + def run_entry(key, at: Time.now.change(usec: 0), **params) + cron[key] = params unless params.empty? + GoodJob::CronEntry.find(key).enqueue(at) + drain + end + + def check_ins + sentry_events.select { |event| event.is_a?(Sentry::CheckInEvent) } + end + + it "reports concurrency-rejected attempts as errors and monitors their subsequent retry" do + overlapping_result = nil + started = false + job_fixture("ExclusiveReportJob") do + include GoodJob::ActiveJobExtensions::Concurrency + self.good_job_labels = ["exclusive_report"] + good_job_concurrency_rule(label: "exclusive_report", perform_limit: 1) + retry_on GoodJob::ActiveJobExtensions::Concurrency::ConcurrencyExceededError, wait: 0, attempts: 2 + + define_method(:perform) do + unless started + started = true + overlapping_result = Thread.new do + GoodJob::Job.perform_with_advisory_lock(lock_id: SecureRandom.uuid) + end.value + end + end + end + + cron[:exclusive_report] = { class: "ExclusiveReportJob", cron: "0 * * * * UTC" } + entry = GoodJob::CronEntry.find(:exclusive_report) + entry.enqueue(Time.now.change(usec: 0)) + entry.enqueue(1.hour.from_now.change(usec: 0)) + drain + + expect(overlapping_result.handled_error).to be_a(GoodJob::ActiveJobExtensions::Concurrency::ConcurrencyExceededError) + expect(GoodJob::Execution.where("error LIKE ?", "%ConcurrencyExceededError%")).to exist + expect(check_ins.map(&:status)).to eq([:in_progress, :in_progress, :error, :ok, :in_progress, :ok]) + end + end + end +end diff --git a/sentry-rails/spec/active_job/good_job_cron_subscriber_spec.rb b/sentry-rails/spec/active_job/good_job_cron_subscriber_spec.rb new file mode 100644 index 000000000..99a643d78 --- /dev/null +++ b/sentry-rails/spec/active_job/good_job_cron_subscriber_spec.rb @@ -0,0 +1,86 @@ +# frozen_string_literal: true + +require "spec_helper" + +begin + require "good_job" +rescue LoadError +end + +if defined?(::GoodJob) + RSpec.describe "GoodJob cron notification subscriber", type: :job do + include_context "active_job backend harness", adapter: :test + include_context "test adapter" + + let(:configure_sentry) { proc { |config| config.enabled_patches << :good_job_cron } } + let(:record) { Struct.new(:cron_key, :job_class).new("report", "ReportJob") } + + around do |example| + configuration = GoodJob.configuration + GoodJob.configuration = GoodJob::Configuration.new({ cron: { report: { class: "ReportJob", cron: "0 9 * * *" } } }) + example.run + ensure + GoodJob.configuration = configuration + end + + before { job_fixture("ReportJob") { def perform; end } } + + def check_ins + sentry_events.grep(Sentry::CheckInEvent) + end + + def instrument(payload = {}, &block) + ActiveSupport::Notifications.instrument("perform_job.good_job", { job: record }.merge(payload), &block) + end + + it "keeps nested executions independent and removes state when they finish" do + outer_payload = inner_payload = nil + instrument do |outer| + outer_payload = outer + instrument { |inner| inner_payload = inner } + end + + expect(check_ins.map(&:status)).to eq([:in_progress, :in_progress, :ok, :ok]) + expect(check_ins[0].check_in_id).to eq(check_ins[3].check_in_id) + expect(check_ins[1].check_in_id).to eq(check_ins[2].check_in_id) + expect(check_ins[0].check_in_id).not_to eq(check_ins[1].check_in_id) + expect(outer_payload).not_to have_key(Sentry::Rails::GoodJobCron::EVENT_STATE) + expect(inner_payload).not_to have_key(Sentry::Rails::GoodJobCron::EVENT_STATE) + end + + [:handled_error, :unhandled_error].each do |error_key| + it "reports #{error_key} from GoodJob's result payload" do + instrument { |payload| payload[error_key] = RuntimeError.new("boom") } + + expect(check_ins.map(&:status)).to eq([:in_progress, :error]) + end + end + + it "reports exceptions that escape the instrumented execution without swallowing them" do + expect { instrument { raise "boom" } }.to raise_error("boom") + + expect(check_ins.map(&:status)).to eq([:in_progress, :error]) + end + + it "does not send check-ins for ordinary jobs" do + record.cron_key = nil + instrument { } + + expect(check_ins).to be_empty + end + + it "does not send check-ins when the patch is disabled in a later configuration" do + Sentry.configuration.enabled_patches.delete(:good_job_cron) + instrument { } + + expect(check_ins).to be_empty + end + + it "registers only once when initialized repeatedly" do + 2.times { Sentry::Rails::GoodJobCron.patch } + instrument { } + + expect(check_ins.map(&:status)).to eq([:in_progress, :ok]) + end + end +end diff --git a/sentry-rails/spec/fixtures/good_job_cron_boot.rb b/sentry-rails/spec/fixtures/good_job_cron_boot.rb new file mode 100644 index 000000000..dcd6c9bb8 --- /dev/null +++ b/sentry-rails/spec/fixtures/good_job_cron_boot.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require "good_job" + +RSpec.describe "Enabling GoodJob cron monitoring during Rails boot" do + it "registers its subscriber without loading CronEntry during Sentry.init" do + expect(Sentry::Rails::GoodJobCron).to receive(:patch).and_wrap_original do |original| + expect($LOADED_FEATURES.grep(%r{/good_job/cron_entry\.rb\z})).to be_empty + original.call + expect($LOADED_FEATURES.grep(%r{/good_job/cron_entry\.rb\z})).to be_empty + end + + make_basic_app do |config| + config.enabled_patches << :good_job_cron + end + + expect(Sentry).to be_initialized + end +end diff --git a/sentry-rails/spec/fixtures/good_job_cron_reload.rb b/sentry-rails/spec/fixtures/good_job_cron_reload.rb new file mode 100644 index 000000000..a3b8fb90b --- /dev/null +++ b/sentry-rails/spec/fixtures/good_job_cron_reload.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +require "good_job" + +RSpec.describe "GoodJob cron monitoring across Rails reloads" do + it "continues monitoring after Rails replaces CronEntry" do + app = Sentry::Rails::Test::Application.define do |application| + application.config.cache_classes = false + application.config.eager_load = false + application.initializer :configure_sentry do + Sentry.init do |config| + config.dsn = "http://12345:67890@sentry.localdomain:3000/sentry/42" + config.transport.transport_class = Sentry::DummyTransport + config.background_worker_threads = 0 + config.enabled_patches << :good_job_cron + end + end + end + + old_entry_class = GoodJob::CronEntry + app.reloader.reload! + expect(GoodJob::CronEntry).not_to equal(old_entry_class) + + GoodJob.configuration = GoodJob::Configuration.new({ cron: { report: { class: "ActiveJob::Base", cron: "0 9 * * *" } } }) + record = Struct.new(:cron_key, :job_class).new("report", "ActiveJob::Base") + ActiveSupport::Notifications.instrument("perform_job.good_job", job: record) { } + + check_ins = sentry_events.grep(Sentry::CheckInEvent) + expect(check_ins.map(&:status)).to eq([:in_progress, :ok]) + expect(check_ins.map(&:monitor_slug)).to eq(["report", "report"]) + end +end diff --git a/sentry-rails/spec/fixtures/good_job_cron_unsupported.rb b/sentry-rails/spec/fixtures/good_job_cron_unsupported.rb new file mode 100644 index 000000000..57d647422 --- /dev/null +++ b/sentry-rails/spec/fixtures/good_job_cron_unsupported.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require "good_job" + +RSpec.describe "Enabling the :good_job_cron patch with a GoodJob older than 4.0" do + let(:sdk_log) { StringIO.new } + + before { stub_const("GoodJob::VERSION", "3.99.0") } + + it "boots the app, logs a warning, and sends no check-ins" do + make_basic_app do |config| + config.enabled_patches << :good_job_cron + config.sdk_logger = Logger.new(sdk_log) + end + + GoodJob.configuration = GoodJob::Configuration.new({ cron: { report: { class: "ActiveJob::Base", cron: "0 9 * * *" } } }) + record = Struct.new(:cron_key, :job_class).new("report", "ActiveJob::Base") + ActiveSupport::Notifications.instrument("perform_job.good_job", job: record) { } + + expect(Sentry).to be_initialized + expect(sdk_log.string).to include("The GoodJob cron integration requires good_job 4.0 or later, but 3.99.0 is installed.") + expect(sentry_events.grep(Sentry::CheckInEvent)).to be_empty + end +end diff --git a/sentry-rails/spec/isolated/good_job_cron_boot_spec.rb b/sentry-rails/spec/isolated/good_job_cron_boot_spec.rb new file mode 100644 index 000000000..7e0c1b370 --- /dev/null +++ b/sentry-rails/spec/isolated/good_job_cron_boot_spec.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require "open3" + +RSpec.describe "GoodJob cron boot in a fresh Rails process" do + it "passes the boot regression without sharing Rails or GoodJob state" do + skip "GoodJob is not bundled in this matrix cell" unless Gem.loaded_specs.key?("good_job") + + fixture = File.expand_path("../fixtures/good_job_cron_boot.rb", __dir__) + stdout, stderr, status = Open3.capture3(RbConfig.ruby, "-S", "rspec", fixture) + + expect(status.exitstatus).to eq(0), stdout + stderr + end +end diff --git a/sentry-rails/spec/isolated/good_job_cron_reload_spec.rb b/sentry-rails/spec/isolated/good_job_cron_reload_spec.rb new file mode 100644 index 000000000..592bd242b --- /dev/null +++ b/sentry-rails/spec/isolated/good_job_cron_reload_spec.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require "open3" + +RSpec.describe "GoodJob cron reload in a fresh Rails process" do + it "passes the reload regression without sharing Rails or GoodJob state" do + skip "GoodJob is not bundled in this matrix cell" unless Gem.loaded_specs.key?("good_job") + + fixture = File.expand_path("../fixtures/good_job_cron_reload.rb", __dir__) + stdout, stderr, status = Open3.capture3(RbConfig.ruby, "-S", "rspec", fixture) + + expect(status.exitstatus).to eq(0), stdout + stderr + end +end diff --git a/sentry-rails/spec/isolated/good_job_cron_unsupported_good_job_spec.rb b/sentry-rails/spec/isolated/good_job_cron_unsupported_good_job_spec.rb new file mode 100644 index 000000000..ad9c7df3d --- /dev/null +++ b/sentry-rails/spec/isolated/good_job_cron_unsupported_good_job_spec.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require "open3" + +RSpec.describe "GoodJob cron with an unsupported GoodJob in a fresh Rails process" do + it "warns instead of monitoring without sharing GoodJob state" do + skip "GoodJob is not bundled in this matrix cell" unless Gem.loaded_specs.key?("good_job") + + fixture = File.expand_path("../fixtures/good_job_cron_unsupported.rb", __dir__) + stdout, stderr, status = Open3.capture3(RbConfig.ruby, "-S", "rspec", fixture) + + expect(status.exitstatus).to eq(0), stdout + stderr + end +end diff --git a/sentry-rails/spec/isolated/good_job_cron_without_good_job_spec.rb b/sentry-rails/spec/isolated/good_job_cron_without_good_job_spec.rb new file mode 100644 index 000000000..b8fad9f63 --- /dev/null +++ b/sentry-rails/spec/isolated/good_job_cron_without_good_job_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +begin + require "simplecov" + SimpleCov.command_name "GoodJobCronWithoutGoodJob" +rescue LoadError +end + +RSpec.describe "Enabling the :good_job_cron patch without the good_job gem" do + let(:sdk_log) { StringIO.new } + + it "boots the app and logs a warning" do + expect(defined?(::GoodJob)).to be_nil + + make_basic_app do |config| + config.enabled_patches << :good_job_cron + config.sdk_logger = Logger.new(sdk_log) + end + + expect(Sentry).to be_initialized + expect(sdk_log.string).to include("You tried to enable the GoodJob cron integration but the `good_job` gem was not detected.") + end +end