whenever 0.5.0 → 0.6.2

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.
@@ -0,0 +1,58 @@
1
+ module Whenever
2
+ module Output
3
+ class Redirection
4
+
5
+ def initialize(output)
6
+ @output = output
7
+ end
8
+
9
+ def to_s
10
+ return '' unless defined?(@output)
11
+ case @output
12
+ when String then redirect_from_string
13
+ when Hash then redirect_from_hash
14
+ when NilClass then ">> /dev/null 2>&1"
15
+ else ''
16
+ end
17
+ end
18
+
19
+ protected
20
+
21
+ def stdout
22
+ return unless @output.has_key?(:standard)
23
+ @output[:standard].nil? ? '/dev/null' : @output[:standard]
24
+ end
25
+
26
+ def stderr
27
+ return unless @output.has_key?(:error)
28
+ @output[:error].nil? ? '/dev/null' : @output[:error]
29
+ end
30
+
31
+ def redirect_from_hash
32
+ case
33
+ when stdout == '/dev/null' && stderr == '/dev/null'
34
+ "> /dev/null 2>&1"
35
+ when stdout && stderr == '/dev/null'
36
+ ">> #{stdout} 2> /dev/null"
37
+ when stdout && stderr
38
+ ">> #{stdout} 2>> #{stderr}"
39
+ when stderr == '/dev/null'
40
+ "2> /dev/null"
41
+ when stderr
42
+ "2>> #{stderr}"
43
+ when stdout == '/dev/null'
44
+ "> /dev/null"
45
+ when stdout
46
+ ">> #{stdout}"
47
+ else
48
+ ''
49
+ end
50
+ end
51
+
52
+ def redirect_from_string
53
+ ">> #{@output} 2>&1"
54
+ end
55
+
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,18 @@
1
+ # Environemnt defaults to production
2
+ set :environment, "production"
3
+ # Path defaults to the directory `whenever` was run from
4
+ set :path, Whenever.path
5
+
6
+ # All jobs are wrapped in this template.
7
+ # http://blog.scoutapp.com/articles/2010/09/07/rvm-and-cron-in-production
8
+ set :job_template, "/bin/bash -l -c ':job'"
9
+
10
+ job_type :command, ":task :output"
11
+ job_type :rake, "cd :path && RAILS_ENV=:environment rake :task --silent :output"
12
+
13
+ # Create a runner job that's appropriate for the Rails version,
14
+ if File.exists?(File.join(path, 'script', 'rails'))
15
+ job_type :runner, "cd :path && script/rails runner -e :environment ':task' :output"
16
+ else
17
+ job_type :runner, "cd :path && script/runner -e :environment ':task' :output"
18
+ end
@@ -1,3 +1,3 @@
1
1
  module Whenever
2
- VERSION = '0.5.0'
2
+ VERSION = '0.6.2'
3
3
  end unless defined?(Whenever::VERSION)
data/lib/whenever.rb CHANGED
@@ -1,20 +1,21 @@
1
1
  require 'chronic'
2
+ require 'active_support/all'
2
3
 
3
- # It was previously defined as a dependency of this gem, but that became
4
- # problematic. See: http://github.com/javan/whenever/issues#issue/1
5
- begin
6
- require 'active_support/all'
7
- rescue LoadError
8
- warn 'To use Whenever you need the active_support gem:'
9
- warn '$ gem install activesupport'
10
- exit(1)
11
- end
12
-
13
- # Whenever files
14
- require 'whenever/base'
15
4
  require 'whenever/job_list'
16
5
  require 'whenever/job'
17
- require 'whenever/outputs/cron'
18
- require 'whenever/outputs/cron/output_redirection'
6
+ require 'whenever/cron'
7
+ require 'whenever/output_redirection'
19
8
  require 'whenever/command_line'
20
- require 'whenever/version'
9
+ require 'whenever/version'
10
+
11
+ module Whenever
12
+
13
+ def self.cron(options)
14
+ Whenever::JobList.new(options).generate_cron_output
15
+ end
16
+
17
+ def self.path
18
+ Dir.pwd
19
+ end
20
+
21
+ end
@@ -0,0 +1,310 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2
+
3
+ class CommandLineTest < Test::Unit::TestCase
4
+
5
+ context "A command line write" do
6
+ setup do
7
+ File.expects(:exists?).with('config/schedule.rb').returns(true)
8
+ @command = Whenever::CommandLine.new(:write => true, :identifier => 'My identifier')
9
+ @task = "#{two_hours} /my/command"
10
+ Whenever.expects(:cron).returns(@task)
11
+ end
12
+
13
+ should "output the cron job with identifier blocks" do
14
+ output = <<-EXPECTED
15
+ # Begin Whenever generated tasks for: My identifier
16
+ #{@task}
17
+ # End Whenever generated tasks for: My identifier
18
+ EXPECTED
19
+
20
+ assert_equal output, @command.send(:whenever_cron)
21
+ end
22
+
23
+ should "write the crontab when run" do
24
+ @command.expects(:write_crontab).returns(true)
25
+ assert @command.run
26
+ end
27
+ end
28
+
29
+ context "A command line update" do
30
+ setup do
31
+ File.expects(:exists?).with('config/schedule.rb').returns(true)
32
+ @command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier')
33
+ @task = "#{two_hours} /my/command"
34
+ Whenever.expects(:cron).returns(@task)
35
+ end
36
+
37
+ should "add the cron to the end of the file if there is no existing identifier block" do
38
+ existing = '# Existing crontab'
39
+ @command.expects(:read_crontab).at_least_once.returns(existing)
40
+
41
+ new_cron = <<-EXPECTED
42
+ #{existing}
43
+
44
+ # Begin Whenever generated tasks for: My identifier
45
+ #{@task}
46
+ # End Whenever generated tasks for: My identifier
47
+ EXPECTED
48
+
49
+ assert_equal new_cron, @command.send(:updated_crontab)
50
+
51
+ @command.expects(:write_crontab).with(new_cron).returns(true)
52
+ assert @command.run
53
+ end
54
+
55
+ should "replace an existing block if the identifier matches" do
56
+ existing = <<-EXISTING_CRON
57
+ # Something
58
+
59
+ # Begin Whenever generated tasks for: My identifier
60
+ My whenever job that was already here
61
+ # End Whenever generated tasks for: My identifier
62
+
63
+ # Begin Whenever generated tasks for: Other identifier
64
+ This shouldn't get replaced
65
+ # End Whenever generated tasks for: Other identifier
66
+ EXISTING_CRON
67
+
68
+ new_cron = <<-NEW_CRON
69
+ # Something
70
+
71
+ # Begin Whenever generated tasks for: My identifier
72
+ #{@task}
73
+ # End Whenever generated tasks for: My identifier
74
+
75
+ # Begin Whenever generated tasks for: Other identifier
76
+ This shouldn't get replaced
77
+ # End Whenever generated tasks for: Other identifier
78
+ NEW_CRON
79
+
80
+ @command.expects(:read_crontab).at_least_once.returns(existing)
81
+ assert_equal new_cron, @command.send(:updated_crontab)
82
+
83
+ @command.expects(:write_crontab).with(new_cron).returns(true)
84
+ assert @command.run
85
+ end
86
+ end
87
+
88
+ context "A command line update that contains backslashes" do
89
+ setup do
90
+ @existing = <<-EXISTING_CRON
91
+ # Begin Whenever generated tasks for: My identifier
92
+ script/runner -e production 'puts '\\''hello'\\'''
93
+ # End Whenever generated tasks for: My identifier
94
+ EXISTING_CRON
95
+ File.expects(:exists?).with('config/schedule.rb').returns(true)
96
+ @command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier')
97
+ @command.expects(:read_crontab).at_least_once.returns(@existing)
98
+ @command.expects(:whenever_cron).returns(@existing)
99
+ end
100
+
101
+ should "replace the existing block with the backslashes in tact" do
102
+ assert_equal @existing, @command.send(:updated_crontab)
103
+ end
104
+ end
105
+
106
+ context "A command line update with an identifier similar to an existing one in the crontab already" do
107
+ setup do
108
+ @existing = <<-EXISTING_CRON
109
+ # Begin Whenever generated tasks for: WheneverExisting
110
+ # End Whenever generated tasks for: WheneverExisting
111
+ EXISTING_CRON
112
+ @new = <<-NEW_CRON
113
+ # Begin Whenever generated tasks for: Whenever
114
+ # End Whenever generated tasks for: Whenever
115
+ NEW_CRON
116
+ File.expects(:exists?).with('config/schedule.rb').returns(true)
117
+ @command = Whenever::CommandLine.new(:update => true, :identifier => 'Whenever')
118
+ @command.expects(:read_crontab).at_least_once.returns(@existing)
119
+ @command.expects(:whenever_cron).returns(@new)
120
+ end
121
+
122
+ should "append the similarly named command" do
123
+ assert_equal @existing + "\n" + @new, @command.send(:updated_crontab)
124
+ end
125
+ end
126
+
127
+ context "A command line clear" do
128
+ setup do
129
+ File.expects(:exists?).with('config/schedule.rb').returns(true)
130
+ @command = Whenever::CommandLine.new(:clear => true, :identifier => 'My identifier')
131
+ @task = "#{two_hours} /my/command"
132
+ end
133
+
134
+ should "clear an existing block if the identifier matches" do
135
+ existing = <<-EXISTING_CRON
136
+ # Something
137
+
138
+ # Begin Whenever generated tasks for: My identifier
139
+ My whenever job that was already here
140
+ # End Whenever generated tasks for: My identifier
141
+
142
+ # Begin Whenever generated tasks for: Other identifier
143
+ This shouldn't get replaced
144
+ # End Whenever generated tasks for: Other identifier
145
+ EXISTING_CRON
146
+
147
+ @command.expects(:read_crontab).at_least_once.returns(existing)
148
+
149
+ new_cron = <<-NEW_CRON
150
+ # Something
151
+
152
+ # Begin Whenever generated tasks for: Other identifier
153
+ This shouldn't get replaced
154
+ # End Whenever generated tasks for: Other identifier
155
+ NEW_CRON
156
+
157
+ assert_equal new_cron, @command.send(:updated_crontab)
158
+
159
+ @command.expects(:write_crontab).with(new_cron).returns(true)
160
+ assert @command.run
161
+ end
162
+ end
163
+
164
+ context "A command line update with no identifier" do
165
+ setup do
166
+ File.expects(:exists?).with('config/schedule.rb').returns(true)
167
+ Whenever::CommandLine.any_instance.expects(:default_identifier).returns('DEFAULT')
168
+ @command = Whenever::CommandLine.new(:update => true, :file => @file)
169
+ end
170
+
171
+ should "use the default identifier" do
172
+ assert_equal "Whenever generated tasks for: DEFAULT", @command.send(:comment_base)
173
+ end
174
+ end
175
+
176
+ context "combined params" do
177
+ setup do
178
+ Whenever::CommandLine.any_instance.expects(:exit)
179
+ Whenever::CommandLine.any_instance.expects(:warn)
180
+ File.expects(:exists?).with('config/schedule.rb').returns(true)
181
+ end
182
+
183
+ should "exit with write and clear" do
184
+ @command = Whenever::CommandLine.new(:write => true, :clear => true)
185
+ end
186
+
187
+ should "exit with write and update" do
188
+ @command = Whenever::CommandLine.new(:write => true, :update => true)
189
+ end
190
+
191
+ should "exit with update and clear" do
192
+ @command = Whenever::CommandLine.new(:update => true, :clear => true)
193
+ end
194
+ end
195
+
196
+ context "A runner where the environment is overridden using the :set option" do
197
+ setup do
198
+ @output = Whenever.cron :set => 'environment=serious', :string => \
199
+ <<-file
200
+ set :job_template, nil
201
+ set :environment, :silly
202
+ set :path, '/my/path'
203
+ every 2.hours do
204
+ runner "blahblah"
205
+ end
206
+ file
207
+ end
208
+
209
+ should "output the runner using the override environment" do
210
+ assert_match two_hours + %( cd /my/path && script/runner -e serious 'blahblah'), @output
211
+ end
212
+ end
213
+
214
+ context "A runner where the environment and path are overridden using the :set option" do
215
+ setup do
216
+ @output = Whenever.cron :set => 'environment=serious&path=/serious/path', :string => \
217
+ <<-file
218
+ set :job_template, nil
219
+ set :environment, :silly
220
+ set :path, '/silly/path'
221
+ every 2.hours do
222
+ runner "blahblah"
223
+ end
224
+ file
225
+ end
226
+
227
+ should "output the runner using the overridden path and environment" do
228
+ assert_match two_hours + %( cd /serious/path && script/runner -e serious 'blahblah'), @output
229
+ end
230
+ end
231
+
232
+ context "A runner where the environment and path are overridden using the :set option with spaces in the string" do
233
+ setup do
234
+ @output = Whenever.cron :set => ' environment = serious& path =/serious/path', :string => \
235
+ <<-file
236
+ set :job_template, nil
237
+ set :environment, :silly
238
+ set :path, '/silly/path'
239
+ every 2.hours do
240
+ runner "blahblah"
241
+ end
242
+ file
243
+ end
244
+
245
+ should "output the runner using the overridden path and environment" do
246
+ assert_match two_hours + %( cd /serious/path && script/runner -e serious 'blahblah'), @output
247
+ end
248
+ end
249
+
250
+ context "A runner where the environment is overridden using the :set option but no value is given" do
251
+ setup do
252
+ @output = Whenever.cron :set => ' environment=', :string => \
253
+ <<-file
254
+ set :job_template, nil
255
+ set :environment, :silly
256
+ set :path, '/silly/path'
257
+ every 2.hours do
258
+ runner "blahblah"
259
+ end
260
+ file
261
+ end
262
+
263
+ should "output the runner using the original environmnet" do
264
+ assert_match two_hours + %( cd /silly/path && script/runner -e silly 'blahblah'), @output
265
+ end
266
+ end
267
+
268
+ context "prepare-ing the output" do
269
+ setup do
270
+ File.expects(:exists?).with('config/schedule.rb').returns(true)
271
+ end
272
+
273
+ should "not trim off the top lines of the file" do
274
+ @command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier', :cut => 0)
275
+ existing = <<-EXISTING_CRON
276
+ # Useless Comments
277
+ # at the top of the file
278
+
279
+ # Begin Whenever generated tasks for: My identifier
280
+ My whenever job that was already here
281
+ # End Whenever generated tasks for: My identifier
282
+ EXISTING_CRON
283
+
284
+ # here-doc adds an extra newline we need removed
285
+ assert_equal existing.strip, @command.send(:prepare, existing)
286
+ end
287
+
288
+ should "trim off the top lines of the file" do
289
+ @command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier', :cut => '3')
290
+ existing = <<-EXISTING_CRON
291
+ # Useless Comments
292
+ # at the top of the file
293
+
294
+ # Begin Whenever generated tasks for: My identifier
295
+ My whenever job that was already here
296
+ # End Whenever generated tasks for: My identifier
297
+ EXISTING_CRON
298
+
299
+ new_cron = <<-NEW_CRON
300
+ # Begin Whenever generated tasks for: My identifier
301
+ My whenever job that was already here
302
+ # End Whenever generated tasks for: My identifier
303
+ NEW_CRON
304
+
305
+ # here-doc adds an extra newline we need removed
306
+ assert_equal new_cron.strip, @command.send(:prepare, existing)
307
+ end
308
+ end
309
+
310
+ end
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + "/test_helper")
1
+ require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2
2
 
3
3
  class OutputAtTest < Test::Unit::TestCase
4
4
 
@@ -6,6 +6,7 @@ class OutputAtTest < Test::Unit::TestCase
6
6
  setup do
7
7
  @output = Whenever.cron \
8
8
  <<-file
9
+ set :job_template, nil
9
10
  every "weekday", :at => '5:02am' do
10
11
  command "blahblah"
11
12
  end
@@ -21,6 +22,7 @@ class OutputAtTest < Test::Unit::TestCase
21
22
  setup do
22
23
  @output = Whenever.cron \
23
24
  <<-file
25
+ set :job_template, nil
24
26
  every "weekday", :at => %w(5:02am 3:52pm) do
25
27
  command "blahblah"
26
28
  end
@@ -37,6 +39,7 @@ class OutputAtTest < Test::Unit::TestCase
37
39
  setup do
38
40
  @output = Whenever.cron \
39
41
  <<-file
42
+ set :job_template, nil
40
43
  every "weekday", :at => '5:02am, 3:52pm' do
41
44
  command "blahblah"
42
45
  end
@@ -53,6 +56,7 @@ class OutputAtTest < Test::Unit::TestCase
53
56
  setup do
54
57
  @output = Whenever.cron \
55
58
  <<-file
59
+ set :job_template, nil
56
60
  every "weekday", :at => '5:02am, 3:02pm' do
57
61
  command "blahblah"
58
62
  end
@@ -68,6 +72,7 @@ class OutputAtTest < Test::Unit::TestCase
68
72
  setup do
69
73
  @output = Whenever.cron \
70
74
  <<-file
75
+ set :job_template, nil
71
76
  every "mon,wed,fri", :at => '5:02am, 3:02pm' do
72
77
  command "blahblah"
73
78
  end
@@ -83,6 +88,7 @@ class OutputAtTest < Test::Unit::TestCase
83
88
  setup do
84
89
  @output = Whenever.cron \
85
90
  <<-file
91
+ set :job_template, nil
86
92
  set :path, '/your/path'
87
93
  every "mon,wed,fri", :at => '5:02am, 3:02pm' do
88
94
  runner "blahblah"
@@ -91,7 +97,7 @@ class OutputAtTest < Test::Unit::TestCase
91
97
  end
92
98
 
93
99
  should "output the runner using one entry because the times are aligned" do
94
- assert_match '2 5,15 * * 1,3,5 cd /your/path && script/runner -e production "blahblah"', @output
100
+ assert_match %(2 5,15 * * 1,3,5 cd /your/path && script/runner -e production 'blahblah'), @output
95
101
  end
96
102
  end
97
103
 
@@ -99,6 +105,7 @@ class OutputAtTest < Test::Unit::TestCase
99
105
  setup do
100
106
  @output = Whenever.cron \
101
107
  <<-file
108
+ set :job_template, nil
102
109
  set :path, '/your/path'
103
110
  every "mon,wed,fri", :at => '5:02am, 3:02pm' do
104
111
  rake "blah:blah"
@@ -107,7 +114,7 @@ class OutputAtTest < Test::Unit::TestCase
107
114
  end
108
115
 
109
116
  should "output the rake task using one entry because the times are aligned" do
110
- assert_match '2 5,15 * * 1,3,5 cd /your/path && RAILS_ENV=production /usr/bin/env rake blah:blah', @output
117
+ assert_match '2 5,15 * * 1,3,5 cd /your/path && RAILS_ENV=production rake blah:blah --silent', @output
111
118
  end
112
119
  end
113
120
 
@@ -115,6 +122,7 @@ class OutputAtTest < Test::Unit::TestCase
115
122
  setup do
116
123
  @output = Whenever.cron \
117
124
  <<-file
125
+ set :job_template, nil
118
126
  every [1.month, 1.day], :at => 'january 5:02am, june 17th at 2:22pm, june 3rd at 3:33am' do
119
127
  command "blahblah"
120
128
  end
@@ -138,6 +146,7 @@ class OutputAtTest < Test::Unit::TestCase
138
146
  setup do
139
147
  @output = Whenever.cron \
140
148
  <<-file
149
+ set :job_template, nil
141
150
  every :reboot do
142
151
  command "command_1"
143
152
  command "command_2"
@@ -155,6 +164,7 @@ class OutputAtTest < Test::Unit::TestCase
155
164
  setup do
156
165
  @output = Whenever.cron \
157
166
  <<-file
167
+ set :job_template, nil
158
168
  set :path, '/your/path'
159
169
  every :day do
160
170
  rake "blah:blah"
@@ -167,10 +177,10 @@ class OutputAtTest < Test::Unit::TestCase
167
177
  end
168
178
 
169
179
  should "output all of the commands @daily" do
170
- assert_match '@daily cd /your/path && RAILS_ENV=production /usr/bin/env rake blah:blah', @output
171
- assert_match '@daily cd /your/path && script/runner -e production "runner_1"', @output
180
+ assert_match '@daily cd /your/path && RAILS_ENV=production rake blah:blah --silent', @output
181
+ assert_match %(@daily cd /your/path && script/runner -e production 'runner_1'), @output
172
182
  assert_match '@daily command_1', @output
173
- assert_match '@daily cd /your/path && script/runner -e production "runner_2"', @output
183
+ assert_match %(@daily cd /your/path && script/runner -e production 'runner_2'), @output
174
184
  assert_match '@daily command_2', @output
175
185
  end
176
186
  end
@@ -179,6 +189,7 @@ class OutputAtTest < Test::Unit::TestCase
179
189
  setup do
180
190
  @output = Whenever.cron \
181
191
  <<-file
192
+ set :job_template, nil
182
193
  every 5.minutes, :at => 1 do
183
194
  command "blahblah"
184
195
  end
@@ -194,6 +205,7 @@ class OutputAtTest < Test::Unit::TestCase
194
205
  setup do
195
206
  @output = Whenever.cron \
196
207
  <<-file
208
+ set :job_template, nil
197
209
  every 4.minutes, :at => 2 do
198
210
  command "blahblah"
199
211
  end
@@ -209,6 +221,7 @@ class OutputAtTest < Test::Unit::TestCase
209
221
  setup do
210
222
  @output = Whenever.cron \
211
223
  <<-file
224
+ set :job_template, nil
212
225
  every 3.minutes, :at => 7 do
213
226
  command "blahblah"
214
227
  end
@@ -224,6 +237,7 @@ class OutputAtTest < Test::Unit::TestCase
224
237
  setup do
225
238
  @output = Whenever.cron \
226
239
  <<-file
240
+ set :job_template, nil
227
241
  every 2.minutes, :at => 27 do
228
242
  command "blahblah"
229
243
  end