le1t0-whenever 0.4.2.003 → 0.6.2.001
Sign up to get free protection for your applications and to get access to all the features.
- data/{CHANGELOG.rdoc → CHANGELOG.md} +55 -18
- data/README.md +139 -0
- data/Rakefile +9 -6
- data/bin/whenever +15 -5
- data/lib/whenever.rb +17 -27
- data/lib/whenever/capistrano.rb +31 -0
- data/lib/whenever/command_line.rb +25 -11
- data/lib/whenever/{outputs/cron.rb → cron.rb} +5 -12
- data/lib/whenever/job.rb +49 -0
- data/lib/whenever/job_list.rb +29 -42
- data/lib/whenever/output_redirection.rb +58 -0
- data/lib/whenever/setup.rb +18 -0
- data/lib/whenever/version.rb +1 -1
- data/test/functional/command_line_test.rb +310 -0
- data/test/{output_at_test.rb → functional/output_at_test.rb} +79 -6
- data/test/functional/output_default_defined_jobs_test.rb +164 -0
- data/test/functional/output_defined_job_test.rb +111 -0
- data/test/functional/output_env_test.rb +23 -0
- data/test/{output_redirection_test.rb → functional/output_redirection_test.rb} +19 -1
- data/test/test_helper.rb +5 -18
- data/test/{cron_test.rb → unit/cron_test.rb} +1 -1
- data/test/unit/job_test.rb +77 -0
- metadata +90 -37
- data/.gitignore +0 -4
- data/README.rdoc +0 -105
- data/lib/whenever/base.rb +0 -15
- data/lib/whenever/job_types/default.rb +0 -45
- data/lib/whenever/job_types/rake_task.rb +0 -12
- data/lib/whenever/job_types/runner.rb +0 -12
- data/lib/whenever/outputs/cron/output_redirection.rb +0 -60
- data/test/command_line_test.rb +0 -101
- data/test/output_command_test.rb +0 -37
- data/test/output_env_test.rb +0 -56
- data/test/output_rake_test.rb +0 -74
- data/test/output_runner_test.rb +0 -209
data/.gitignore
DELETED
data/README.rdoc
DELETED
@@ -1,105 +0,0 @@
|
|
1
|
-
== Introduction
|
2
|
-
|
3
|
-
Whenever is a Ruby gem that provides a clear syntax for defining cron jobs. It outputs valid cron syntax and can even write your crontab file for you. It is designed to work well with Rails applications and can be deployed with Capistrano. Whenever works fine independently as well.
|
4
|
-
|
5
|
-
Ryan Bates created a great Railscast about Whenever: http://railscasts.com/episodes/164-cron-in-ruby
|
6
|
-
|
7
|
-
Discussion: http://groups.google.com/group/whenever-gem
|
8
|
-
|
9
|
-
== Installation
|
10
|
-
|
11
|
-
$ sudo gem install whenever
|
12
|
-
|
13
|
-
== Getting started
|
14
|
-
|
15
|
-
$ cd /my/rails/app
|
16
|
-
$ wheneverize .
|
17
|
-
|
18
|
-
This will create an initial "config/schedule.rb" file you.
|
19
|
-
|
20
|
-
== Example schedule.rb file
|
21
|
-
|
22
|
-
every 3.hours do
|
23
|
-
runner "MyModel.some_process"
|
24
|
-
rake "my:rake:task"
|
25
|
-
command "/usr/bin/my_great_command"
|
26
|
-
end
|
27
|
-
|
28
|
-
every 1.day, :at => '4:30 am' do
|
29
|
-
runner "MyModel.task_to_run_at_four_thirty_in_the_morning"
|
30
|
-
end
|
31
|
-
|
32
|
-
every :hour do # Many shortcuts available: :hour, :day, :month, :year, :reboot
|
33
|
-
runner "SomeModel.ladeeda"
|
34
|
-
end
|
35
|
-
|
36
|
-
every :sunday, :at => '12pm' do # Use any day of the week or :weekend, :weekday
|
37
|
-
runner "Task.do_something_great"
|
38
|
-
end
|
39
|
-
|
40
|
-
More examples on the wiki: http://wiki.github.com/javan/whenever/instructions-and-examples
|
41
|
-
|
42
|
-
== Cron output
|
43
|
-
|
44
|
-
$ cd /my/rails/app
|
45
|
-
$ whenever
|
46
|
-
|
47
|
-
And you'll see your schedule.rb converted to cron sytax. Note: running `whenever' with no options does not display your current crontab file, it simply shows you the output of converting your schedule.rb file.
|
48
|
-
|
49
|
-
== Capistrano integration
|
50
|
-
|
51
|
-
In your "config/deploy.rb" file do something like:
|
52
|
-
|
53
|
-
after "deploy:symlink", "deploy:update_crontab"
|
54
|
-
|
55
|
-
namespace :deploy do
|
56
|
-
desc "Update the crontab file"
|
57
|
-
task :update_crontab, :roles => :db do
|
58
|
-
run "cd #{release_path} && whenever --update-crontab #{application}"
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
This will update your crontab file, leaving any existing entries unharmed. When using the <code>--update-crontab</code> option, Whenever will only update the entries in your crontab file related to the current schedule.rb file. You can replace the <code>#{application}</code> with any identifying string you'd like. You can have any number of apps deploy to the same crontab file peacefully given they each use a different identifier.
|
63
|
-
|
64
|
-
If you wish to simply overwrite your crontab file each time you deploy, use the <code>--write-crontab</code> option. This is ideal if you are only working with one app and every crontab entry is contained in a single schedule.rb file.
|
65
|
-
|
66
|
-
By mixing and matching the <code>--load-file</code> and <code>--user</code> options with your various :roles in Capistrano it is entirely possible to deploy different crontab schedules under different users to all your various servers. Get creative!
|
67
|
-
|
68
|
-
If you want to override a variable (like your environment) at the time of deployment you can do so with the <code>--set</code> option: http://wiki.github.com/javan/whenever/setting-variables-on-the-fly
|
69
|
-
|
70
|
-
== Credit
|
71
|
-
|
72
|
-
Whenever was created for use at Inkling (http://inklingmarkets.com) where I work. Their take on it: http://blog.inklingmarkets.com/2009/02/whenever-easy-way-to-do-cron-jobs-from.html
|
73
|
-
|
74
|
-
While building Whenever, I learned a lot by digging through the source code of Capistrano - http://github.com/jamis/capistrano
|
75
|
-
|
76
|
-
== Discussion / Feedback / Issues / Bugs
|
77
|
-
|
78
|
-
For general discussion and questions, please use the google group: http://groups.google.com/group/whenever-gem
|
79
|
-
|
80
|
-
If you've found a genuine bug or issue, please use the Issues section on github: http://github.com/javan/whenever/issues
|
81
|
-
|
82
|
-
== License
|
83
|
-
|
84
|
-
Copyright (c) 2009+ Javan Makhmali
|
85
|
-
|
86
|
-
Permission is hereby granted, free of charge, to any person
|
87
|
-
obtaining a copy of this software and associated documentation
|
88
|
-
files (the "Software"), to deal in the Software without
|
89
|
-
restriction, including without limitation the rights to use,
|
90
|
-
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
91
|
-
copies of the Software, and to permit persons to whom the
|
92
|
-
Software is furnished to do so, subject to the following
|
93
|
-
conditions:
|
94
|
-
|
95
|
-
The above copyright notice and this permission notice shall be
|
96
|
-
included in all copies or substantial portions of the Software.
|
97
|
-
|
98
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
99
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
100
|
-
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
101
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
102
|
-
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
103
|
-
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
104
|
-
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
105
|
-
OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/whenever/base.rb
DELETED
@@ -1,45 +0,0 @@
|
|
1
|
-
module Whenever
|
2
|
-
module Job
|
3
|
-
class Default
|
4
|
-
|
5
|
-
attr_accessor :task, :at, :output, :output_redirection
|
6
|
-
|
7
|
-
def initialize(options = {})
|
8
|
-
opts = options.clone
|
9
|
-
@task = opts.delete(:task)
|
10
|
-
@at = opts.delete(:at)
|
11
|
-
@output_redirection = opts.delete(:output) || :not_set
|
12
|
-
@environment = opts.delete(:environment) || :production
|
13
|
-
@path = opts.delete(:path) || Whenever.path
|
14
|
-
@envvars = envvars(opts)
|
15
|
-
end
|
16
|
-
|
17
|
-
def output
|
18
|
-
envvars + task
|
19
|
-
end
|
20
|
-
|
21
|
-
def envvars(opts=nil)
|
22
|
-
return @envvars if opts.nil?
|
23
|
-
env_vars = opts
|
24
|
-
if env_vars.is_a?(Hash)
|
25
|
-
env_vars.keys.select do |key|
|
26
|
-
key.to_sym != :RAILS_ENV && key.to_s =~ /^[A-Z_]+$/
|
27
|
-
end.collect do |key|
|
28
|
-
"#{key}=#{env_vars[key]}"
|
29
|
-
end.join(' ') + ' '
|
30
|
-
elsif env_vars.is_a?(Array)
|
31
|
-
env_vars.join(' ') + ' '
|
32
|
-
else
|
33
|
-
env_vars.to_s + ' '
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
protected
|
38
|
-
|
39
|
-
def path_required
|
40
|
-
raise ArgumentError, "No path available; set :path, '/your/path' in your schedule file" if @path.blank?
|
41
|
-
end
|
42
|
-
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
@@ -1,60 +0,0 @@
|
|
1
|
-
module Whenever
|
2
|
-
module Output
|
3
|
-
class Cron
|
4
|
-
class OutputRedirection
|
5
|
-
|
6
|
-
def initialize(output)
|
7
|
-
@output = output
|
8
|
-
end
|
9
|
-
|
10
|
-
def to_s
|
11
|
-
return '' unless defined?(@output)
|
12
|
-
case @output
|
13
|
-
when String then redirect_from_string
|
14
|
-
when Hash then redirect_from_hash
|
15
|
-
when NilClass then ">> /dev/null 2>&1"
|
16
|
-
else ''
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
protected
|
21
|
-
|
22
|
-
def stdout
|
23
|
-
return unless @output.has_key?(:standard)
|
24
|
-
@output[:standard].nil? ? '/dev/null' : @output[:standard]
|
25
|
-
end
|
26
|
-
|
27
|
-
def stderr
|
28
|
-
return unless @output.has_key?(:error)
|
29
|
-
@output[:error].nil? ? '/dev/null' : @output[:error]
|
30
|
-
end
|
31
|
-
|
32
|
-
def redirect_from_hash
|
33
|
-
case
|
34
|
-
when stdout == '/dev/null' && stderr == '/dev/null'
|
35
|
-
"> /dev/null 2>&1"
|
36
|
-
when stdout && stderr == '/dev/null'
|
37
|
-
">> #{stdout} 2> /dev/null"
|
38
|
-
when stdout && stderr
|
39
|
-
">> #{stdout} 2>> #{stderr}"
|
40
|
-
when stderr == '/dev/null'
|
41
|
-
"2> /dev/null"
|
42
|
-
when stderr
|
43
|
-
"2>> #{stderr}"
|
44
|
-
when stdout == '/dev/null'
|
45
|
-
"> /dev/null"
|
46
|
-
when stdout
|
47
|
-
">> #{stdout}"
|
48
|
-
else
|
49
|
-
''
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
def redirect_from_string
|
54
|
-
">> #{@output} 2>&1"
|
55
|
-
end
|
56
|
-
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
data/test/command_line_test.rb
DELETED
@@ -1,101 +0,0 @@
|
|
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
|
-
@command.expects(:read_crontab).at_least_once.returns(existing)
|
69
|
-
|
70
|
-
new_cron = <<-NEW_CRON
|
71
|
-
# Something
|
72
|
-
|
73
|
-
# Begin Whenever generated tasks for: My identifier
|
74
|
-
#{@task}
|
75
|
-
# End Whenever generated tasks for: My identifier
|
76
|
-
|
77
|
-
# Begin Whenever generated tasks for: Other identifier
|
78
|
-
This shouldn't get replaced
|
79
|
-
# End Whenever generated tasks for: Other identifier
|
80
|
-
NEW_CRON
|
81
|
-
|
82
|
-
assert_equal new_cron, @command.send(:updated_crontab)
|
83
|
-
|
84
|
-
@command.expects(:write_crontab).with(new_cron).returns(true)
|
85
|
-
assert @command.run
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
context "A command line update with no identifier" do
|
90
|
-
setup do
|
91
|
-
File.expects(:exists?).with('config/schedule.rb').returns(true)
|
92
|
-
Whenever::CommandLine.any_instance.expects(:default_identifier).returns('DEFAULT')
|
93
|
-
@command = Whenever::CommandLine.new(:update => true, :file => @file)
|
94
|
-
end
|
95
|
-
|
96
|
-
should "use the default identifier" do
|
97
|
-
assert_equal "Whenever generated tasks for: DEFAULT", @command.send(:comment_base)
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
end
|
data/test/output_command_test.rb
DELETED
@@ -1,37 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + "/test_helper")
|
2
|
-
|
3
|
-
class OutputCommandTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
context "A plain command" do
|
6
|
-
setup do
|
7
|
-
@output = Whenever.cron \
|
8
|
-
<<-file
|
9
|
-
every 2.hours do
|
10
|
-
command "blahblah"
|
11
|
-
end
|
12
|
-
file
|
13
|
-
end
|
14
|
-
|
15
|
-
should "output the command" do
|
16
|
-
assert_match /^.+ .+ .+ .+ blahblah$/, @output
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
context "An every statement with two commands in it" do
|
21
|
-
setup do
|
22
|
-
@output = Whenever.cron \
|
23
|
-
<<-file
|
24
|
-
every 1.hour do
|
25
|
-
command "first"
|
26
|
-
command "second"
|
27
|
-
end
|
28
|
-
file
|
29
|
-
end
|
30
|
-
|
31
|
-
should "output both commands" do
|
32
|
-
assert_match "0 * * * * first", @output
|
33
|
-
assert_match "0 * * * * second", @output
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
end
|
data/test/output_env_test.rb
DELETED
@@ -1,56 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + "/test_helper")
|
2
|
-
|
3
|
-
class OutputEnvTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
context "The output from Whenever with environment variables set" do
|
6
|
-
setup do
|
7
|
-
@output = Whenever.cron \
|
8
|
-
<<-file
|
9
|
-
env :MYVAR, 'blah'
|
10
|
-
env 'MAILTO', "someone@example.com"
|
11
|
-
file
|
12
|
-
end
|
13
|
-
|
14
|
-
should "output MYVAR environment variable" do
|
15
|
-
assert_match "MYVAR=blah", @output
|
16
|
-
end
|
17
|
-
|
18
|
-
should "output MAILTO environment variable" do
|
19
|
-
assert_match "MAILTO=someone@example.com", @output
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
context "No PATH environment variable set" do
|
24
|
-
setup do
|
25
|
-
Whenever::JobList.any_instance.expects(:read_path).at_least_once.returns('/usr/local/bin')
|
26
|
-
@output = Whenever.cron ""
|
27
|
-
end
|
28
|
-
|
29
|
-
should "add a PATH variable based on the user's PATH" do
|
30
|
-
assert_match "PATH=/usr/local/bin", @output
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
context "A PATH environment variable set" do
|
35
|
-
setup do
|
36
|
-
Whenever::JobList.stubs(:read_path).returns('/usr/local/bin')
|
37
|
-
@output = Whenever.cron "env :PATH, '/my/path'"
|
38
|
-
end
|
39
|
-
|
40
|
-
should "use that path and the user's PATH" do
|
41
|
-
assert_match "PATH=/my/path", @output
|
42
|
-
assert_no_match /local/, @output
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
context "No PATH set and instructed not to automatically load the user's path" do
|
47
|
-
setup do
|
48
|
-
@output = Whenever.cron "set :set_path_automatically, false"
|
49
|
-
end
|
50
|
-
|
51
|
-
should "not have a PATH set" do
|
52
|
-
assert_no_match /PATH/, @output
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
end
|