Class: RubyCron::RubyCronJob
- Inherits:
-
Object
- Object
- RubyCron::RubyCronJob
- Defined in:
- lib/rubycron.rb
Instance Attribute Summary collapse
-
#author ⇒ Object
Returns the value of attribute author.
-
#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.
-
#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_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
- #set_defaults ⇒ Object
- #terminate(message) ⇒ Object
- #warning(message) ⇒ Object
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.) end |
Instance Attribute Details
#author ⇒ Object
Returns the value of attribute author.
15 16 17 |
# File 'lib/rubycron.rb', line 15 def @author end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
16 17 18 |
# File 'lib/rubycron.rb', line 16 def errors @errors end |
#exiton ⇒ Object
Returns the value of attribute exiton.
15 16 17 |
# File 'lib/rubycron.rb', line 15 def exiton @exiton end |
#logfile ⇒ Object
Returns the value of attribute logfile.
15 16 17 |
# File 'lib/rubycron.rb', line 15 def logfile @logfile end |
#mailfrom ⇒ Object
Returns the value of attribute mailfrom.
15 16 17 |
# File 'lib/rubycron.rb', line 15 def mailfrom @mailfrom end |
#mailon ⇒ Object
Returns the value of attribute mailon.
15 16 17 |
# File 'lib/rubycron.rb', line 15 def mailon @mailon end |
#mailsubject ⇒ Object
Returns the value of attribute mailsubject.
15 16 17 |
# File 'lib/rubycron.rb', line 15 def mailsubject @mailsubject end |
#mailto ⇒ Object
Returns the value of attribute mailto.
15 16 17 |
# File 'lib/rubycron.rb', line 15 def mailto @mailto end |
#name ⇒ Object
Returns the value of attribute name.
15 16 17 |
# File 'lib/rubycron.rb', line 15 def name @name end |
#report ⇒ Object (readonly)
Returns the value of attribute report.
16 17 18 |
# File 'lib/rubycron.rb', line 16 def report @report end |
#smtpsettings ⇒ Object
Returns the value of attribute smtpsettings.
15 16 17 |
# File 'lib/rubycron.rb', line 15 def smtpsettings @smtpsettings end |
#template ⇒ Object
Returns the value of attribute template.
15 16 17 |
# File 'lib/rubycron.rb', line 15 def template @template end |
#verbose ⇒ Object
Returns the value of attribute verbose.
15 16 17 |
# File 'lib/rubycron.rb', line 15 def verbose @verbose end |
#warnings ⇒ Object (readonly)
Returns the value of attribute warnings.
16 17 18 |
# File 'lib/rubycron.rb', line 16 def warnings @warnings end |
Instance Method Details
#check_sanity ⇒ Object
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_settings ⇒ Object
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_logging ⇒ Object
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() $stderr.puts if self.verbose || self.logfile raise "Configured to exit on error." if exiton == (:error || :all) @errors << 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. terminate(e.) 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_defaults ⇒ Object
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() $stderr.puts "## Cannot complete job. Reason: #{}" unless ENV['RSPEC'] exit 1 end |
#warning(message) ⇒ Object
118 119 120 121 122 |
# File 'lib/rubycron.rb', line 118 def warning() $stderr.puts if self.verbose || self.logfile raise "Configured to exit on warning." if exiton == (:warning || :all) @warnings << end |