class CronHelper::Job

Public Class Methods

register(method_name) click to toggle source
# File lib/cron_helper/job.rb, line 3
def self.register(method_name)
  @cron_methods ||= []
  @cron_methods << method_name
end

Public Instance Methods

run() click to toggle source
# File lib/cron_helper/job.rb, line 8
def run
  backup_streams
  hijack_streams

  result = ''

  file_lock do
    cron_methods.each do |m|
      @output.truncate(0)

      begin
        send(m)
      rescue Exception => e
        @output.string.chomp!
        puts "\n" if @output.string.length > 0
        puts "EXCEPTION #{e.class} (#{e.message.chomp})\n#{e.backtrace.join("\n")}\n"
      ensure
        if @output.string.length > 0
          result << "#######################################################\n"
          result << "#{self.class.to_s}##{m.to_s}\n"
          result << "#######################################################\n"
          result << "#{@output.string.chomp}\n"
          result << "---\n\n"
        end
      end
    end
  end
ensure
  restore_streams
  output_handler(result)
end

Private Instance Methods

backup_streams() click to toggle source
# File lib/cron_helper/job.rb, line 50
def backup_streams
  @stderr_backup = $stderr
  @stdout_backup = $stdout
end
cron_methods() click to toggle source
# File lib/cron_helper/job.rb, line 46
def cron_methods
  self.class.instance_variable_get('@cron_methods') || []
end
file_lock() { || ... } click to toggle source
# File lib/cron_helper/job.rb, line 68
def file_lock(&block)
  FileUtils.mkdir_p(lock_dir_path)

  File.open(lock_file_path, File::RDWR|File::CREAT, 0644) do |f|
    unless f.flock(File::LOCK_EX|File::LOCK_NB)
      puts "CRON_HELPER FAILED TO LOCK (#{job_name} at #{Time.zone.now})"
      return
    end

    begin
      yield
    rescue Exception => e
      puts "CRON_HELPER EXCEPTION (#{job_name} at #{Time.zone.now}): #{e.message}\n#{e.backtrace}"
    ensure
      f.flock(File::LOCK_UN)
    end
  end
end
hijack_streams() click to toggle source
# File lib/cron_helper/job.rb, line 64
def hijack_streams
  @output = $stderr = $stdout = StringIO.new
end
job_name() click to toggle source
# File lib/cron_helper/job.rb, line 87
def job_name
  return self.class.to_s
end
lock_dir_path() click to toggle source
# File lib/cron_helper/job.rb, line 91
def lock_dir_path
  return File.join(Rails.root, 'tmp', 'crons')
end
lock_file_name() click to toggle source
# File lib/cron_helper/job.rb, line 95
def lock_file_name
  return "#{job_name}.lock"
end
lock_file_path() click to toggle source
# File lib/cron_helper/job.rb, line 99
def lock_file_path
  return File.join(lock_dir_path, lock_file_name)
end
output_handler(output) click to toggle source
# File lib/cron_helper/job.rb, line 42
def output_handler(output)
  puts output if output.length > 0
end
restore_streams() click to toggle source
# File lib/cron_helper/job.rb, line 55
def restore_streams
  return unless @stderr_backup && @stdout_backup

  $stderr = @stderr_backup
  $stdout = @stdout_backup

  @stderr_backup = @stdout_backup = nil
end