Skip to content

Commit

Permalink
Example of a rake task with monitor check-ins (#2162)
Browse files Browse the repository at this point in the history
  • Loading branch information
natikgadzhi authored Nov 23, 2023
1 parent bc3de42 commit 62989c1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
7 changes: 7 additions & 0 deletions sentry-ruby/examples/crons/Gemfile
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: "../../"
8 changes: 8 additions & 0 deletions sentry-ruby/examples/crons/README.md
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.
79 changes: 79 additions & 0 deletions sentry-ruby/examples/crons/Rakefile.rb
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

0 comments on commit 62989c1

Please sign in to comment.