Class: RubyCron::RubyCronJob

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = nil) ⇒ RubyCronJob

Returns a new instance of RubyCronJob.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rubycron.rb', line 18

def initialize(args = nil)
  @warnings, @errors = [], []
  
  case args
    when NilClass then yield self if block_given?
    when Proc     then instance_eval(&args)
    when Hash     then 
      
      args = load_config(:file, args[:configfile]).merge(args) if args[:configfile]
      args = load_config(:url, args[:configurl]).merge(args)   if args[:configurl] 
      
      args.each do |key, value|
        instance_variable_set("@#{key}", value) if value
      end
    else terminate "Expected a hash or a block to initialize, but instead received a #{args.class} object."
  end
        
  check_sanity
  
  rescue => e
    terminate(e.message)
end

Instance Attribute Details

#authorObject

Returns the value of attribute author.



15
16
17
# File 'lib/rubycron.rb', line 15

def author
  @author
end

#errorsObject (readonly)

Returns the value of attribute errors.



16
17
18
# File 'lib/rubycron.rb', line 16

def errors
  @errors
end

#exitonObject

Returns the value of attribute exiton.



15
16
17
# File 'lib/rubycron.rb', line 15

def exiton
  @exiton
end

#logfileObject

Returns the value of attribute logfile.



15
16
17
# File 'lib/rubycron.rb', line 15

def logfile
  @logfile
end

#mailfromObject

Returns the value of attribute mailfrom.



15
16
17
# File 'lib/rubycron.rb', line 15

def mailfrom
  @mailfrom
end

#mailonObject

Returns the value of attribute mailon.



15
16
17
# File 'lib/rubycron.rb', line 15

def mailon
  @mailon
end

#mailsubjectObject

Returns the value of attribute mailsubject.



15
16
17
# File 'lib/rubycron.rb', line 15

def mailsubject
  @mailsubject
end

#mailtoObject

Returns the value of attribute mailto.



15
16
17
# File 'lib/rubycron.rb', line 15

def mailto
  @mailto
end

#nameObject

Returns the value of attribute name.



15
16
17
# File 'lib/rubycron.rb', line 15

def name
  @name
end

#reportObject (readonly)

Returns the value of attribute report.



16
17
18
# File 'lib/rubycron.rb', line 16

def report
  @report
end

#smtpsettingsObject

Returns the value of attribute smtpsettings.



15
16
17
# File 'lib/rubycron.rb', line 15

def smtpsettings
  @smtpsettings
end

#templateObject

Returns the value of attribute template.



15
16
17
# File 'lib/rubycron.rb', line 15

def template
  @template
end

#verboseObject

Returns the value of attribute verbose.



15
16
17
# File 'lib/rubycron.rb', line 15

def verbose
  @verbose
end

#warningsObject (readonly)

Returns the value of attribute warnings.



16
17
18
# File 'lib/rubycron.rb', line 16

def warnings
  @warnings
end

Instance Method Details

#check_sanityObject



55
56
57
58
59
60
61
62
63
# File 'lib/rubycron.rb', line 55

def check_sanity
  raise "This job has no name."   unless @name 
  raise "This job has no author." unless @author
  raise "No To: header was set. " unless @mailto
  
  check_smtp_settings
  set_defaults
  enable_file_logging if @logfile  
end

#check_smtp_settingsObject



65
66
67
68
69
70
71
72
73
# File 'lib/rubycron.rb', line 65

def check_smtp_settings     
  if @smtpsettings
    raise "SMTP settings have to be passed in as a hash." unless @smtpsettings.instance_of?(Hash)
    raise "SMTP settings should include at least an address (:address)." unless @smtpsettings.keys.include?(:address)
    raise "SMTP settings should include at least a port number (:port)." unless @smtpsettings.keys.include?(:port)
  elsif @smtpsettings.nil?
    raise "Cannot connect to local smtp server." unless smtp_connection?
  end
end

#enable_file_loggingObject



83
84
85
86
87
88
89
90
# File 'lib/rubycron.rb', line 83

def enable_file_logging
  $stdout.reopen(@logfile, "a")
  $stdout.sync = true
  $stderr.reopen($stdout)
  rescue => e
    $stdout, $stderr = STDOUT, STDERR
    raise e
end

#error(message) ⇒ Object



124
125
126
127
128
# File 'lib/rubycron.rb', line 124

def error(message)
  $stderr.puts message if self.verbose || self.logfile
  raise "Configured to exit on error." if exiton == (:error || :all) 
  @errors << message
end

#execute(&block) ⇒ Object

Execute a given block of code (the cronjob), rescue encountered errors, and send a report about it if necessary.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rubycron.rb', line 99

def execute(&block)
  @starttime = Time.now
  puts "\nStarting run of #{self.name} at #{@starttime}.\n----"  if self.verbose || self.logfile
  instance_eval(&block)
rescue Exception => e
  @errors << e.message
  terminate(e.message) if exiton == (:error || :all)
ensure
  @endtime = Time.now
  if self.verbose || self.logfile
    puts "Run ended at #{@endtime}.\n----"  
    puts "Number of warnings: #{@warnings.size}" 
    puts "Number of errors  : #{@errors.size}" 
  end
  unless self.mailon == :none || (@warnings.empty? && @errors.empty? && self.mailon != :all)
    send_report
  end 
end

#load_config(source_type, source) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rubycron.rb', line 41

def load_config(source_type, source)
  if source_type == :file
    io = File.open(source) if File.file?(source)
  elsif source_type == :url
    io = open(source)
  end
  yml = YAML::load(io)
  if yml.is_a?(Hash)
    return yml
  else
    terminate "Could not load the YAML configuration."
  end
end

#set_defaultsObject



75
76
77
78
79
80
81
# File 'lib/rubycron.rb', line 75

def set_defaults
  @mailfrom       ||= 'root@localhost' 
  @verbose        ||= false 
  @template       ||= File.join(File.dirname(__FILE__), '/report.erb')
  @mailon = :all unless self.mailon && [:none, :warning, :error, :all].include?(self.mailon)
  @exiton = :all unless self.exiton && [:none, :warning, :error, :all].include?(self.exiton)
end

#terminate(message) ⇒ Object



92
93
94
95
# File 'lib/rubycron.rb', line 92

def terminate(message)
  $stderr.puts "## Cannot complete job. Reason: #{message}" unless ENV['RSPEC']
  exit 1
end

#warning(message) ⇒ Object



118
119
120
121
122
# File 'lib/rubycron.rb', line 118

def warning(message)
 $stderr.puts message if self.verbose || self.logfile
 raise "Configured to exit on warning." if exiton == (:warning || :all)
 @warnings << message
end