rubycron 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.md +60 -13
  2. data/bin/rcjrunner.rb +4 -4
  3. data/lib/rubycron.rb +83 -24
  4. data/sample/test.rcj +9 -10
  5. metadata +5 -7
data/README.md CHANGED
@@ -15,7 +15,7 @@ By letting RubyCron deal with warnings, errors, and sending reports, you can foc
15
15
 
16
16
  This gem depends on [Mikel's wonderful mail gem](https://github.com/mikel/mail).
17
17
 
18
- In order to send mail, it assumes you have a local smtp server running on port 25.
18
+ By default, RubyCron assumes you have a local smtp server running on port 25 in order to send mail.
19
19
 
20
20
  ## Usage
21
21
 
@@ -23,11 +23,9 @@ In order to send mail, it assumes you have a local smtp server running on port 2
23
23
 
24
24
  Open a new file, require and include RubyCron, and then initialize a new RubyCronJob as follows:
25
25
 
26
- rcj = RubyCronJob.new do |script|
27
- script.author = 'John Doe'
28
- script.name = 'test'
29
- script.mailto = 'john@doe.com'
30
- end
26
+ rcj = RubyCronJob.new( :author => 'John Doe',
27
+ :name => 'test',
28
+ :mailto => 'john@doe.com' )
31
29
 
32
30
  ### Write your cronjob
33
31
 
@@ -72,9 +70,9 @@ Simply feed your rubycronjob to rcjrunner.rb as a command-line argument in your
72
70
 
73
71
  ### I now get all these reports, but I really only care if the job fails!
74
72
 
75
- 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 to following line to the RubyCronJob's initialization.
73
+ 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.
76
74
 
77
- script.mailon = :error
75
+ :mailon => :error
78
76
 
79
77
  RubyCron will now only report when errors occurred during the run. Other options are :none, :warning and :all (default).
80
78
 
@@ -82,35 +80,84 @@ RubyCron will now only report when errors occurred during the run. Other options
82
80
 
83
81
  Of course. Use
84
82
 
85
- script.mailfrom = 'root@doe.com'
83
+ :mailfrom => 'root@doe.com'
86
84
 
87
85
  to change the From:-header.
88
86
 
87
+ ### Do I really have to configure a local smtp server to send mail?
88
+
89
+ No. You can use other smtp servers for delivery like so:
90
+
91
+ smtpsettings = { :address => 'smtp.gmail.com',
92
+ :port => 587,
93
+ :domain => 'your.host.name',
94
+ :user_name => '<username>',
95
+ :password => '<password>',
96
+ :authentication => 'plain',
97
+ :enable_starttls_auto => true }
98
+
99
+ rcj = RubyCronJob.new( :author => 'John Doe',
100
+ :name => 'test',
101
+ :mailto => 'john@doe.com',
102
+ :mailfrom => 'root@doe.com',
103
+ :smtpsettings => smtpsettings )
104
+
89
105
  ### I want my cronjob to stop running when there are errors.
90
106
 
91
107
  No problem. You can configure this behavior with
92
108
 
93
- script.exiton = :all
109
+ :exiton => :all
94
110
 
95
111
  Valid values are :none, :warning, :error, :all.
96
112
 
113
+ ### Is there a way to manipulate the content of the email reports?
114
+
115
+ There sure is. Simply write your own ERB template, and tell the RubyCronJob about it with the :template directive.
116
+
117
+ rcj = RubyCronJob.new(
118
+ :author => 'John Doe',
119
+ :name => 'test',
120
+ :mailto => 'john@doe.com',
121
+ :mailfrom => 'root@doe.com',
122
+ :template => 'my_template.erb' )
123
+
124
+ Note that from inside the ERB template (my_template.erb in the above example) you have access to the @warnings and @errors arrays.
125
+
97
126
  ### May I please see some output while I'm developing my cronjob?
98
127
 
99
128
  Output to stdout and stderr can be very useful when debugging your cronjob. Just set the verbose flag to true:
100
129
 
101
- script.verbose = true
130
+ :verbose => true
102
131
 
103
132
  ### As a sysadmin, I like grepping through files. Can I have a log file please?
104
133
 
105
134
  Yes. Set a file path in RubyCronJob's logfile variable, and all output will be redirected to file:
106
135
 
107
- script.logfile = '/tmp/rcjlogfile'
136
+ :logfile => '/tmp/rcjlogfile'
108
137
 
109
138
  Note that you will still receive email reports when you enable file logging.
110
139
 
140
+ ### I like all these different options, but can't I set some of them globally for all my cronjobs?
141
+
142
+ 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:
143
+
144
+ rcj = RubyCronJob.new( :configfile => "my_config_file.yml" )
145
+
146
+ Or this:
147
+
148
+ rcj = RubyCronJob.new( :configurl => "http://www.foo.bar/my_config.yml")
149
+
150
+ Or even a combination:
151
+
152
+ rcj = RubyCronJob.new( :configfile => "my_config_file.yml",
153
+ :configurl => "http://www.foo.bar/my_config.yml",
154
+ :author => 'John Doe' )
155
+
156
+ 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.
157
+
111
158
  ## License
112
159
 
113
- Copyright (c) 2011, Bart Kamphorst
160
+ Copyright (c) 2011 - 2012, Bart Kamphorst
114
161
 
115
162
  (Modified BSD License)
116
163
 
data/bin/rcjrunner.rb CHANGED
@@ -1,11 +1,11 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'rubygems'
4
- require 'RubyCron'
4
+ require 'rubycron'
5
5
  include RubyCron
6
6
 
7
- if ARGV.empty? || ARGV.size > 1
8
- $stderr.puts "## Usage: rcjrunner.rb <rubycronjob>"
9
- else
7
+ if ARGV[0]
10
8
  instance_eval File.read(ARGV[0])
9
+ else
10
+ $stderr.puts "## Usage: rcjrunner.rb <rubycronjob>"
11
11
  end
data/lib/rubycron.rb CHANGED
@@ -1,4 +1,4 @@
1
- # Copyright (c) Bart Kamphorst <rubycron@kamphorst.com>, 2011
1
+ # Copyright (c) Bart Kamphorst <rubycron@kamphorst.com>, 2011 - 2012.
2
2
  # Licensed under the modified BSD License. All rights reserved.
3
3
 
4
4
  module RubyCron
@@ -6,36 +6,88 @@ module RubyCron
6
6
  class RubyCronJob
7
7
 
8
8
  require 'net/smtp'
9
+ require 'yaml'
10
+ require 'open-uri'
9
11
  require 'rubygems'
10
12
  require 'mail'
11
13
  require 'erb'
12
14
 
13
- attr_accessor :name, :author, :mailto, :mailfrom, :mailsubject, :mailon, :exiton, :logfile, :verbose
15
+ attr_accessor :name, :author, :mailto, :mailfrom, :mailsubject, :mailon, :exiton, :template, :smtpsettings, :logfile, :verbose
16
+ attr_reader :warnings, :errors
14
17
 
15
- def initialize(&block)
18
+ def initialize(args = nil)
16
19
  @warnings, @errors = [], []
17
-
18
- instance_eval(&block)
19
- terminate("Cannot connect to local smtp server.") unless smtp_connection?
20
- terminate("This job has no name.") unless self.name
21
- terminate("This job has no author.") unless self.author
22
- terminate("No To: header was set. ") unless self.mailto
23
20
 
24
- self.mailfrom ||= 'root@localhost'
25
- self.verbose ||= false
26
- self.mailon = :all unless self.mailon && [:none, :warning, :error, :all].include?(self.mailon)
27
- self.exiton = :all unless self.exiton && [:none, :warning, :error, :all].include?(self.exiton)
28
-
29
- if self.logfile
30
- $stdout.reopen(self.logfile, "a")
31
- $stdout.sync = true
32
- $stderr.reopen($stdout)
21
+ case args
22
+ when NilClass then yield self if block_given?
23
+ when Proc then instance_eval(args)
24
+ when Hash then
25
+
26
+ args = load_config(:file, args[:configfile]).merge(args) if args[:configfile]
27
+ args = load_config(:url, args[:configurl]).merge(args) if args[:configurl]
28
+
29
+ args.each do |key, value|
30
+ instance_variable_set("@#{key}", value) if value
31
+ end
32
+ else terminate "Expected a hash or a block to initialize, but instead received a #{args.class} object."
33
33
  end
34
+
35
+ check_sanity
36
+
34
37
  rescue => e
35
- $stdout = STDOUT
36
38
  terminate(e.message)
37
39
  end
38
40
 
41
+ def load_config(source_type, source)
42
+ if source_type == :file
43
+ io = File.open(source) if File.file?(source)
44
+ elsif source_type == :url
45
+ io = open(source)
46
+ end
47
+ yml = YAML::load(io)
48
+ return yml if yml.is_a?(Hash)
49
+ return {}
50
+ end
51
+
52
+ def check_sanity
53
+
54
+ raise "This job has no name." unless @name
55
+ raise "This job has no author." unless @author
56
+ raise "No To: header was set. " unless @mailto
57
+
58
+ check_smtp_settings
59
+ set_defaults
60
+ enable_file_logging if @logfile
61
+
62
+ end
63
+
64
+ def check_smtp_settings
65
+ if @smtpsettings
66
+ raise "SMTP settings have to be passed in as a hash." unless @smtpsettings.instance_of?(Hash)
67
+ raise "SMTP settings should include at least an address (:address)." unless @smtpsettings.keys.include?(:address)
68
+ raise "SMTP settings should include at least a port number (:port)." unless @smtpsettings.keys.include?(:port)
69
+ else
70
+ raise "Cannot connect to local smtp server." unless smtp_connection?
71
+ end
72
+ end
73
+
74
+ def set_defaults
75
+ @mailfrom ||= 'root@localhost'
76
+ @verbose ||= false
77
+ @template ||= File.join(File.dirname(__FILE__), '/report.erb')
78
+ @mailon = :all unless self.mailon && [:none, :warning, :error, :all].include?(self.mailon)
79
+ @exiton = :all unless self.exiton && [:none, :warning, :error, :all].include?(self.exiton)
80
+ end
81
+
82
+ def enable_file_logging
83
+ $stdout.reopen(@logfile, "a")
84
+ $stdout.sync = true
85
+ $stderr.reopen($stdout)
86
+ rescue => e
87
+ $stdout = STDOUT
88
+ raise e
89
+ end
90
+
39
91
  def terminate(message)
40
92
  $stderr.puts "## Cannot complete job. Reason: #{message}"
41
93
  exit 1
@@ -85,12 +137,19 @@ module RubyCron
85
137
  # an erb template file, and mikel's excellent mail gem.
86
138
  private
87
139
  def report
88
- self.mailsubject = "Cron report for #{name}: #{@warnings.size} warnings & #{@errors.size} errors" unless self.mailsubject
89
- mailfrom = self.mailfrom
90
- mailto = self.mailto
91
- mailsubject = self.mailsubject
92
- mailbody = ERB.new(File.read(File.join(File.dirname(__FILE__), '/report.erb'))).result(binding)
140
+ @mailsubject = "Cron report for #{name}: #{@warnings.size} warnings & #{@errors.size} errors" unless @mailsubject
141
+ mailfrom = @mailfrom
142
+ mailto = @mailto
143
+ mailsubject = @mailsubject
144
+ mailbody = ERB.new(File.read(@template)).result(binding)
93
145
 
146
+ if @smtpsettings
147
+ smtpsettings = @smtpsettings
148
+ Mail.defaults do
149
+ delivery_method :smtp, smtpsettings
150
+ end
151
+ end
152
+
94
153
  mail = Mail.new do
95
154
  from mailfrom
96
155
  to mailto
data/sample/test.rcj CHANGED
@@ -1,18 +1,17 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'rubygems'
4
- require 'RubyCron'
4
+ require 'rubycron'
5
5
  include RubyCron
6
6
 
7
- rcj = RubyCronJob.new do |script|
8
- script.author = 'John Doe'
9
- script.name = 'test'
10
- script.mailto = 'john@doe.com'
11
- script.mailfrom = 'root@doe.com'
12
- script.mailon = :all
13
- script.exiton = :none
14
- script.verbose = false
15
- end
7
+ rcj = RubyCronJob.new(
8
+ :author => 'John Doe',
9
+ :name => 'test',
10
+ :mailto => 'john@doe.com',
11
+ :mailfrom => 'root@doe.com',
12
+ :mailon => :all,
13
+ :exiton => :none,
14
+ :verbose => false )
16
15
 
17
16
  rcj.execute do
18
17
  unless File.directory?('/tmp')
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.1'
4
+ version: '0.2'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-19 00:00:00.000000000 +02:00
13
- default_executable:
12
+ date: 2012-01-27 00:00:00.000000000Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: mail
17
- requirement: &2160625260 !ruby/object:Gem::Requirement
16
+ requirement: &2153384820 !ruby/object:Gem::Requirement
18
17
  none: false
19
18
  requirements:
20
19
  - - ! '>='
@@ -22,7 +21,7 @@ dependencies:
22
21
  version: '0'
23
22
  type: :runtime
24
23
  prerelease: false
25
- version_requirements: *2160625260
24
+ version_requirements: *2153384820
26
25
  description: Write clean cronjobs in Ruby, and get reporting for free!
27
26
  email: rubycron@kamphorst.com
28
27
  executables: []
@@ -36,7 +35,6 @@ files:
36
35
  - lib/report.erb
37
36
  - sample/test.rcj
38
37
  - bin/rcjrunner.rb
39
- has_rdoc: true
40
38
  homepage: https://github.com/bartkamphorst/rubycron
41
39
  licenses: []
42
40
  post_install_message:
@@ -57,7 +55,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
55
  version: '0'
58
56
  requirements: []
59
57
  rubyforge_project:
60
- rubygems_version: 1.6.2
58
+ rubygems_version: 1.8.15
61
59
  signing_key:
62
60
  specification_version: 3
63
61
  summary: Simplifies your Ruby cronjobs by automating the reporting process.