Skip to content

Instantly share code, notes, and snippets.

@benders
Created November 7, 2009 21:51
Show Gist options
  • Save benders/228909 to your computer and use it in GitHub Desktop.
Save benders/228909 to your computer and use it in GitHub Desktop.
Script for checking BackgroundJob activity, suitable for Cron or Nagios
#!/usr/bin/env ruby
# Crontab example:
# 55 * * * * (cd $HOME/rails-app/current ; ./script/check_bj_activity.rb >/dev/null)
TEST_SQL = <<-SQL
SELECT COUNT(*) FROM bj_job WHERE state = 'finished'
AND finished_at > DATE_SUB(UTC_TIMESTAMP(), INTERVAL 1 HOUR)
SQL
begin
require File.dirname(__FILE__) + '/../config/boot'
require File.dirname(__FILE__) + '/../config/environment'
recent_jobs = ActiveRecord::Base.connection.select_value(TEST_SQL).to_i
rescue Exception => e
STDERR.puts "ERROR: #{e.class.to_s} - #{e.to_s}"
Kernel.exit(3)
end
if recent_jobs > 0
STDOUT.puts "Found #{recent_jobs} finished jobs in the last hour"
Kernel.exit(0)
else
STDERR.puts "WARNING: No jobs finished in the last hour!"
Kernel.exit(1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment