whenever 0.6.1 → 0.6.8

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/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  pkg
3
3
  doc
4
4
  .*.sw[a-z]
5
+ Gemfile.lock
@@ -1,4 +1,45 @@
1
- == 0.6.1 / October 20th, 2010
1
+ ### 0.6.8 / May 24th, 2011
2
+
3
+ * Convert most shortcuts to seconds. every :day -> every 1.day. #129 [Javan Makhmali]
4
+
5
+ * Allow commas in raw cron syntax. #130 [Marco Bergantin, Javan Makhmali]
6
+
7
+ * Output no update message as comments. #135 [Javan Makhmali]
8
+
9
+ * require 'thread' to support Rubygems >= 1.6.0. #132 [Javan Makhmali]
10
+
11
+
12
+ ### 0.6.7 / March 23rd, 2011
13
+
14
+ * Fix issue with comment block being corrupted during subsequent insertion of duplicate entries to the crontab. #123 [Jeremy (@lingmann)]
15
+
16
+ * Removed -i from default job template. #118 [Javan Makhmali]
17
+
18
+
19
+ ### 0.6.6 / March 8th, 2011
20
+
21
+ * Fix unclosed identifier bug. #119 [Javan Makhmali]
22
+
23
+
24
+ ### 0.6.5 / March 8th, 2011
25
+
26
+ * Preserve whitespace at the end of crontab file. #95 [Rich Meyers]
27
+
28
+ * Setting nil or blank environment variables now properly formats output. [T.J. VanSlyke]
29
+
30
+ * Allow raw cron sytax, added -i to bash job template, general cleanup. [Javan Makhmali]
31
+
32
+
33
+ ### 0.6.2 / October 26th, 2010
34
+
35
+ * --clear-crontab option completely removes entries. #63 [Javan Makhmali]
36
+
37
+ * Set default :environment and :path earlier in the new setup.rb (formerly job_types/default.rb). [Javan Makhmali]
38
+
39
+ * Converted README and CHANGELOG to markdown. [Javan Makhmali]
40
+
41
+
42
+ ### 0.6.1 / October 20th, 2010
2
43
 
3
44
  * Detect script/rails file and change runner to Rails 3 style if found. [Javan Makhmali]
4
45
 
@@ -7,14 +48,14 @@
7
48
  * Added a built-in Capistrano recipe. [Javan Makhmali]
8
49
 
9
50
 
10
- == 0.5.3 / September 24th, 2010
51
+ ### 0.5.3 / September 24th, 2010
11
52
 
12
53
  * Better regexes for replacing Whenever blocks in the crontab. #45 [Javan Makhmali]
13
54
 
14
55
  * Preserving backslashes when updating existing crontab. #82 [Javan Makhmali]
15
56
 
16
57
 
17
- == 0.5.2 / September 15th, 2010
58
+ ### 0.5.2 / September 15th, 2010
18
59
 
19
60
  * Quotes automatically escaped in jobs. [Jay Adkisson]
20
61
 
@@ -25,7 +66,7 @@
25
66
  * Lots of internal reorganizing; tests broken into unit and functional. [Javan Makhmali]
26
67
 
27
68
 
28
- == 0.5.0 / June 28th, 2010
69
+ ### 0.5.0 / June 28th, 2010
29
70
 
30
71
  * New job_type API for writing custom jobs. Internals use this to define command, runner, and rake. [Javan Makhmali - inspired by idlefingers (Damien)]
31
72
 
@@ -34,7 +75,7 @@
34
75
  * --clear option to remove crontab entries for a specific [identifier]. [mraidel (Michael Raidel)]
35
76
 
36
77
 
37
- == 0.4.2 / April 26th, 2010
78
+ ### 0.4.2 / April 26th, 2010
38
79
 
39
80
  * runners now cd into the app's directory and then execute. [Michael Guterl]
40
81
 
@@ -45,49 +86,49 @@
45
86
  * bugfix: comparison Time with 0 failed. #32 [Dan Hixon]
46
87
 
47
88
 
48
- == 0.4.1 / November 30th, 2009
89
+ ### 0.4.1 / November 30th, 2009
49
90
 
50
91
  * exit(0) instead of just exit to make JRuby happy. [Elan Meng]
51
92
 
52
93
  * Fixed activesupport deprecation warning by requiring active_support. #37 [Andrew Nesbitt]
53
94
 
54
95
 
55
- == 0.4.0 / October 20th, 2009
96
+ ### 0.4.0 / October 20th, 2009
56
97
 
57
98
  * New output option replaces the old cron_log option for output redirection and is much more flexible. #31 [Peer Allan]
58
99
 
59
100
  * Reorganized the lib files (http://weblog.rubyonrails.org/2009/9/1/gem-packaging-best-practices) and switched to Jeweler from Echoe.
60
101
 
61
102
 
62
- == 0.3.7 / September 4th, 2009
103
+ ### 0.3.7 / September 4th, 2009
63
104
 
64
105
  * No longer tries (and fails) to combine @shortcut jobs. #20 [Javan Makhmali]
65
106
 
66
107
 
67
- == 0.3.6 / June 15th, 2009
108
+ ### 0.3.6 / June 15th, 2009
68
109
 
69
110
  * Setting a PATH in the crontab automatically based on the user's PATH. [Javan Makhmali]
70
111
 
71
112
 
72
- == 0.3.5 / June 13th, 2009
113
+ ### 0.3.5 / June 13th, 2009
73
114
 
74
115
  * Added ability to accept lists of every's and at's and intelligently group them. (ex: every 'monday, wednesday', :at => ['3pm', '6am']). [Sam Ruby]
75
116
 
76
117
  * Fixed issue with new lines. #18 [Javan Makhmali]
77
118
 
78
- == 0.3.1 / June 25th, 2009
119
+ ### 0.3.1 / June 25th, 2009
79
120
 
80
121
  * Removed activesupport gem dependency. #1 [Javan Makhmali]
81
122
 
82
123
  * Switched to numeric days of the week for Solaris support (and probably others). #8 [Roger Ertesvåg]
83
124
 
84
125
 
85
- == 0.3.0 / June 2nd, 2009
126
+ ### 0.3.0 / June 2nd, 2009
86
127
 
87
128
  * Added ability to set variables on the fly from the command line (ex: whenever --set environment=staging). [Javan Makhmali]
88
129
 
89
130
 
90
- == 0.2.2 / April 30th, 2009
131
+ ### 0.2.2 / April 30th, 2009
91
132
 
92
133
  * Days of week jobs can now accept an :at directive (ex: every :monday, :at => '5pm'). [David Eisinger]
93
134
 
@@ -96,22 +137,22 @@
96
137
  * Raising an exception if someone tries to specify an :at with a cron shortcut (:day, :reboot, etc) so there are no false hopes. [Javan Makhmali]
97
138
 
98
139
 
99
- == 0.1.7 / March 5th, 2009
140
+ ### 0.1.7 / March 5th, 2009
100
141
 
101
142
  * Added ability to update the crontab file non-destuctively instead of only overwriting it. [Javan Makhmali -- Inspired by code submitted individually from: Tien Dung (tiendung), Tom Lea (cwninja), Kyle Maxwell (fizx), and Andrew Timberlake (andrewtimberlake) on github]
102
143
 
103
144
 
104
- == 0.1.5 / February 19th, 2009
145
+ ### 0.1.5 / February 19th, 2009
105
146
 
106
147
  * Fixed load path so Whenever's files don't conflict with anything in Rails. Thanks Ryan Koopmans. [Javan Makhmali]
107
148
 
108
149
 
109
- == 0.1.4 / February 17th, 2009
150
+ ### 0.1.4 / February 17th, 2009
110
151
 
111
152
  * Added --load-file and --user opts to whenever binary. [Javan Makhmali]
112
153
 
113
154
 
114
- == 0.1.3 / February 16th, 2009
155
+ ### 0.1.3 / February 16th, 2009
115
156
 
116
157
  * Added 'rake' helper for defining scheduled rake tasks. [Javan Makhmali]
117
158
 
@@ -124,6 +165,6 @@
124
165
  * Requiring specific gem versions: Chronic >=0.2.3 and activesupport >= 1.3.0 [Javan Makhmali]
125
166
 
126
167
 
127
- == 0.1.0 / February 15th, 2009
168
+ ### 0.1.0 / February 15th, 2009
128
169
 
129
170
  * Initial release [Javan Makhmali]
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in newever.gemspec
4
+ gemspec
@@ -1,119 +1,130 @@
1
- == Introduction
1
+ ### Introduction
2
2
 
3
3
  Whenever is a Ruby gem that provides a clear syntax for writing and deploying cron jobs.
4
4
 
5
- == Installation
5
+ ### Installation
6
6
 
7
- $ gem install whenever
7
+ $ gem install whenever
8
8
 
9
9
  Or with Bundler in your Gemfile.
10
10
 
11
- gem 'whenever', :require => false
11
+ gem 'whenever', :require => false
12
12
 
13
- == Getting started
13
+ ### Getting started
14
14
 
15
- $ cd /my/rails/app
16
- $ wheneverize .
15
+ $ cd /my/rails/app
16
+ $ wheneverize .
17
17
 
18
18
  This will create an initial "config/schedule.rb" file you.
19
19
 
20
- == Example schedule.rb file
20
+ ### Example schedule.rb file
21
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
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
27
 
28
- every 1.day, :at => '4:30 am' do
29
- runner "MyModel.task_to_run_at_four_thirty_in_the_morning"
30
- end
28
+ every 1.day, :at => '4:30 am' do
29
+ runner "MyModel.task_to_run_at_four_thirty_in_the_morning"
30
+ end
31
31
 
32
- every :hour do # Many shortcuts available: :hour, :day, :month, :year, :reboot
33
- runner "SomeModel.ladeeda"
34
- end
32
+ every :hour do # Many shortcuts available: :hour, :day, :month, :year, :reboot
33
+ runner "SomeModel.ladeeda"
34
+ end
35
35
 
36
- every :sunday, :at => '12pm' do # Use any day of the week or :weekend, :weekday
37
- runner "Task.do_something_great"
38
- end
36
+ every :sunday, :at => '12pm' do # Use any day of the week or :weekend, :weekday
37
+ runner "Task.do_something_great"
38
+ end
39
39
 
40
- More examples on the wiki: http://wiki.github.com/javan/whenever/instructions-and-examples
40
+ every '0 0 27-31 * *' do
41
+ command "echo 'you can use raw cron sytax too'"
42
+ end
41
43
 
42
- == Define your own job types
44
+ More examples on the wiki: <http://wiki.github.com/javan/whenever/instructions-and-examples>
45
+
46
+ ### Define your own job types
43
47
 
44
48
  Whenever ships with three pre-defined job types: command, runner, and rake. You can define your own with `job_type`.
45
49
 
46
50
  For example:
47
51
 
48
- job_type :awesome, '/usr/local/bin/awesome :task :fun_level'
52
+ job_type :awesome, '/usr/local/bin/awesome :task :fun_level'
49
53
 
50
- every 2.hours do
51
- awesome "party", :fun_level => "extreme"
52
- end
54
+ every 2.hours do
55
+ awesome "party", :fun_level => "extreme"
56
+ end
53
57
 
54
58
  Would run `/usr/local/bin/awesome party extreme` every two hours. `:task` is always replaced with the first argument, and any additional `:whatevers` are replaced with the options passed in or by variables that have been defined with `set`.
55
59
 
56
60
  The default job types that ship with Whenever are defined like so:
57
61
 
58
- job_type :command, ":task :output"
59
- job_type :rake, "cd :path && RAILS_ENV=:environment rake :task :output"
60
- job_type :runner, "cd :path && script/runner -e :environment ':task' :output"
62
+ job_type :command, ":task :output"
63
+ job_type :rake, "cd :path && RAILS_ENV=:environment rake :task :output"
64
+ job_type :runner, "cd :path && script/runner -e :environment ':task' :output"
61
65
 
62
66
  If a script/rails file is detected (like in a Rails 3 app), runner will be defined to fit:
63
67
 
64
- job_type :runner, "cd :path && script/rails runner -e :environment ':task' :output"
68
+ job_type :runner, "cd :path && script/rails runner -e :environment ':task' :output"
65
69
 
66
- If a `:path` is not set it will default to the directory in which `whenever` was executed. `:environment` will default to 'production'. `:output` will be replaced with your output redirection settings which you can read more about here: http://github.com/javan/whenever/wiki/Output-redirection-(logging-your-cron-jobs)
70
+ If a `:path` is not set it will default to the directory in which `whenever` was executed. `:environment` will default to 'production'. `:output` will be replaced with your output redirection settings which you can read more about here: <http://github.com/javan/whenever/wiki/Output-redirection-aka-logging-your-cron-jobs>
67
71
 
68
- All jobs are by default run with `bash -l -c 'command...'`. Among other things, this allows your cron jobs to play nice with RVM by loading the entire environment instead of cron's somewhat limited environment. Read more: http://blog.scoutapp.com/articles/2010/09/07/rvm-and-cron-in-production
72
+ All jobs are by default run with `bash -l -c 'command...'`. Among other things, this allows your cron jobs to play nice with RVM by loading the entire environment instead of cron's somewhat limited environment. Read more: <http://blog.scoutapp.com/articles/2010/09/07/rvm-and-cron-in-production>
69
73
 
70
- You can change this by setting your own job_template.
74
+ You can change this by setting your own `:job_template`.
71
75
 
72
- set :job_template, "bash -l -c ':job'"
76
+ set :job_template, "bash -l -c ':job'"
73
77
 
74
78
  Or set the job_template to nil to have your jobs execute normally.
75
79
 
76
- set :job_template, nil
80
+ set :job_template, nil
77
81
 
78
82
  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.
79
83
 
80
- == Capistrano integration
84
+ ### Capistrano integration
81
85
 
82
86
  Use the built-in Capistrano recipe for easy crontab updates with deploys.
83
87
 
84
88
  In your "config/deploy.rb" file:
85
89
 
86
- require "whenever/capistrano"
90
+ require "whenever/capistrano"
87
91
 
88
- Take a look at the recipe for options you can set. http://github.com/javan/whenever/blob/master/lib/whenever/capistrano.rb
92
+ Take a look at the recipe for options you can set. <http://github.com/javan/whenever/blob/master/lib/whenever/capistrano.rb>
89
93
  For example, if you're using bundler do this:
90
94
 
91
- set :whenever_command, "bundle exec whenever"
92
- require "whenever/capistrano"
95
+ set :whenever_command, "bundle exec whenever"
96
+ require "whenever/capistrano"
97
+
98
+ If you are using different environments (such as staging, production), then you may want to do this:
99
+
100
+ set :whenever_environment, defer { stage }
101
+ require "whenever/capistrano"
102
+
103
+ The capistrano variable `:stage` should be the one holding your environment name. This will make the correct `:environment` available in your schedule.rb.
93
104
 
94
- == The `whenever` command
105
+ ### The `whenever` command
95
106
 
96
- $ cd /my/rails/app
97
- $ whenever
107
+ $ cd /my/rails/app
108
+ $ whenever
98
109
 
99
110
  This will simply show you your schedule.rb file converted to cron syntax. It does not read or write your crontab file. Run `whenever --help` for a complete list of options.
100
111
 
101
- == Credit
112
+ ### Credit
102
113
 
103
- 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
114
+ Whenever was created for use at Inkling (<http://inklingmarkets.com>). Their take on it: <http://blog.inklingmarkets.com/2009/02/whenever-easy-way-to-do-cron-jobs-from.html>
104
115
 
105
- While building Whenever, I learned a lot by digging through the source code of Capistrano - http://github.com/jamis/capistrano
116
+ Thanks to all the contributors who have made it even better: <http://github.com/javan/whenever/contributors>
106
117
 
107
- == Discussion / Feedback / Issues / Bugs
118
+ ### Discussion / Feedback / Issues / Bugs
108
119
 
109
- For general discussion and questions, please use the google group: http://groups.google.com/group/whenever-gem
120
+ For general discussion and questions, please use the google group: <http://groups.google.com/group/whenever-gem>
110
121
 
111
- If you've found a genuine bug or issue, please use the Issues section on github: http://github.com/javan/whenever/issues
122
+ If you've found a genuine bug or issue, please use the Issues section on github: <http://github.com/javan/whenever/issues>
112
123
 
113
- Ryan Bates created a great Railscast about Whenever: http://railscasts.com/episodes/164-cron-in-ruby
124
+ Ryan Bates created a great Railscast about Whenever: <http://railscasts.com/episodes/164-cron-in-ruby>
114
125
  It's a little bit dated now, but remains a good introduction.
115
126
 
116
- == License
127
+ ### License
117
128
 
118
129
  Copyright (c) 2009+ Javan Makhmali
119
130
 
data/Rakefile CHANGED
@@ -1,35 +1,11 @@
1
- require 'rubygems'
2
- require 'rake'
3
-
4
- require File.expand_path(File.dirname(__FILE__) + "/lib/whenever/version")
5
-
6
- begin
7
- require 'jeweler'
8
- Jeweler::Tasks.new do |gemspec|
9
- gemspec.name = "whenever"
10
- gemspec.version = Whenever::VERSION
11
- gemspec.summary = "Clean ruby syntax for writing and deploying cron jobs."
12
- gemspec.description = "http://www.allyrics.net/Kid-Cudi/lyrics/Whenever/"
13
- gemspec.email = "javan@javan.us"
14
- gemspec.homepage = "http://github.com/javan/whenever"
15
- gemspec.authors = ["Javan Makhmali"]
16
- gemspec.add_dependency 'aaronh-chronic', '>= 0.3.9'
17
- gemspec.add_dependency 'activesupport', '>= 2.3.4'
18
- gemspec.add_development_dependency 'shoulda', '>= 2.1.1'
19
- gemspec.add_development_dependency 'mocha', '>= 0.9.5'
20
- end
21
- Jeweler::GemcutterTasks.new
22
- rescue LoadError
23
- puts "Jeweler not available. Install it with: sudo gem install jeweler -s http://gemcutter.org"
24
- end
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
25
3
 
26
4
  require 'rake/testtask'
27
5
  Rake::TestTask.new(:test) do |test|
28
- test.libs << 'lib' << 'test'
29
- test.pattern = 'test/{functional,unit}/**/*_test.rb'
30
- test.verbose = true
31
- end
32
-
33
- task :test => :check_dependencies
6
+ test.libs << 'lib' << 'test'
7
+ test.pattern = 'test/{functional,unit}/**/*_test.rb'
8
+ test.verbose = true
9
+ end
34
10
 
35
11
  task :default => :test
data/bin/whenever CHANGED
File without changes
data/bin/wheneverize CHANGED
File without changes
@@ -3,7 +3,8 @@ Capistrano::Configuration.instance(:must_exist).load do
3
3
  _cset(:whenever_roles) { :db }
4
4
  _cset(:whenever_command) { "whenever" }
5
5
  _cset(:whenever_identifier) { application }
6
- _cset(:whenever_update_flags) { "--update-crontab #{whenever_identifier}" }
6
+ _cset(:whenever_environment) { "production" }
7
+ _cset(:whenever_update_flags) { "--update-crontab #{whenever_identifier} --set environment=#{whenever_environment}" }
7
8
  _cset(:whenever_clear_flags) { "--clear-crontab #{whenever_identifier}" }
8
9
 
9
10
  # Disable cron jobs at the begining of a deploy.
@@ -30,7 +30,6 @@ module Whenever
30
30
  exit(1)
31
31
  end
32
32
  @options[:cut] = @options[:cut].to_i
33
-
34
33
  end
35
34
 
36
35
  def run
@@ -40,6 +39,8 @@ module Whenever
40
39
  write_crontab(whenever_cron)
41
40
  else
42
41
  puts Whenever.cron(@options)
42
+ puts "## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated."
43
+ puts "## [message] Run `whenever --help' for more options."
43
44
  exit(0)
44
45
  end
45
46
  end
@@ -51,7 +52,8 @@ module Whenever
51
52
  end
52
53
 
53
54
  def whenever_cron
54
- @whenever_cron ||= [comment_open, (Whenever.cron(@options) unless @options[:clear]), comment_close].compact.join("\n") + "\n"
55
+ return '' if @options[:clear]
56
+ @whenever_cron ||= [comment_open, Whenever.cron(@options), comment_close].compact.join("\n") + "\n"
55
57
  end
56
58
 
57
59
  def read_crontab
@@ -87,26 +89,34 @@ module Whenever
87
89
 
88
90
  def updated_crontab
89
91
  # Check for unopened or unclosed identifier blocks
90
- if read_crontab =~ Regexp.new("^#{comment_open}$") && (read_crontab =~ Regexp.new("^#{comment_close}$")).nil?
92
+ if read_crontab =~ Regexp.new("^#{comment_open}\s*$") && (read_crontab =~ Regexp.new("^#{comment_close}\s*$")).nil?
91
93
  warn "[fail] Unclosed indentifier; Your crontab file contains '#{comment_open}', but no '#{comment_close}'"
92
94
  exit(1)
93
- elsif (read_crontab =~ Regexp.new("^#{comment_open}$")).nil? && read_crontab =~ Regexp.new("^#{comment_close}$")
95
+ elsif (read_crontab =~ Regexp.new("^#{comment_open}\s*$")).nil? && read_crontab =~ Regexp.new("^#{comment_close}\s*$")
94
96
  warn "[fail] Unopened indentifier; Your crontab file contains '#{comment_close}', but no '#{comment_open}'"
95
97
  exit(1)
96
98
  end
97
99
 
98
100
  # If an existing identier block is found, replace it with the new cron entries
99
- if read_crontab =~ Regexp.new("^#{comment_open}$") && read_crontab =~ Regexp.new("^#{comment_close}$")
101
+ if read_crontab =~ Regexp.new("^#{comment_open}\s*$") && read_crontab =~ Regexp.new("^#{comment_close}\s*$")
100
102
  # If the existing crontab file contains backslashes they get lost going through gsub.
101
103
  # .gsub('\\', '\\\\\\') preserves them. Go figure.
102
- read_crontab.gsub(Regexp.new("^#{comment_open}$.+^#{comment_close}$", Regexp::MULTILINE), whenever_cron.chomp.gsub('\\', '\\\\\\'))
104
+ read_crontab.gsub(Regexp.new("^#{comment_open}\s*$.+^#{comment_close}\s*$", Regexp::MULTILINE), whenever_cron.chomp.gsub('\\', '\\\\\\'))
103
105
  else # Otherwise, append the new cron entries after any existing ones
104
106
  [read_crontab, whenever_cron].join("\n\n")
105
- end
107
+ end.gsub(/\n{3,}/, "\n\n") # More than two newlines becomes just two.
106
108
  end
107
109
 
108
110
  def prepare(contents)
109
- contents.split("\n")[@options[:cut]..-1].join("\n")
111
+ # Strip n lines from the top of the file as specified by the :cut option.
112
+ # Use split with a -1 limit option to ensure the join is able to rebuild
113
+ # the file with all of the original seperators in-tact.
114
+ stripped_contents = contents.split($/,-1)[@options[:cut]..-1].join($/)
115
+
116
+ # Some cron implementations require all non-comment lines to be newline-
117
+ # terminated. (issue #95) Strip all newlines and replace with the default
118
+ # platform record seperator ($/)
119
+ stripped_contents.gsub!(/\s+$/, $/)
110
120
  end
111
121
 
112
122
  def comment_base
data/lib/whenever/cron.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  module Whenever
2
2
  module Output
3
3
  class Cron
4
+ REGEX = /^.+ .+ .+ .+ .+.?$/
4
5
 
5
6
  attr_accessor :time, :task
6
7
 
@@ -10,9 +11,14 @@ module Whenever
10
11
  @at = at.is_a?(String) ? (Chronic.parse(at) || 0) : (at || 0)
11
12
  end
12
13
 
13
- def self.enumerate(item)
14
+ def self.enumerate(item, detect_cron = true)
14
15
  if item and item.is_a?(String)
15
- items = item.split(',')
16
+ items =
17
+ if detect_cron && item =~ REGEX
18
+ [item]
19
+ else
20
+ item.split(',')
21
+ end
16
22
  else
17
23
  items = item
18
24
  items = [items] unless items and items.respond_to?(:each)
@@ -22,7 +28,7 @@ module Whenever
22
28
 
23
29
  def self.output(times, job)
24
30
  enumerate(times).each do |time|
25
- enumerate(job.at).each do |at|
31
+ enumerate(job.at, false).each do |at|
26
32
  yield new(time, job.output, at).output
27
33
  end
28
34
  end
@@ -34,6 +40,7 @@ module Whenever
34
40
 
35
41
  def time_in_cron_syntax
36
42
  case @time
43
+ when REGEX then @time # raw cron sytax given
37
44
  when Symbol then parse_symbol
38
45
  when String then parse_as_string
39
46
  else parse_time
@@ -44,16 +51,25 @@ module Whenever
44
51
 
45
52
  def parse_symbol
46
53
  shortcut = case @time
47
- when :reboot then '@reboot'
48
- when :year, :yearly then '@annually'
49
- when :day, :daily then '@daily'
50
- when :midnight then '@midnight'
51
- when :month, :monthly then '@monthly'
52
- when :week, :weekly then '@weekly'
53
- when :hour, :hourly then '@hourly'
54
+ when :reboot then '@reboot'
55
+ when :year then 12.months
56
+ when :yearly,
57
+ :annually then '@annually'
58
+ when :day then 1.day
59
+ when :daily then '@daily'
60
+ when :midnight then '@midnight'
61
+ when :month then 1.month
62
+ when :monthly then '@monthly'
63
+ when :week then 1.week
64
+ when :weekly then '@weekly'
65
+ when :hour then 1.hour
66
+ when :hourly then '@hourly'
54
67
  end
55
68
 
56
- if shortcut
69
+ if shortcut.is_a?(Numeric)
70
+ @time = shortcut
71
+ parse_time
72
+ elsif shortcut
57
73
  if @at.is_a?(Time) || (@at.is_a?(Numeric) && @at > 0)
58
74
  raise ArgumentError, "You cannot specify an ':at' when using the shortcuts for times."
59
75
  else
@@ -16,12 +16,9 @@ module Whenever
16
16
  pre_set(options[:set])
17
17
  end
18
18
 
19
- # Load all job type files.
20
- Dir["#{File.expand_path(File.dirname(__FILE__))}/job_types/*.rb"].each do |file|
21
- eval(File.read(file))
22
- end
19
+ setup = File.read("#{File.expand_path(File.dirname(__FILE__))}/setup.rb")
23
20
 
24
- eval(config)
21
+ eval(setup + config)
25
22
  end
26
23
 
27
24
  def set(variable, value)
@@ -91,7 +88,7 @@ module Whenever
91
88
 
92
89
  output = []
93
90
  @env.each do |key, val|
94
- output << "#{key}=#{val}\n"
91
+ output << "#{key}=#{val.blank? ? '""' : val}\n"
95
92
  end
96
93
  output << "\n"
97
94
 
@@ -1,10 +1,17 @@
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.
1
7
  # http://blog.scoutapp.com/articles/2010/09/07/rvm-and-cron-in-production
2
8
  set :job_template, "/bin/bash -l -c ':job'"
3
9
 
4
10
  job_type :command, ":task :output"
5
11
  job_type :rake, "cd :path && RAILS_ENV=:environment rake :task --silent :output"
6
12
 
7
- if File.exists?(File.join(Whenever.path, 'script', 'rails'))
13
+ # Create a runner job that's appropriate for the Rails version,
14
+ if File.exists?(File.join(path, 'script', 'rails'))
8
15
  job_type :runner, "cd :path && script/rails runner -e :environment ':task' :output"
9
16
  else
10
17
  job_type :runner, "cd :path && script/runner -e :environment ':task' :output"
@@ -1,3 +1,3 @@
1
1
  module Whenever
2
- VERSION = '0.6.1'
3
- end unless defined?(Whenever::VERSION)
2
+ VERSION = '0.6.8'
3
+ end
data/lib/whenever.rb CHANGED
@@ -1,10 +1,22 @@
1
1
  require 'chronic'
2
2
  require 'active_support/all'
3
+ require 'thread'
3
4
 
4
- require 'whenever/base'
5
5
  require 'whenever/job_list'
6
6
  require 'whenever/job'
7
7
  require 'whenever/cron'
8
8
  require 'whenever/output_redirection'
9
9
  require 'whenever/command_line'
10
- require 'whenever/version'
10
+ require 'whenever/version'
11
+
12
+ module Whenever
13
+
14
+ def self.cron(options)
15
+ Whenever::JobList.new(options).generate_cron_output
16
+ end
17
+
18
+ def self.path
19
+ Dir.pwd
20
+ end
21
+
22
+ end
@@ -120,35 +120,18 @@ NEW_CRON
120
120
  end
121
121
 
122
122
  should "append the similarly named command" do
123
- assert_equal @existing + "\n\n" + @new, @command.send(:updated_crontab)
123
+ assert_equal @existing + "\n" + @new, @command.send(:updated_crontab)
124
124
  end
125
125
  end
126
126
 
127
- context "A command line delete" do
127
+ context "A command line clear" do
128
128
  setup do
129
129
  File.expects(:exists?).with('config/schedule.rb').returns(true)
130
130
  @command = Whenever::CommandLine.new(:clear => true, :identifier => 'My identifier')
131
131
  @task = "#{two_hours} /my/command"
132
132
  end
133
133
 
134
- should "add an empty identifier block if there is no existing one" do
135
- existing = '# Existing crontab'
136
- @command.expects(:read_crontab).at_least_once.returns(existing)
137
-
138
- new_cron = <<-EXPECTED
139
- #{existing}
140
-
141
- # Begin Whenever generated tasks for: My identifier
142
- # End Whenever generated tasks for: My identifier
143
- EXPECTED
144
-
145
- assert_equal new_cron, @command.send(:updated_crontab)
146
-
147
- @command.expects(:write_crontab).with(new_cron).returns(true)
148
- assert @command.run
149
- end
150
-
151
- should "delete an existing block if the identifier matches" do
134
+ should "clear an existing block if the identifier matches" do
152
135
  existing = <<-EXISTING_CRON
153
136
  # Something
154
137
 
@@ -162,20 +145,17 @@ This shouldn't get replaced
162
145
  EXISTING_CRON
163
146
 
164
147
  @command.expects(:read_crontab).at_least_once.returns(existing)
165
-
148
+
166
149
  new_cron = <<-NEW_CRON
167
150
  # Something
168
151
 
169
- # Begin Whenever generated tasks for: My identifier
170
- # End Whenever generated tasks for: My identifier
171
-
172
152
  # Begin Whenever generated tasks for: Other identifier
173
153
  This shouldn't get replaced
174
154
  # End Whenever generated tasks for: Other identifier
175
155
  NEW_CRON
176
-
156
+
177
157
  assert_equal new_cron, @command.send(:updated_crontab)
178
-
158
+
179
159
  @command.expects(:write_crontab).with(new_cron).returns(true)
180
160
  assert @command.run
181
161
  end
@@ -301,8 +281,7 @@ My whenever job that was already here
301
281
  # End Whenever generated tasks for: My identifier
302
282
  EXISTING_CRON
303
283
 
304
- # here-doc adds an extra newline we need removed
305
- assert_equal existing.strip, @command.send(:prepare, existing)
284
+ assert_equal existing, @command.send(:prepare, existing)
306
285
  end
307
286
 
308
287
  should "trim off the top lines of the file" do
@@ -322,8 +301,21 @@ My whenever job that was already here
322
301
  # End Whenever generated tasks for: My identifier
323
302
  NEW_CRON
324
303
 
325
- # here-doc adds an extra newline we need removed
326
- assert_equal new_cron.strip, @command.send(:prepare, existing)
304
+ assert_equal new_cron, @command.send(:prepare, existing)
305
+ end
306
+
307
+ should "preserve terminating newlines in files" do
308
+ @command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier')
309
+ existing = <<-EXISTING_CRON
310
+ # Begin Whenever generated tasks for: My identifier
311
+ My whenever job that was already here
312
+ # End Whenever generated tasks for: My identifier
313
+
314
+ # A non-Whenever task
315
+ My non-whenever job that was already here
316
+ EXISTING_CRON
317
+
318
+ assert_equal existing, @command.send(:prepare, existing)
327
319
  end
328
320
  end
329
321
 
@@ -166,7 +166,7 @@ class OutputAtTest < Test::Unit::TestCase
166
166
  <<-file
167
167
  set :job_template, nil
168
168
  set :path, '/your/path'
169
- every :day do
169
+ every :daily do
170
170
  rake "blah:blah"
171
171
  runner "runner_1"
172
172
  command "command_1"
@@ -248,4 +248,21 @@ class OutputAtTest < Test::Unit::TestCase
248
248
  assert_match '27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59 * * * * blahblah', @output
249
249
  end
250
250
  end
251
+
252
+ context "using raw cron syntax" do
253
+ setup do
254
+ @output = Whenever.cron \
255
+ <<-file
256
+ set :job_template, nil
257
+ every '0 0 27,31 * *' do
258
+ command "blahblah"
259
+ end
260
+ file
261
+ end
262
+
263
+ should "output the command using the same cron syntax" do
264
+ assert_match '0 0 27,31 * * blahblah', @output
265
+ end
266
+ end
267
+
251
268
  end
@@ -51,6 +51,25 @@ class OutputDefaultDefinedJobsTest < Test::Unit::TestCase
51
51
  assert_no_match /bash/, @output
52
52
  end
53
53
  end
54
+
55
+ context "A plain command that is conditional on default environent and path" do
56
+ setup do
57
+ Whenever.expects(:path).at_least_once.returns('/what/you/want')
58
+ @output = Whenever.cron \
59
+ <<-file
60
+ set :job_template, nil
61
+ if environment == 'production' && path == '/what/you/want'
62
+ every 2.hours do
63
+ command "blahblah"
64
+ end
65
+ end
66
+ file
67
+ end
68
+
69
+ should "output the command" do
70
+ assert_match /blahblah/, @output
71
+ end
72
+ end
54
73
 
55
74
  # runner
56
75
 
@@ -8,6 +8,8 @@ class OutputEnvTest < Test::Unit::TestCase
8
8
  <<-file
9
9
  env :MYVAR, 'blah'
10
10
  env 'MAILTO', "someone@example.com"
11
+ env :BLANKVAR, ''
12
+ env :NILVAR, nil
11
13
  file
12
14
  end
13
15
 
@@ -18,6 +20,14 @@ class OutputEnvTest < Test::Unit::TestCase
18
20
  should "output MAILTO environment variable" do
19
21
  assert_match "MAILTO=someone@example.com", @output
20
22
  end
23
+
24
+ should "output BLANKVAR environment variable" do
25
+ assert_match "BLANKVAR=\"\"", @output
26
+ end
27
+
28
+ should "output NILVAR environment variable" do
29
+ assert_match "NILVAR=\"\"", @output
30
+ end
21
31
  end
22
32
 
23
- end
33
+ end
@@ -174,20 +174,26 @@ class CronTest < Test::Unit::TestCase
174
174
  context "When parsing time using the cron shortcuts" do
175
175
  should "parse a :symbol into the correct shortcut" do
176
176
  assert_equal '@reboot', parse_time(:reboot)
177
- assert_equal '@annually', parse_time(:year)
177
+ assert_equal '@annually', parse_time(:annually)
178
178
  assert_equal '@annually', parse_time(:yearly)
179
- assert_equal '@daily', parse_time(:day)
180
179
  assert_equal '@daily', parse_time(:daily)
181
180
  assert_equal '@midnight', parse_time(:midnight)
182
- assert_equal '@monthly', parse_time(:month)
183
181
  assert_equal '@monthly', parse_time(:monthly)
184
- assert_equal '@hourly', parse_time(:hour)
182
+ assert_equal '@weekly', parse_time(:weekly)
185
183
  assert_equal '@hourly', parse_time(:hourly)
186
184
  end
187
185
 
186
+ should "convert time-based shortcuts to times" do
187
+ assert_equal '0 0 1 * *', parse_time(:month)
188
+ assert_equal '0 0 * * *', parse_time(:day)
189
+ assert_equal '0 * * * *', parse_time(:hour)
190
+ assert_equal '0 0 1 12 *', parse_time(:year)
191
+ assert_equal '0 0 1,8,15,22 * *', parse_time(:week)
192
+ end
193
+
188
194
  should "raise an exception if a valid shortcut is given but also an :at" do
189
195
  assert_raises ArgumentError do
190
- parse_time(:hour, nil, "1:00 am")
196
+ parse_time(:hourly, nil, "1:00 am")
191
197
  end
192
198
 
193
199
  assert_raises ArgumentError do
@@ -195,7 +201,16 @@ class CronTest < Test::Unit::TestCase
195
201
  end
196
202
 
197
203
  assert_raises ArgumentError do
198
- parse_time(:day, nil, '4:20pm')
204
+ parse_time(:daily, nil, '4:20pm')
205
+ end
206
+ end
207
+ end
208
+
209
+ context "When given raw cron sytax" do
210
+ should "return the same cron sytax" do
211
+ crons = ['0 0 27-31 * *', '* * * * *', '2/3 1,9,22 11-26 1-6 *']
212
+ crons.each do |cron|
213
+ assert_equal cron, parse_time(cron)
199
214
  end
200
215
  end
201
216
  end
data/whenever.gemspec CHANGED
@@ -1,86 +1,22 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
1
  # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "whenever/version"
5
4
 
6
5
  Gem::Specification.new do |s|
7
- s.name = %q{whenever}
8
- s.version = "0.6.1"
9
-
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Javan Makhmali"]
12
- s.date = %q{2010-10-20}
13
- s.description = %q{http://www.allyrics.net/Kid-Cudi/lyrics/Whenever/}
14
- s.email = %q{javan@javan.us}
15
- s.executables = ["whenever", "wheneverize"]
16
- s.extra_rdoc_files = [
17
- "README.rdoc"
18
- ]
19
- s.files = [
20
- ".gitignore",
21
- "CHANGELOG.rdoc",
22
- "README.rdoc",
23
- "Rakefile",
24
- "bin/whenever",
25
- "bin/wheneverize",
26
- "lib/whenever.rb",
27
- "lib/whenever/base.rb",
28
- "lib/whenever/capistrano.rb",
29
- "lib/whenever/command_line.rb",
30
- "lib/whenever/cron.rb",
31
- "lib/whenever/job.rb",
32
- "lib/whenever/job_list.rb",
33
- "lib/whenever/job_types/default.rb",
34
- "lib/whenever/output_redirection.rb",
35
- "lib/whenever/version.rb",
36
- "test/functional/command_line_test.rb",
37
- "test/functional/output_at_test.rb",
38
- "test/functional/output_default_defined_jobs_test.rb",
39
- "test/functional/output_defined_job_test.rb",
40
- "test/functional/output_env_test.rb",
41
- "test/functional/output_redirection_test.rb",
42
- "test/test_helper.rb",
43
- "test/unit/cron_test.rb",
44
- "test/unit/job_test.rb",
45
- "whenever.gemspec"
46
- ]
47
- s.homepage = %q{http://github.com/javan/whenever}
48
- s.rdoc_options = ["--charset=UTF-8"]
6
+ s.name = "whenever"
7
+ s.version = Whenever::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Javan Makhmali"]
10
+ s.email = ["javan@javan.us"]
11
+ s.homepage = ""
12
+ s.summary = %q{Cron jobs in ruby.}
13
+ s.description = %q{Clean ruby syntax for writing and deploying cron jobs.}
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- test/{functional,unit}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
49
17
  s.require_paths = ["lib"]
50
- s.rubygems_version = %q{1.3.7}
51
- s.summary = %q{Clean ruby syntax for writing and deploying cron jobs.}
52
- s.test_files = [
53
- "test/functional/command_line_test.rb",
54
- "test/functional/output_at_test.rb",
55
- "test/functional/output_default_defined_jobs_test.rb",
56
- "test/functional/output_defined_job_test.rb",
57
- "test/functional/output_env_test.rb",
58
- "test/functional/output_redirection_test.rb",
59
- "test/test_helper.rb",
60
- "test/unit/cron_test.rb",
61
- "test/unit/job_test.rb"
62
- ]
63
-
64
- if s.respond_to? :specification_version then
65
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
66
- s.specification_version = 3
67
-
68
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
69
- s.add_runtime_dependency(%q<aaronh-chronic>, [">= 0.3.9"])
70
- s.add_runtime_dependency(%q<activesupport>, [">= 2.3.4"])
71
- s.add_development_dependency(%q<shoulda>, [">= 2.1.1"])
72
- s.add_development_dependency(%q<mocha>, [">= 0.9.5"])
73
- else
74
- s.add_dependency(%q<aaronh-chronic>, [">= 0.3.9"])
75
- s.add_dependency(%q<activesupport>, [">= 2.3.4"])
76
- s.add_dependency(%q<shoulda>, [">= 2.1.1"])
77
- s.add_dependency(%q<mocha>, [">= 0.9.5"])
78
- end
79
- else
80
- s.add_dependency(%q<aaronh-chronic>, [">= 0.3.9"])
81
- s.add_dependency(%q<activesupport>, [">= 2.3.4"])
82
- s.add_dependency(%q<shoulda>, [">= 2.1.1"])
83
- s.add_dependency(%q<mocha>, [">= 0.9.5"])
84
- end
18
+ s.add_runtime_dependency(%q<aaronh-chronic>, [">= 0.3.9"])
19
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.3.4"])
20
+ s.add_development_dependency(%q<shoulda>, [">= 2.1.1"])
21
+ s.add_development_dependency(%q<mocha>, [">= 0.9.5"])
85
22
  end
86
-
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: whenever
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
4
+ hash: 23
5
+ prerelease:
5
6
  segments:
6
7
  - 0
7
8
  - 6
8
- - 1
9
- version: 0.6.1
9
+ - 8
10
+ version: 0.6.8
10
11
  platform: ruby
11
12
  authors:
12
13
  - Javan Makhmali
@@ -14,7 +15,7 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-10-20 00:00:00 -04:00
18
+ date: 2011-05-24 00:00:00 -04:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
@@ -25,6 +26,7 @@ dependencies:
25
26
  requirements:
26
27
  - - ">="
27
28
  - !ruby/object:Gem::Version
29
+ hash: 1
28
30
  segments:
29
31
  - 0
30
32
  - 3
@@ -40,6 +42,7 @@ dependencies:
40
42
  requirements:
41
43
  - - ">="
42
44
  - !ruby/object:Gem::Version
45
+ hash: 11
43
46
  segments:
44
47
  - 2
45
48
  - 3
@@ -55,6 +58,7 @@ dependencies:
55
58
  requirements:
56
59
  - - ">="
57
60
  - !ruby/object:Gem::Version
61
+ hash: 9
58
62
  segments:
59
63
  - 2
60
64
  - 1
@@ -70,6 +74,7 @@ dependencies:
70
74
  requirements:
71
75
  - - ">="
72
76
  - !ruby/object:Gem::Version
77
+ hash: 49
73
78
  segments:
74
79
  - 0
75
80
  - 9
@@ -77,31 +82,32 @@ dependencies:
77
82
  version: 0.9.5
78
83
  type: :development
79
84
  version_requirements: *id004
80
- description: http://www.allyrics.net/Kid-Cudi/lyrics/Whenever/
81
- email: javan@javan.us
85
+ description: Clean ruby syntax for writing and deploying cron jobs.
86
+ email:
87
+ - javan@javan.us
82
88
  executables:
83
89
  - whenever
84
90
  - wheneverize
85
91
  extensions: []
86
92
 
87
- extra_rdoc_files:
88
- - README.rdoc
93
+ extra_rdoc_files: []
94
+
89
95
  files:
90
96
  - .gitignore
91
- - CHANGELOG.rdoc
92
- - README.rdoc
97
+ - CHANGELOG.md
98
+ - Gemfile
99
+ - README.md
93
100
  - Rakefile
94
101
  - bin/whenever
95
102
  - bin/wheneverize
96
103
  - lib/whenever.rb
97
- - lib/whenever/base.rb
98
104
  - lib/whenever/capistrano.rb
99
105
  - lib/whenever/command_line.rb
100
106
  - lib/whenever/cron.rb
101
107
  - lib/whenever/job.rb
102
108
  - lib/whenever/job_list.rb
103
- - lib/whenever/job_types/default.rb
104
109
  - lib/whenever/output_redirection.rb
110
+ - lib/whenever/setup.rb
105
111
  - lib/whenever/version.rb
106
112
  - test/functional/command_line_test.rb
107
113
  - test/functional/output_at_test.rb
@@ -114,12 +120,12 @@ files:
114
120
  - test/unit/job_test.rb
115
121
  - whenever.gemspec
116
122
  has_rdoc: true
117
- homepage: http://github.com/javan/whenever
123
+ homepage: ""
118
124
  licenses: []
119
125
 
120
126
  post_install_message:
121
- rdoc_options:
122
- - --charset=UTF-8
127
+ rdoc_options: []
128
+
123
129
  require_paths:
124
130
  - lib
125
131
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -127,6 +133,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
127
133
  requirements:
128
134
  - - ">="
129
135
  - !ruby/object:Gem::Version
136
+ hash: 3
130
137
  segments:
131
138
  - 0
132
139
  version: "0"
@@ -135,16 +142,17 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
142
  requirements:
136
143
  - - ">="
137
144
  - !ruby/object:Gem::Version
145
+ hash: 3
138
146
  segments:
139
147
  - 0
140
148
  version: "0"
141
149
  requirements: []
142
150
 
143
151
  rubyforge_project:
144
- rubygems_version: 1.3.7
152
+ rubygems_version: 1.4.2
145
153
  signing_key:
146
154
  specification_version: 3
147
- summary: Clean ruby syntax for writing and deploying cron jobs.
155
+ summary: Cron jobs in ruby.
148
156
  test_files:
149
157
  - test/functional/command_line_test.rb
150
158
  - test/functional/output_at_test.rb
@@ -152,6 +160,5 @@ test_files:
152
160
  - test/functional/output_defined_job_test.rb
153
161
  - test/functional/output_env_test.rb
154
162
  - test/functional/output_redirection_test.rb
155
- - test/test_helper.rb
156
163
  - test/unit/cron_test.rb
157
164
  - test/unit/job_test.rb
data/lib/whenever/base.rb DELETED
@@ -1,11 +0,0 @@
1
- module Whenever
2
-
3
- def self.cron(options)
4
- Whenever::JobList.new(options).generate_cron_output
5
- end
6
-
7
- def self.path
8
- Dir.pwd
9
- end
10
-
11
- end