Class: CronHelper::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/cron_helper/job.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.register(method_name) ⇒ Object



3
4
5
6
# File 'lib/cron_helper/job.rb', line 3

def self.register(method_name)
  @cron_methods ||= []
  @cron_methods << method_name
end

Instance Method Details

#runObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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