A simple example demonstrating how to use Sentry Crons monitoring with Clockwork, a Ruby job scheduler.
- Runs a heartbeat task every minute
- Reports check-ins to Sentry Crons with upserting
- Handles errors gracefully
-
Install dependencies:
cd ruby/clockwork bundle install -
Configure Sentry:
Option A: Using .env file (for local development)
cp .env.example .env
Then edit
.envand set your actual Sentry DSN:SENTRY_DSN=https://your-actual-dsn@o0.ingest.sentry.io/0 SENTRY_ENVIRONMENT=developmentOption B: Using environment variables (for production)
export SENTRY_DSN="https://your-actual-dsn@o0.ingest.sentry.io/0" export SENTRY_ENVIRONMENT="production"
-
Run the scheduler:
bundle exec clockwork clock.rb
-
Build the image:
docker build -t ruby-clockwork-example . -
Run with environment variables:
docker run -d --name clockwork-app \ -e SENTRY_DSN="your-dsn-here" \ -e SENTRY_ENVIRONMENT="development" \ ruby-clockwork-example
Or with .env file:
docker run -d --name clockwork-app --env-file .env ruby-clockwork-example
-
View logs:
docker logs -f clockwork-app
-
Stop and remove:
docker stop clockwork-app && docker rm clockwork-app
Clockwork is a simple Ruby daemon that runs scheduled jobs. Unlike cron, it runs as a persistent process and doesn't require system cron to be installed.
The example:
- Initializes Sentry SDK with environment variables
- Defines a heartbeat job that runs every minute
- Uses Sentry check-ins to monitor job execution
- Automatically creates/updates the monitor in Sentry (upserting)
The heartbeat task includes full Sentry cron monitoring:
- Automatic Monitor Creation: The monitor is created/updated automatically when the job runs
- Check-in Tracking: Each execution sends:
- Start status (
in_progress) - End status (
okon success,erroron failure)
- Start status (
- Error Reporting: Any exceptions are captured and sent to Sentry
- Monitor Configuration:
- Schedule: Every minute (
* * * * *) - Check-in margin: 1 minute
- Max runtime: 2 minutes
- Monitor slug:
ruby-clockwork-heartbeat
- Schedule: Every minute (