Class: RubyCron::RubyCronJob
- Inherits:
-
Object
- Object
- RubyCron::RubyCronJob
- Defined in:
- lib/rubycron/main.rb
Constant Summary collapse
- DEFAULT_SERVER =
'localhost'
- DEFAULT_PORT =
25
Instance Attribute Summary collapse
-
#author ⇒ Object
Returns the value of attribute author.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#exiton ⇒ Object
Returns the value of attribute exiton.
-
#logfile ⇒ Object
Returns the value of attribute logfile.
-
#mailfrom ⇒ Object
Returns the value of attribute mailfrom.
-
#mailon ⇒ Object
Returns the value of attribute mailon.
-
#mailsubject ⇒ Object
Returns the value of attribute mailsubject.
-
#mailto ⇒ Object
Returns the value of attribute mailto.
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
-
#name ⇒ Object
Returns the value of attribute name.
-
#report ⇒ Object
readonly
Returns the value of attribute report.
-
#smtpsettings ⇒ Object
Returns the value of attribute smtpsettings.
-
#template ⇒ Object
Returns the value of attribute template.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
-
#warnings ⇒ Object
readonly
Returns the value of attribute warnings.
Instance Method Summary collapse
- #check_sanity ⇒ Object
- #check_smtp_settings ⇒ Object
- #enable_debug_mode ⇒ Object
- #enable_file_logging ⇒ Object
- #error(message) ⇒ Object
-
#execute(&block) ⇒ Object
Execute a given block of code (the cronjob), rescue encountered errors, and send a report about it if necessary.
-
#initialize(args = nil) ⇒ RubyCronJob
constructor
A new instance of RubyCronJob.
- #load_config(source_type, source) ⇒ Object
- #message(message) ⇒ Object (also: #info)
- #produce_summary ⇒ Object
- #set_defaults ⇒ Object
- #terminate(message) ⇒ Object
- #warning(message) ⇒ Object
Constructor Details
#initialize(args = nil) ⇒ RubyCronJob
Returns a new instance of RubyCronJob.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rubycron/main.rb', line 21 def initialize(args = nil) @messages, @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.) end |
Instance Attribute Details
#author ⇒ Object
Returns the value of attribute author.
15 16 17 |
# File 'lib/rubycron/main.rb', line 15 def @author end |
#debug ⇒ Object
Returns the value of attribute debug.
15 16 17 |
# File 'lib/rubycron/main.rb', line 15 def debug @debug end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
16 17 18 |
# File 'lib/rubycron/main.rb', line 16 def errors @errors end |
#exiton ⇒ Object
Returns the value of attribute exiton.
15 16 17 |
# File 'lib/rubycron/main.rb', line 15 def exiton @exiton end |
#logfile ⇒ Object
Returns the value of attribute logfile.
15 16 17 |
# File 'lib/rubycron/main.rb', line 15 def logfile @logfile end |
#mailfrom ⇒ Object
Returns the value of attribute mailfrom.
15 16 17 |
# File 'lib/rubycron/main.rb', line 15 def mailfrom @mailfrom end |
#mailon ⇒ Object
Returns the value of attribute mailon.
15 16 17 |
# File 'lib/rubycron/main.rb', line 15 def mailon @mailon end |
#mailsubject ⇒ Object
Returns the value of attribute mailsubject.
15 16 17 |
# File 'lib/rubycron/main.rb', line 15 def mailsubject @mailsubject end |
#mailto ⇒ Object
Returns the value of attribute mailto.
15 16 17 |
# File 'lib/rubycron/main.rb', line 15 def mailto @mailto end |
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
16 17 18 |
# File 'lib/rubycron/main.rb', line 16 def @messages end |
#name ⇒ Object
Returns the value of attribute name.
15 16 17 |
# File 'lib/rubycron/main.rb', line 15 def name @name end |
#report ⇒ Object (readonly)
Returns the value of attribute report.
16 17 18 |
# File 'lib/rubycron/main.rb', line 16 def report @report end |
#smtpsettings ⇒ Object
Returns the value of attribute smtpsettings.
15 16 17 |
# File 'lib/rubycron/main.rb', line 15 def smtpsettings @smtpsettings end |
#template ⇒ Object
Returns the value of attribute template.
15 16 17 |
# File 'lib/rubycron/main.rb', line 15 def template @template end |
#verbose ⇒ Object
Returns the value of attribute verbose.
15 16 17 |
# File 'lib/rubycron/main.rb', line 15 def verbose @verbose end |
#warnings ⇒ Object (readonly)
Returns the value of attribute warnings.
16 17 18 |
# File 'lib/rubycron/main.rb', line 16 def warnings @warnings end |
Instance Method Details
#check_sanity ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rubycron/main.rb', line 58 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_debug_mode if @debug enable_file_logging if @logfile end |
#check_smtp_settings ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'lib/rubycron/main.rb', line 69 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_debug_mode ⇒ Object
87 88 89 90 |
# File 'lib/rubycron/main.rb', line 87 def enable_debug_mode @mailon = :none @verbose = true end |
#enable_file_logging ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'lib/rubycron/main.rb', line 92 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
147 148 149 150 151 |
# File 'lib/rubycron/main.rb', line 147 def error() $stderr.puts "[ERROR] #{}" if self.verbose || self.logfile @errors << raise ExitOnError.new("Configured to exit on error.") if exiton == (:error || :all) end |
#execute(&block) ⇒ Object
Execute a given block of code (the cronjob), rescue encountered errors, and send a report about it if necessary.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/rubycron/main.rb', line 108 def execute(&block) puts "[INFO ] Running in debug mode. Will not send mail." if self.debug @starttime = Time.now puts "\nStarting run of #{self.name} at #{@starttime}.\n----" if self.verbose || self.logfile instance_eval(&block) rescue ExitOnWarning, ExitOnError => e terminate(e.) rescue Exception => e trace = "#{e.}\n" + e.backtrace.join("\n\t") @errors << trace $stderr.puts "[ERROR] #{trace}" if self.verbose || self.logfile terminate(trace) if exiton == (:error || :all) ensure @endtime = Time.now produce_summary if (self.verbose || self.logfile) unless self.mailon == :none || (@warnings.empty? && @errors.empty? && self.mailon != :all) send_report end end |
#load_config(source_type, source) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rubycron/main.rb', line 44 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 |
#message(message) ⇒ Object Also known as: info
135 136 137 138 |
# File 'lib/rubycron/main.rb', line 135 def () $stderr.puts "[INFO ] #{}" if self.verbose || self.logfile @messages << end |
#produce_summary ⇒ Object
128 129 130 131 132 133 |
# File 'lib/rubycron/main.rb', line 128 def produce_summary puts "Run ended at #{@endtime}.\n----" puts "Number of messages: #{@messages.size}" puts "Number of warnings: #{@warnings.size}" puts "Number of errors : #{@errors.size}" end |
#set_defaults ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/rubycron/main.rb', line 79 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
101 102 103 104 |
# File 'lib/rubycron/main.rb', line 101 def terminate() $stderr.puts "## Cannot complete job. Reason: #{}" unless ENV['RSPEC'] exit 1 end |
#warning(message) ⇒ Object
141 142 143 144 145 |
# File 'lib/rubycron/main.rb', line 141 def warning() $stderr.puts "[WARN ] #{}" if self.verbose || self.logfile @warnings << raise ExitOnWarning.new("Configured to exit on warning.") if exiton == (:warning || :all) end |