-
-
Notifications
You must be signed in to change notification settings - Fork 501
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Example of a rake task with monitor check-ins (#2162)
- Loading branch information
1 parent
bc3de42
commit 62989c1
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
source 'https://rubygems.org' | ||
git_source(:github) { |repo| "https://github.com/#{repo}.git" } | ||
|
||
ruby '> 2.6' | ||
|
||
gem "rake" | ||
gem 'sentry-ruby', path: "../../" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# sentry-ruby Crons example | ||
|
||
Crons monitoring allows you to track that certain that should be performed | ||
on a certain schedule are indeed performed on time and without errors. See | ||
[documentation](https://docs.sentry.io/platforms/ruby/crons/) for more details. | ||
|
||
This example project has a few rake tasks that manually send monitor check-ins | ||
to Sentry. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
require "sentry-ruby" | ||
|
||
Sentry.init do |config| | ||
config.dsn = 'https://[email protected]/5434472' | ||
end | ||
|
||
# Create a config from an interval schedule (every 10 minutes) | ||
monitor_config = Sentry::Cron::MonitorConfig.from_interval( | ||
1, | ||
:hour, | ||
checkin_margin: 15, # Optional check-in margin in minutes | ||
max_runtime: 15 # Optional max runtime in minutes | ||
) | ||
|
||
task :successful_cron do | ||
# This check-in will tell Sentry that the cron job started and is in-progress. | ||
# Sentry will expect it to send a :ok check-in within max_runtime minutes. | ||
check_in_id = Sentry.capture_check_in( | ||
"rake-task-example", | ||
:in_progress, | ||
monitor_config: monitor_config | ||
) | ||
|
||
puts "rake task is running" | ||
|
||
Sentry.capture_check_in( | ||
"rake-task-example", | ||
:ok, | ||
check_in_id: check_in_id, | ||
monitor_config: monitor_config | ||
) | ||
end | ||
|
||
task :failed_cron do | ||
check_in_id = Sentry.capture_check_in( | ||
"rake-task-example", | ||
:in_progress, | ||
monitor_config: monitor_config | ||
) | ||
|
||
puts "rake task is running" | ||
|
||
# Sending an :error check-in will mark the cron job as errored on Sentry, | ||
# and this will also create a new Issue on Sentry linked to that cron job. | ||
Sentry.capture_check_in( | ||
"rake-task-example", | ||
:error, | ||
check_in_id: check_in_id, | ||
monitor_config: monitor_config | ||
) | ||
end | ||
|
||
task :heartbeat do | ||
puts "rake task is running" | ||
|
||
# Heartbeat check-in sends :ok status | ||
# without the parent check_in_id. | ||
# This will tell Sentry that this cron run was successful. | ||
Sentry.capture_check_in( | ||
"rake-task-example", | ||
:ok, | ||
monitor_config: monitor_config | ||
) | ||
end | ||
|
||
task :raise_exception do | ||
check_in_id = Sentry.capture_check_in( | ||
"rake-task-example", | ||
:in_progress, | ||
monitor_config: monitor_config | ||
) | ||
|
||
puts "rake task is running" | ||
|
||
# If you raise an error within the job, Sentry will report it and link | ||
# the issue to the cron job. But the job itself will be marked as "in progress" | ||
# until either your job sends another check-in, or it timeouts. | ||
raise "This job errored out" | ||
end |