Skip to content

Commit 62989c1

Browse files
authored
Example of a rake task with monitor check-ins (#2162)
1 parent bc3de42 commit 62989c1

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

sentry-ruby/examples/crons/Gemfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
source 'https://rubygems.org'
2+
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
3+
4+
ruby '> 2.6'
5+
6+
gem "rake"
7+
gem 'sentry-ruby', path: "../../"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# sentry-ruby Crons example
2+
3+
Crons monitoring allows you to track that certain that should be performed
4+
on a certain schedule are indeed performed on time and without errors. See
5+
[documentation](https://docs.sentry.io/platforms/ruby/crons/) for more details.
6+
7+
This example project has a few rake tasks that manually send monitor check-ins
8+
to Sentry.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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

Comments
 (0)