Created
September 1, 2010 16:04
-
-
Save dmerrick/560923 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# a mixin to provide logging and nagios monitoring to cron jobs | |
module Utils | |
module BCMCronJob | |
# set up the log file | |
# we could also simply use RAILS_DEFAULT_LOGGER | |
def before_job | |
log_file_name = "bcm_cron.log" | |
@log_file ||= File.open(File.join(Rails.root, 'log', log_file_name), 'a') | |
require 'logger' | |
@logger ||= Logger.new(@log_file) | |
#@logger.level = Logger::INFO | |
end | |
# run the job and log it | |
# if given a block, log the value yielded | |
def run_job(job_name = "BCM_CRON") | |
if @logger.info? | |
@logger.info(job_name + " starting.") | |
if block_given? | |
block_result = yield | |
@logger.info(job_name + " yielded: #{block_result}") if block_result.respond_to?(:to_s) | |
end | |
@logger.info(job_name + " finished.") | |
end | |
end | |
def after_job | |
# write the changes to the file | |
@log_file.flush | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment