|
| 1 | +require "sentry-ruby" |
| 2 | + |
| 3 | +Sentry.init do |config| |
| 4 | + config.dsn = 'https://2fb45f003d054a7ea47feb45898f7649@o447951.ingest.sentry.io/5434472' |
| 5 | +end |
| 6 | + |
| 7 | +# Create a config from an interval schedule (every 10 minutes) |
| 8 | +monitor_config = Sentry::Cron::MonitorConfig.from_interval( |
| 9 | + 1, |
| 10 | + :hour, |
| 11 | + checkin_margin: 15, # Optional check-in margin in minutes |
| 12 | + max_runtime: 15 # Optional max runtime in minutes |
| 13 | +) |
| 14 | + |
| 15 | +task :successful_cron do |
| 16 | + # This check-in will tell Sentry that the cron job started and is in-progress. |
| 17 | + # Sentry will expect it to send a :ok check-in within max_runtime minutes. |
| 18 | + check_in_id = Sentry.capture_check_in( |
| 19 | + "rake-task-example", |
| 20 | + :in_progress, |
| 21 | + monitor_config: monitor_config |
| 22 | + ) |
| 23 | + |
| 24 | + puts "rake task is running" |
| 25 | + |
| 26 | + Sentry.capture_check_in( |
| 27 | + "rake-task-example", |
| 28 | + :ok, |
| 29 | + check_in_id: check_in_id, |
| 30 | + monitor_config: monitor_config |
| 31 | + ) |
| 32 | +end |
| 33 | + |
| 34 | +task :failed_cron do |
| 35 | + check_in_id = Sentry.capture_check_in( |
| 36 | + "rake-task-example", |
| 37 | + :in_progress, |
| 38 | + monitor_config: monitor_config |
| 39 | + ) |
| 40 | + |
| 41 | + puts "rake task is running" |
| 42 | + |
| 43 | + # Sending an :error check-in will mark the cron job as errored on Sentry, |
| 44 | + # and this will also create a new Issue on Sentry linked to that cron job. |
| 45 | + Sentry.capture_check_in( |
| 46 | + "rake-task-example", |
| 47 | + :error, |
| 48 | + check_in_id: check_in_id, |
| 49 | + monitor_config: monitor_config |
| 50 | + ) |
| 51 | +end |
| 52 | + |
| 53 | +task :heartbeat do |
| 54 | + puts "rake task is running" |
| 55 | + |
| 56 | + # Heartbeat check-in sends :ok status |
| 57 | + # without the parent check_in_id. |
| 58 | + # This will tell Sentry that this cron run was successful. |
| 59 | + Sentry.capture_check_in( |
| 60 | + "rake-task-example", |
| 61 | + :ok, |
| 62 | + monitor_config: monitor_config |
| 63 | + ) |
| 64 | +end |
| 65 | + |
| 66 | +task :raise_exception do |
| 67 | + check_in_id = Sentry.capture_check_in( |
| 68 | + "rake-task-example", |
| 69 | + :in_progress, |
| 70 | + monitor_config: monitor_config |
| 71 | + ) |
| 72 | + |
| 73 | + puts "rake task is running" |
| 74 | + |
| 75 | + # If you raise an error within the job, Sentry will report it and link |
| 76 | + # the issue to the cron job. But the job itself will be marked as "in progress" |
| 77 | + # until either your job sends another check-in, or it timeouts. |
| 78 | + raise "This job errored out" |
| 79 | +end |
0 commit comments