rubycron 0.2.5 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +42 -31
- data/lib/rubycron/main.rb +14 -7
- data/lib/rubycron/report.erb +6 -0
- data/lib/rubycron/version.rb +1 -1
- metadata +2 -2
    
        data/README.md
    CHANGED
    
    | @@ -25,24 +25,26 @@ By default, RubyCron assumes you have a local smtp server running on port 25 in | |
| 25 25 |  | 
| 26 26 | 
             
            * ruby-1.8.7-p371 [ i686 ]
         | 
| 27 27 | 
             
            * ruby-1.9.2-p320 [ x86_64 ]
         | 
| 28 | 
            -
            * ruby-1.9.3- | 
| 29 | 
            -
            *  | 
| 28 | 
            +
            * ruby-1.9.3-p448 [ x86_64 ]
         | 
| 29 | 
            +
            * ruby-2.0.0-p247 [ x86_64 ]
         | 
| 30 | 
            +
            * jruby-1.7.4 [ x86_64 ]
         | 
| 30 31 |  | 
| 31 32 | 
             
            ## Usage
         | 
| 32 33 |  | 
| 33 34 | 
             
            ### Configure the basics
         | 
| 34 35 |  | 
| 35 36 | 
             
            Open a new file, require and include RubyCron, and then initialize a new RubyCronJob as follows:
         | 
| 36 | 
            -
             | 
| 37 | 
            +
            ```ruby
         | 
| 37 38 | 
             
            	rcj = RubyCronJob.new( 	:author => 'John Doe',
         | 
| 38 39 | 
             
              							:name   => 'test',
         | 
| 39 40 | 
             
            							:mailto => 'john@doe.com' )
         | 
| 40 | 
            -
             | 
| 41 | 
            +
            ```
         | 
| 41 42 | 
             
            ### Write your cronjob
         | 
| 42 43 |  | 
| 43 | 
            -
            Call RubyCronJob's execute method, and define your cronjob within the do-end block.
         | 
| 44 | 
            -
             | 
| 44 | 
            +
            Call RubyCronJob's execute method, and define your cronjob within the do-end block. Use the *info*, *warning* and *error* methods to create your reports.
         | 
| 45 | 
            +
            ```ruby
         | 
| 45 46 | 
             
            	rcj.execute do 
         | 
| 47 | 
            +
            	  info "Starting run on #{`uname -n`}"
         | 
| 46 48 | 
             
            	  unless File.directory?('/tmp')
         | 
| 47 49 | 
             
            	    warning "Something awry is going on with /tmp."
         | 
| 48 50 | 
             
            	  end
         | 
| @@ -54,7 +56,7 @@ Call RubyCronJob's execute method, and define your cronjob within the do-end blo | |
| 54 56 | 
             
            	    error "Something went wrong trying to write to file: #{e.message}"
         | 
| 55 57 | 
             
            	  end
         | 
| 56 58 | 
             
            	end
         | 
| 57 | 
            -
            	
         | 
| 59 | 
            +
            ```	
         | 
| 58 60 | 
             
            That's it! Now when you run this cronjob, you will receive a report by email.
         | 
| 59 61 |  | 
| 60 62 | 
             
            ## Set up Cron
         | 
| @@ -64,7 +66,7 @@ To activate the cronjob, add it to your crontab like any other cronjob. There ar | |
| 64 66 | 
             
            ### Stand-alone
         | 
| 65 67 |  | 
| 66 68 | 
             
            For the stand-alone option, add a crontab entry like so:
         | 
| 67 | 
            -
             | 
| 69 | 
            +
             | 
| 68 70 | 
             
            	min		hour	mday	month	wday	command
         | 
| 69 71 | 
             
            	* 		* 		* 		* 		* 		test.rcj
         | 
| 70 72 |  | 
| @@ -82,23 +84,23 @@ Simply feed your rubycronjob to rcjrunner.rb as a command-line argument in your | |
| 82 84 | 
             
            ### I now get all these reports, but I really only care if the job fails! 
         | 
| 83 85 |  | 
| 84 86 | 
             
            Sorting through hundreds of cron mails per day that report successful runs may be gratifying at times, but most sane people only care to be notified when their cronjobs fail. Not to worry, just add the following line to the RubyCronJob's initialization hash. 
         | 
| 85 | 
            -
             | 
| 87 | 
            +
            ```ruby
         | 
| 86 88 | 
             
            	:mailon	=> :error
         | 
| 87 | 
            -
             | 
| 88 | 
            -
            RubyCron will now only report when errors occurred during the run. Other options are  | 
| 89 | 
            +
            ```
         | 
| 90 | 
            +
            RubyCron will now only report when errors occurred during the run. Other options are *:none*, *:warning* and **:all** (default).
         | 
| 89 91 |  | 
| 90 92 | 
             
            ### All emails originate from root@localhost. Can I change that?
         | 
| 91 93 |  | 
| 92 94 | 
             
            Of course. Use 
         | 
| 93 | 
            -
             | 
| 95 | 
            +
            ```ruby
         | 
| 94 96 | 
             
            	:mailfrom => 'root@doe.com'
         | 
| 95 | 
            -
             | 
| 97 | 
            +
            ```
         | 
| 96 98 | 
             
            to change the From:-header.
         | 
| 97 99 |  | 
| 98 100 | 
             
            ### Do I really have to configure a local smtp server to send mail?
         | 
| 99 101 |  | 
| 100 102 | 
             
            No. You can use other smtp servers for delivery like so:
         | 
| 101 | 
            -
             | 
| 103 | 
            +
            ```ruby
         | 
| 102 104 | 
             
            	smtpsettings = { 	:address              => 'smtp.gmail.com',
         | 
| 103 105 | 
             
                        			:port                 => 587,
         | 
| 104 106 | 
             
                        			:domain               => 'your.host.name',
         | 
| @@ -112,58 +114,67 @@ No. You can use other smtp servers for delivery like so: | |
| 112 114 | 
             
            						 	:mailto     	=> 'john@doe.com',
         | 
| 113 115 | 
             
            						 	:mailfrom   	=> 'root@doe.com',
         | 
| 114 116 | 
             
            							:smtpsettings 	=> smtpsettings )
         | 
| 115 | 
            -
             | 
| 117 | 
            +
            ```
         | 
| 116 118 | 
             
            ### I want my cronjob to stop running when there are errors.
         | 
| 117 119 |  | 
| 118 120 | 
             
            No problem. You can configure this behavior with
         | 
| 119 | 
            -
             | 
| 121 | 
            +
            ```ruby
         | 
| 120 122 | 
             
            	:exiton	=> :all
         | 
| 121 | 
            -
             | 
| 122 | 
            -
            Valid values are  | 
| 123 | 
            +
            ```
         | 
| 124 | 
            +
            Valid values are **:none** (default), *:warning*, *:error*, *:all*.
         | 
| 123 125 |  | 
| 124 126 | 
             
            ### Is there a way to manipulate the content of the email reports?
         | 
| 125 127 |  | 
| 126 128 | 
             
            There sure is. Simply write your own ERB template, and tell the RubyCronJob about it with the :template directive.
         | 
| 127 | 
            -
             | 
| 129 | 
            +
            ```ruby
         | 
| 128 130 | 
             
            	rcj = RubyCronJob.new(
         | 
| 129 131 | 
             
            		:author     	=> 'John Doe',
         | 
| 130 132 | 
             
            	 	:name       	=> 'test',
         | 
| 131 133 | 
             
            	 	:mailto     	=> 'john@doe.com',
         | 
| 132 134 | 
             
            	 	:mailfrom   	=> 'root@doe.com',
         | 
| 133 | 
            -
            		:template | 
| 134 | 
            -
             | 
| 135 | 
            +
            		:template 	=> 'my_template.erb' )
         | 
| 136 | 
            +
            ```
         | 
| 135 137 | 
             
            Note that from inside the ERB template (my_template.erb in the above example) you have access to the @warnings and @errors arrays.
         | 
| 136 138 |  | 
| 137 139 | 
             
            ### May I please see some output while I'm developing my cronjob?
         | 
| 138 140 |  | 
| 139 141 | 
             
            Output to stdout and stderr can be very useful when debugging your cronjob. Just set the verbose flag to true:
         | 
| 140 | 
            -
             | 
| 142 | 
            +
            ```ruby
         | 
| 141 143 | 
             
            	:verbose => true
         | 
| 144 | 
            +
            ```
         | 
| 142 145 |  | 
| 143 | 
            -
            ###  | 
| 146 | 
            +
            ### Is er there a debug mode?
         | 
| 144 147 |  | 
| 145 | 
            -
             | 
| 148 | 
            +
            Absolutely: 
         | 
| 149 | 
            +
            ```ruby
         | 
| 150 | 
            +
            	:debug => true
         | 
| 151 | 
            +
            ```
         | 
| 152 | 
            +
            This is roughly the equivalent of setting :verbose to true and :mailon to :none. It allows you to test your cronjobs without being spammed.
         | 
| 146 153 |  | 
| 147 | 
            -
             | 
| 154 | 
            +
            ### As a sysadmin, I like grepping through files. Can I have a log file please?
         | 
| 148 155 |  | 
| 156 | 
            +
            Yes. Set a file path in RubyCronJob's logfile variable, and all output will be redirected to file:
         | 
| 157 | 
            +
            ```ruby
         | 
| 158 | 
            +
            	:logfile => '/tmp/rcjlogfile.log'
         | 
| 159 | 
            +
            ```
         | 
| 149 160 | 
             
            Note that you will still receive email reports when you enable file logging. 
         | 
| 150 161 |  | 
| 151 162 | 
             
            ### I like all these different options, but can't I set some of them globally for all my cronjobs?
         | 
| 152 163 |  | 
| 153 164 | 
             
            Anything to prevent redundancy, right? Use the :configfile and :configurl directives to point towards YAML files that hold your configuration hashes. For instance, this works:
         | 
| 154 | 
            -
             | 
| 165 | 
            +
            ```ruby
         | 
| 155 166 | 
             
            	rcj = RubyCronJob.new( :configfile => "my_config_file.yml" )
         | 
| 156 | 
            -
             | 
| 167 | 
            +
            ```
         | 
| 157 168 | 
             
            Or this:
         | 
| 158 | 
            -
             | 
| 169 | 
            +
            ```ruby
         | 
| 159 170 | 
             
            	rcj = RubyCronJob.new( :configurl => "http://www.foo.bar/my_config.yml")
         | 
| 160 | 
            -
             | 
| 171 | 
            +
            ```
         | 
| 161 172 | 
             
            Or even a combination:
         | 
| 162 | 
            -
             | 
| 173 | 
            +
            ```ruby
         | 
| 163 174 | 
             
            	rcj = RubyCronJob.new(  :configfile => "my_config_file.yml",
         | 
| 164 175 | 
             
            							:configurl 	=> "http://www.foo.bar/my_config.yml",
         | 
| 165 176 | 
             
            							:author    	=> 'John Doe' )
         | 
| 166 | 
            -
            	
         | 
| 177 | 
            +
            ```	
         | 
| 167 178 | 
             
            Note that in the latter case the values of the directives specified within the RubyCronJob itself will take precedence over the file or url directives.
         | 
| 168 179 |  | 
| 169 180 | 
             
            ## License
         | 
    
        data/lib/rubycron/main.rb
    CHANGED
    
    | @@ -13,13 +13,13 @@ module RubyCron | |
| 13 13 | 
             
              require 'erb'
         | 
| 14 14 |  | 
| 15 15 | 
             
              attr_accessor :name, :author, :mailto, :mailfrom, :mailsubject, :mailon, :exiton, :template, :smtpsettings, :debug, :logfile, :verbose
         | 
| 16 | 
            -
              attr_reader   :warnings, :errors, :report
         | 
| 16 | 
            +
              attr_reader   :messages, :warnings, :errors, :report
         | 
| 17 17 |  | 
| 18 18 | 
             
              DEFAULT_SERVER = 'localhost'
         | 
| 19 19 | 
             
              DEFAULT_PORT   = 25
         | 
| 20 20 |  | 
| 21 21 | 
             
                def initialize(args = nil)
         | 
| 22 | 
            -
                  @warnings, @errors = [], []
         | 
| 22 | 
            +
                  @messages, @warnings, @errors = [], [], []
         | 
| 23 23 |  | 
| 24 24 | 
             
                  case args
         | 
| 25 25 | 
             
                    when NilClass then yield self if block_given?
         | 
| @@ -125,12 +125,19 @@ module RubyCron | |
| 125 125 | 
             
                  end 
         | 
| 126 126 | 
             
                end
         | 
| 127 127 |  | 
| 128 | 
            -
             | 
| 129 | 
            -
             | 
| 130 | 
            -
             | 
| 131 | 
            -
             | 
| 132 | 
            -
             | 
| 128 | 
            +
                def produce_summary
         | 
| 129 | 
            +
                  puts "Run ended at #{@endtime}.\n----"  
         | 
| 130 | 
            +
                  puts "Number of messages: #{@messages.size}"
         | 
| 131 | 
            +
                  puts "Number of warnings: #{@warnings.size}" 
         | 
| 132 | 
            +
                  puts "Number of errors  : #{@errors.size}"
         | 
| 133 | 
            +
                end
         | 
| 133 134 |  | 
| 135 | 
            +
                def message(message)
         | 
| 136 | 
            +
                  $stderr.puts "[INFO ] #{message}" if self.verbose || self.logfile
         | 
| 137 | 
            +
                  @messages << message
         | 
| 138 | 
            +
                end
         | 
| 139 | 
            +
                alias_method :info, :message
         | 
| 140 | 
            +
                
         | 
| 134 141 | 
             
                def warning(message)
         | 
| 135 142 | 
             
                 $stderr.puts "[WARN ] #{message}" if self.verbose || self.logfile
         | 
| 136 143 | 
             
                 @warnings << message
         | 
    
        data/lib/rubycron/report.erb
    CHANGED
    
    | @@ -2,6 +2,12 @@ This is your RubyCron mailer reporting on <%= self.name %>. | |
| 2 2 |  | 
| 3 3 | 
             
            There were <%= @warnings.size %> warnings and <%= @errors.size %> errors during the last run, which started at 
         | 
| 4 4 | 
             
            <%= @starttime %> and ended at <%= @endtime %>.
         | 
| 5 | 
            +
            <% unless @messages.empty? %>
         | 
| 6 | 
            +
            <%= "These were the informational messages:" %>
         | 
| 7 | 
            +
            <% @messages.each_with_index do |message, index| %> 
         | 
| 8 | 
            +
            <%=	index + 1 %>: <%= message %>
         | 
| 9 | 
            +
            <%=	"============================================================" %>
         | 
| 10 | 
            +
            <% end %> <% end %>
         | 
| 5 11 | 
             
            <% unless @warnings.empty? %>
         | 
| 6 12 | 
             
            <%= "These were the warnings:" %>
         | 
| 7 13 | 
             
            <% @warnings.each_with_index do |warning, index| %> 
         | 
    
        data/lib/rubycron/version.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: rubycron
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0. | 
| 4 | 
            +
              version: 0.3.0
         | 
| 5 5 | 
             
              prerelease: 
         | 
| 6 6 | 
             
            platform: ruby
         | 
| 7 7 | 
             
            authors:
         | 
| @@ -9,7 +9,7 @@ authors: | |
| 9 9 | 
             
            autorequire: 
         | 
| 10 10 | 
             
            bindir: bin
         | 
| 11 11 | 
             
            cert_chain: []
         | 
| 12 | 
            -
            date: 2013- | 
| 12 | 
            +
            date: 2013-09-11 00:00:00.000000000 Z
         | 
| 13 13 | 
             
            dependencies:
         | 
| 14 14 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 15 15 | 
             
              name: mail
         |