whenever 0.6.1 → 0.7.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/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  pkg
3
3
  doc
4
4
  .*.sw[a-z]
5
+ Gemfile.lock
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - ree
5
+ - jruby
@@ -1,4 +1,62 @@
1
- == 0.6.1 / October 20th, 2010
1
+ ### 0.7.0 / September 2nd, 2011
2
+
3
+ * Use mojombo's chronic, it's active again. [Javan Makhmali]
4
+
5
+ * Capistrano task enhancements. [Chris Griego]
6
+
7
+ * wheneverize command defaults to '.' directory. [Andrew Nesbitt]
8
+
9
+ * rake job_type uses bundler if detected. [Michał Szajbe]
10
+
11
+ * Indicate filename in exceptions stemming from schedule file. [Javan Makhmali]
12
+
13
+ * Don't require rubygems, bundler where possible. [Oleg Pudeyev]
14
+
15
+ * Documentation and code cleanup. [many nice people]
16
+
17
+
18
+ ### 0.6.8 / May 24th, 2011
19
+
20
+ * Convert most shortcuts to seconds. every :day -> every 1.day. #129 [Javan Makhmali]
21
+
22
+ * Allow commas in raw cron syntax. #130 [Marco Bergantin, Javan Makhmali]
23
+
24
+ * Output no update message as comments. #135 [Javan Makhmali]
25
+
26
+ * require 'thread' to support Rubygems >= 1.6.0. #132 [Javan Makhmali]
27
+
28
+
29
+ ### 0.6.7 / March 23rd, 2011
30
+
31
+ * Fix issue with comment block being corrupted during subsequent insertion of duplicate entries to the crontab. #123 [Jeremy (@lingmann)]
32
+
33
+ * Removed -i from default job template. #118 [Javan Makhmali]
34
+
35
+
36
+ ### 0.6.6 / March 8th, 2011
37
+
38
+ * Fix unclosed identifier bug. #119 [Javan Makhmali]
39
+
40
+
41
+ ### 0.6.5 / March 8th, 2011
42
+
43
+ * Preserve whitespace at the end of crontab file. #95 [Rich Meyers]
44
+
45
+ * Setting nil or blank environment variables now properly formats output. [T.J. VanSlyke]
46
+
47
+ * Allow raw cron sytax, added -i to bash job template, general cleanup. [Javan Makhmali]
48
+
49
+
50
+ ### 0.6.2 / October 26th, 2010
51
+
52
+ * --clear-crontab option completely removes entries. #63 [Javan Makhmali]
53
+
54
+ * Set default :environment and :path earlier in the new setup.rb (formerly job_types/default.rb). [Javan Makhmali]
55
+
56
+ * Converted README and CHANGELOG to markdown. [Javan Makhmali]
57
+
58
+
59
+ ### 0.6.1 / October 20th, 2010
2
60
 
3
61
  * Detect script/rails file and change runner to Rails 3 style if found. [Javan Makhmali]
4
62
 
@@ -7,14 +65,14 @@
7
65
  * Added a built-in Capistrano recipe. [Javan Makhmali]
8
66
 
9
67
 
10
- == 0.5.3 / September 24th, 2010
68
+ ### 0.5.3 / September 24th, 2010
11
69
 
12
70
  * Better regexes for replacing Whenever blocks in the crontab. #45 [Javan Makhmali]
13
71
 
14
72
  * Preserving backslashes when updating existing crontab. #82 [Javan Makhmali]
15
73
 
16
74
 
17
- == 0.5.2 / September 15th, 2010
75
+ ### 0.5.2 / September 15th, 2010
18
76
 
19
77
  * Quotes automatically escaped in jobs. [Jay Adkisson]
20
78
 
@@ -25,7 +83,7 @@
25
83
  * Lots of internal reorganizing; tests broken into unit and functional. [Javan Makhmali]
26
84
 
27
85
 
28
- == 0.5.0 / June 28th, 2010
86
+ ### 0.5.0 / June 28th, 2010
29
87
 
30
88
  * New job_type API for writing custom jobs. Internals use this to define command, runner, and rake. [Javan Makhmali - inspired by idlefingers (Damien)]
31
89
 
@@ -34,7 +92,7 @@
34
92
  * --clear option to remove crontab entries for a specific [identifier]. [mraidel (Michael Raidel)]
35
93
 
36
94
 
37
- == 0.4.2 / April 26th, 2010
95
+ ### 0.4.2 / April 26th, 2010
38
96
 
39
97
  * runners now cd into the app's directory and then execute. [Michael Guterl]
40
98
 
@@ -45,49 +103,49 @@
45
103
  * bugfix: comparison Time with 0 failed. #32 [Dan Hixon]
46
104
 
47
105
 
48
- == 0.4.1 / November 30th, 2009
106
+ ### 0.4.1 / November 30th, 2009
49
107
 
50
108
  * exit(0) instead of just exit to make JRuby happy. [Elan Meng]
51
109
 
52
110
  * Fixed activesupport deprecation warning by requiring active_support. #37 [Andrew Nesbitt]
53
111
 
54
112
 
55
- == 0.4.0 / October 20th, 2009
113
+ ### 0.4.0 / October 20th, 2009
56
114
 
57
115
  * New output option replaces the old cron_log option for output redirection and is much more flexible. #31 [Peer Allan]
58
116
 
59
117
  * Reorganized the lib files (http://weblog.rubyonrails.org/2009/9/1/gem-packaging-best-practices) and switched to Jeweler from Echoe.
60
118
 
61
119
 
62
- == 0.3.7 / September 4th, 2009
120
+ ### 0.3.7 / September 4th, 2009
63
121
 
64
122
  * No longer tries (and fails) to combine @shortcut jobs. #20 [Javan Makhmali]
65
123
 
66
124
 
67
- == 0.3.6 / June 15th, 2009
125
+ ### 0.3.6 / June 15th, 2009
68
126
 
69
127
  * Setting a PATH in the crontab automatically based on the user's PATH. [Javan Makhmali]
70
128
 
71
129
 
72
- == 0.3.5 / June 13th, 2009
130
+ ### 0.3.5 / June 13th, 2009
73
131
 
74
132
  * 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
133
 
76
134
  * Fixed issue with new lines. #18 [Javan Makhmali]
77
135
 
78
- == 0.3.1 / June 25th, 2009
136
+ ### 0.3.1 / June 25th, 2009
79
137
 
80
138
  * Removed activesupport gem dependency. #1 [Javan Makhmali]
81
139
 
82
140
  * Switched to numeric days of the week for Solaris support (and probably others). #8 [Roger Ertesvåg]
83
141
 
84
142
 
85
- == 0.3.0 / June 2nd, 2009
143
+ ### 0.3.0 / June 2nd, 2009
86
144
 
87
145
  * Added ability to set variables on the fly from the command line (ex: whenever --set environment=staging). [Javan Makhmali]
88
146
 
89
147
 
90
- == 0.2.2 / April 30th, 2009
148
+ ### 0.2.2 / April 30th, 2009
91
149
 
92
150
  * Days of week jobs can now accept an :at directive (ex: every :monday, :at => '5pm'). [David Eisinger]
93
151
 
@@ -96,22 +154,22 @@
96
154
  * 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
155
 
98
156
 
99
- == 0.1.7 / March 5th, 2009
157
+ ### 0.1.7 / March 5th, 2009
100
158
 
101
159
  * 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
160
 
103
161
 
104
- == 0.1.5 / February 19th, 2009
162
+ ### 0.1.5 / February 19th, 2009
105
163
 
106
164
  * Fixed load path so Whenever's files don't conflict with anything in Rails. Thanks Ryan Koopmans. [Javan Makhmali]
107
165
 
108
166
 
109
- == 0.1.4 / February 17th, 2009
167
+ ### 0.1.4 / February 17th, 2009
110
168
 
111
169
  * Added --load-file and --user opts to whenever binary. [Javan Makhmali]
112
170
 
113
171
 
114
- == 0.1.3 / February 16th, 2009
172
+ ### 0.1.3 / February 16th, 2009
115
173
 
116
174
  * Added 'rake' helper for defining scheduled rake tasks. [Javan Makhmali]
117
175
 
@@ -124,6 +182,6 @@
124
182
  * Requiring specific gem versions: Chronic >=0.2.3 and activesupport >= 1.3.0 [Javan Makhmali]
125
183
 
126
184
 
127
- == 0.1.0 / February 15th, 2009
185
+ ### 0.1.0 / February 15th, 2009
128
186
 
129
187
  * 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
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2011 Javan Makhmali
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,165 @@
1
+ Whenever is a Ruby gem that provides a clear syntax for writing and deploying cron jobs.
2
+
3
+ ### Installation
4
+
5
+ ```sh
6
+ $ gem install whenever
7
+ ```
8
+
9
+ Or with Bundler in your Gemfile.
10
+
11
+ ```ruby
12
+ gem 'whenever', :require => false
13
+ ```
14
+
15
+ ### Getting started
16
+
17
+ ```sh
18
+ $ cd /apps/my-great-project
19
+ $ wheneverize .
20
+ ```
21
+
22
+ This will create an initial `config/schedule.rb` file for you.
23
+
24
+ ### Example schedule.rb file
25
+
26
+ ```ruby
27
+ every 3.hours do
28
+ runner "MyModel.some_process"
29
+ rake "my:rake:task"
30
+ command "/usr/bin/my_great_command"
31
+ end
32
+
33
+ every 1.day, :at => '4:30 am' do
34
+ runner "MyModel.task_to_run_at_four_thirty_in_the_morning"
35
+ end
36
+
37
+ every :hour do # Many shortcuts available: :hour, :day, :month, :year, :reboot
38
+ runner "SomeModel.ladeeda"
39
+ end
40
+
41
+ every :sunday, :at => '12pm' do # Use any day of the week or :weekend, :weekday
42
+ runner "Task.do_something_great"
43
+ end
44
+
45
+ every '0 0 27-31 * *' do
46
+ command "echo 'you can use raw cron syntax too'"
47
+ end
48
+ ```
49
+
50
+ ### Define your own job types
51
+
52
+ Whenever ships with three pre-defined job types: command, runner, and rake. You can define your own with `job_type`.
53
+
54
+ For example:
55
+
56
+ ```ruby
57
+ job_type :awesome, '/usr/local/bin/awesome :task :fun_level'
58
+
59
+ every 2.hours do
60
+ awesome "party", :fun_level => "extreme"
61
+ end
62
+ ```
63
+
64
+ 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`.
65
+
66
+ The default job types that ship with Whenever are defined like so:
67
+
68
+ ```ruby
69
+ job_type :command, ":task :output"
70
+ job_type :rake, "cd :path && RAILS_ENV=:environment bundle exec rake :task --silent :output"
71
+ job_type :runner, "cd :path && script/rails runner -e :environment ':task' :output"
72
+ ```
73
+
74
+ Pre-Rails 3 apps and apps that don't use Bundler will redefine the `rake` and `runner` jobs respectively to function correctly.
75
+
76
+ 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>
77
+
78
+ 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>
79
+
80
+ You can change this by setting your own `:job_template`.
81
+
82
+ ```ruby
83
+ set :job_template, "bash -l -c ':job'"
84
+ ```
85
+
86
+ Or set the job_template to nil to have your jobs execute normally.
87
+
88
+ ```ruby
89
+ set :job_template, nil
90
+ ```
91
+
92
+ ### Capistrano integration
93
+
94
+ Use the built-in Capistrano recipe for easy crontab updates with deploys.
95
+
96
+ In your "config/deploy.rb" file:
97
+
98
+ ```ruby
99
+ require "whenever/capistrano"
100
+ ```
101
+
102
+ Take a look at the recipe for options you can set. <http://github.com/javan/whenever/blob/master/lib/whenever/capistrano.rb>
103
+ For example, if you're using bundler do this:
104
+
105
+ ```ruby
106
+ set :whenever_command, "bundle exec whenever"
107
+ require "whenever/capistrano"
108
+ ```
109
+
110
+ If you are using different environments (such as staging, production), then you may want to do this:
111
+
112
+ ```ruby
113
+ set :whenever_environment, defer { stage }
114
+ require "whenever/capistrano"
115
+ ```
116
+
117
+ The capistrano variable `:stage` should be the one holding your environment name. This will make the correct `:environment` available in your `schedule.rb`.
118
+
119
+ If both your environments are on the same server you'll want to namespace them or they'll overwrite each other when you deploy:
120
+
121
+ ```ruby
122
+ set :whenever_environment, defer { stage }
123
+ set :whenever_identifier, defer { "#{application}_#{stage}" }
124
+ require "whenever/capistrano"
125
+ ```
126
+
127
+ ### RVM Integration
128
+
129
+ If your production environment uses RVM (Ruby Version Manager) you will run into a gotcha that causes your cron jobs to hang. This is not directly related to Whenever, and can be tricky to debug. Your .rvmrc files must be trusted or else the cron jobs will hang waiting for the file to be trusted. A solution is to disable the prompt by adding this line to your user rvm file in `~/.rvmrc`
130
+
131
+ `rvm_trust_rvmrcs_flag=1`
132
+
133
+ This tells rvm to trust all rvmrc files, which is documented here: http://wayneeseguin.beginrescueend.com/2010/08/22/ruby-environment-version-manager-rvm-1-0-0/
134
+
135
+ ### The `whenever` command
136
+
137
+ ```sh
138
+ $ cd /apps/my-great-project
139
+ $ whenever
140
+ ```
141
+
142
+ 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.
143
+
144
+ ### Credit
145
+
146
+ 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>
147
+
148
+ Thanks to all the contributors who have made it even better: <http://github.com/javan/whenever/contributors>
149
+
150
+ ### Discussion / Feedback / Issues / Bugs
151
+
152
+ For general discussion and questions, please use the google group: <http://groups.google.com/group/whenever-gem>
153
+
154
+ If you've found a genuine bug or issue, please use the Issues section on github: <http://github.com/javan/whenever/issues>
155
+
156
+ Ryan Bates created a great Railscast about Whenever: <http://railscasts.com/episodes/164-cron-in-ruby>
157
+ It's a little bit dated now, but remains a good introduction.
158
+
159
+ ----
160
+
161
+ Compatible with Ruby 1.8.7-1.9.2, JRuby, and Rubinius. [![Build Status](https://secure.travis-ci.org/javan/whenever.png)](http://travis-ci.org/javan/whenever)
162
+
163
+ ----
164
+
165
+ Copyright &copy; 2011 Javan Makhmali
data/Rakefile CHANGED
@@ -1,35 +1,17 @@
1
- require 'rubygems'
2
- require 'rake'
3
-
4
- require File.expand_path(File.dirname(__FILE__) + "/lib/whenever/version")
5
-
6
1
  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"
2
+ require 'bundler'
3
+ rescue LoadError => e
4
+ warn("warning: Could not load bundler: #{e}")
5
+ warn(" Some rake tasks will not be defined")
6
+ else
7
+ Bundler::GemHelper.install_tasks
24
8
  end
25
9
 
26
10
  require 'rake/testtask'
27
11
  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
12
+ test.libs << 'lib' << 'test'
13
+ test.pattern = 'test/{functional,unit}/**/*_test.rb'
14
+ test.verbose = true
15
+ end
34
16
 
35
17
  task :default => :test
data/bin/whenever CHANGED
@@ -1,8 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'rubygems'
4
3
  require 'optparse'
5
4
  require 'whenever'
5
+ require 'whenever/version'
6
6
 
7
7
  options = {}
8
8
 
@@ -20,7 +20,7 @@ OptionParser.new do |opts|
20
20
  options[:clear] = true
21
21
  options[:identifier] = identifier if identifier
22
22
  end
23
- opts.on('-s', '--set [variables]', 'Example: --set environment=staging&path=/my/sweet/path') do |set|
23
+ opts.on('-s', '--set [variables]', 'Example: --set \'environment=staging&path=/my/sweet/path\'') do |set|
24
24
  options[:set] = set if set
25
25
  end
26
26
  opts.on('-f', '--load-file [schedule file]', 'Default: config/schedule.rb') do |file|
data/bin/wheneverize CHANGED
@@ -17,17 +17,16 @@ OptionParser.new do |opts|
17
17
  end
18
18
  end
19
19
 
20
- if ARGV.empty?
21
- abort "Please specify the directory to wheneverize, e.g. `#{File.basename($0)} .'"
22
- elsif !File.exists?(ARGV.first)
23
- abort "`#{ARGV.first}' does not exist."
24
- elsif !File.directory?(ARGV.first)
25
- abort "`#{ARGV.first}' is not a directory."
26
- elsif ARGV.length > 1
27
- abort "Too many arguments; please specify only the directory to wheneverize."
20
+ unless ARGV.empty?
21
+ if !File.exists?(ARGV.first)
22
+ abort "`#{ARGV.first}' does not exist."
23
+ elsif !File.directory?(ARGV.first)
24
+ abort "`#{ARGV.first}' is not a directory."
25
+ elsif ARGV.length > 1
26
+ abort "Too many arguments; please specify only the directory to wheneverize."
27
+ end
28
28
  end
29
29
 
30
-
31
30
  content = <<-FILE
32
31
  # Use this file to easily define all of your cron jobs.
33
32
  #
@@ -52,7 +51,7 @@ content = <<-FILE
52
51
  FILE
53
52
 
54
53
  file = 'config/schedule.rb'
55
- base = ARGV.shift
54
+ base = ARGV.empty? ? '.' : ARGV.shift
56
55
 
57
56
  file = File.join(base, file)
58
57
  if File.exists?(file)
@@ -1,31 +1,62 @@
1
1
  Capistrano::Configuration.instance(:must_exist).load do
2
-
3
2
  _cset(:whenever_roles) { :db }
4
3
  _cset(:whenever_command) { "whenever" }
5
- _cset(:whenever_identifier) { application }
6
- _cset(:whenever_update_flags) { "--update-crontab #{whenever_identifier}" }
7
- _cset(:whenever_clear_flags) { "--clear-crontab #{whenever_identifier}" }
8
-
4
+ _cset(:whenever_identifier) { fetch :application }
5
+ _cset(:whenever_environment) { fetch :rails_env, "production" }
6
+ _cset(:whenever_update_flags) { "--update-crontab #{fetch :whenever_identifier} --set environment=#{fetch :whenever_environment}" }
7
+ _cset(:whenever_clear_flags) { "--clear-crontab #{fetch :whenever_identifier}" }
8
+
9
9
  # Disable cron jobs at the begining of a deploy.
10
10
  after "deploy:update_code", "whenever:clear_crontab"
11
11
  # Write the new cron jobs near the end.
12
12
  after "deploy:symlink", "whenever:update_crontab"
13
13
  # If anything goes wrong, undo.
14
14
  after "deploy:rollback", "whenever:update_crontab"
15
-
15
+
16
16
  namespace :whenever do
17
- desc "Update application's crontab entries using Whenever"
18
- task :update_crontab, :roles => whenever_roles do
19
- # Hack by Jamis to skip a task if the role has no servers defined. http://tinyurl.com/ckjgnz
20
- next if find_servers_for_task(current_task).empty?
21
- run "cd #{current_path} && #{whenever_command} #{whenever_update_flags}"
17
+ desc <<-DESC
18
+ Update application's crontab entries using Whenever. You can configure \
19
+ the command used to invoke Whenever by setting the :whenever_command \
20
+ variable, which can be used with Bundler to set the command to \
21
+ "bundle exec whenever". You can configure the identifier used by setting \
22
+ the :whenever_identifier variable, which defaults to the same value configured \
23
+ for the :application variable. You can configure the environment by setting \
24
+ the :whenever_environment variable, which defaults to the same value \
25
+ configured for the :rails_env variable which itself defaults to "production". \
26
+ Finally, you can completely override all arguments to the Whenever command \
27
+ by setting the :whenever_update_flags variable. Additionally you can configure \
28
+ which servers the crontab is updated on by setting the :whenever_roles variable.
29
+ DESC
30
+ task :update_crontab do
31
+ options = { :roles => fetch(:whenever_roles) }
32
+
33
+ if find_servers(options).any?
34
+ on_rollback do
35
+ if fetch :previous_release
36
+ run "cd #{fetch :previous_release} && #{fetch :whenever_command} #{fetch :whenever_update_flags}", options
37
+ else
38
+ run "cd #{fetch :release_path} && #{fetch :whenever_command} #{fetch :whenever_clear_flags}", options
39
+ end
40
+ end
41
+
42
+ run "cd #{fetch :current_path} && #{fetch :whenever_command} #{fetch :whenever_update_flags}", options
43
+ end
22
44
  end
23
45
 
24
- desc "Clear application's crontab entries using Whenever"
25
- task :clear_crontab, :roles => whenever_roles do
26
- next if find_servers_for_task(current_task).empty?
27
- run "cd #{release_path} && #{whenever_command} #{whenever_clear_flags}"
46
+ desc <<-DESC
47
+ Clear application's crontab entries using Whenever. You can configure \
48
+ the command used to invoke Whenever by setting the :whenever_command \
49
+ variable, which can be used with Bundler to set the command to \
50
+ "bundle exec whenever". You can configure the identifier used by setting \
51
+ the :whenever_identifier variable, which defaults to the same value configured \
52
+ for the :application variable. Finally, you can completely override all \
53
+ arguments to the Whenever command by setting the :whenever_clear_flags variable. \
54
+ Additionally you can configure which servers the crontab is cleared on by setting \
55
+ the :whenever_roles variable.
56
+ DESC
57
+ task :clear_crontab do
58
+ options = { :roles => whenever_roles }
59
+ run "cd #{fetch :release_path} && #{fetch :whenever_command} #{fetch :whenever_clear_flags}", options if find_servers(options).any?
28
60
  end
29
61
  end
30
-
31
- end
62
+ end
@@ -3,7 +3,6 @@ require 'tempfile'
3
3
 
4
4
  module Whenever
5
5
  class CommandLine
6
-
7
6
  def self.execute(options={})
8
7
  new(options).run
9
8
  end
@@ -30,7 +29,6 @@ module Whenever
30
29
  exit(1)
31
30
  end
32
31
  @options[:cut] = @options[:cut].to_i
33
-
34
32
  end
35
33
 
36
34
  def run
@@ -40,6 +38,8 @@ module Whenever
40
38
  write_crontab(whenever_cron)
41
39
  else
42
40
  puts Whenever.cron(@options)
41
+ puts "## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated."
42
+ puts "## [message] Run `whenever --help' for more options."
43
43
  exit(0)
44
44
  end
45
45
  end
@@ -51,7 +51,8 @@ module Whenever
51
51
  end
52
52
 
53
53
  def whenever_cron
54
- @whenever_cron ||= [comment_open, (Whenever.cron(@options) unless @options[:clear]), comment_close].compact.join("\n") + "\n"
54
+ return '' if @options[:clear]
55
+ @whenever_cron ||= [comment_open, Whenever.cron(@options), comment_close].compact.join("\n") + "\n"
55
56
  end
56
57
 
57
58
  def read_crontab
@@ -87,26 +88,34 @@ module Whenever
87
88
 
88
89
  def updated_crontab
89
90
  # Check for unopened or unclosed identifier blocks
90
- if read_crontab =~ Regexp.new("^#{comment_open}$") && (read_crontab =~ Regexp.new("^#{comment_close}$")).nil?
91
+ if read_crontab =~ Regexp.new("^#{comment_open}\s*$") && (read_crontab =~ Regexp.new("^#{comment_close}\s*$")).nil?
91
92
  warn "[fail] Unclosed indentifier; Your crontab file contains '#{comment_open}', but no '#{comment_close}'"
92
93
  exit(1)
93
- elsif (read_crontab =~ Regexp.new("^#{comment_open}$")).nil? && read_crontab =~ Regexp.new("^#{comment_close}$")
94
+ elsif (read_crontab =~ Regexp.new("^#{comment_open}\s*$")).nil? && read_crontab =~ Regexp.new("^#{comment_close}\s*$")
94
95
  warn "[fail] Unopened indentifier; Your crontab file contains '#{comment_close}', but no '#{comment_open}'"
95
96
  exit(1)
96
97
  end
97
98
 
98
99
  # 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}$")
100
+ if read_crontab =~ Regexp.new("^#{comment_open}\s*$") && read_crontab =~ Regexp.new("^#{comment_close}\s*$")
100
101
  # If the existing crontab file contains backslashes they get lost going through gsub.
101
102
  # .gsub('\\', '\\\\\\') preserves them. Go figure.
102
- read_crontab.gsub(Regexp.new("^#{comment_open}$.+^#{comment_close}$", Regexp::MULTILINE), whenever_cron.chomp.gsub('\\', '\\\\\\'))
103
+ read_crontab.gsub(Regexp.new("^#{comment_open}\s*$.+^#{comment_close}\s*$", Regexp::MULTILINE), whenever_cron.chomp.gsub('\\', '\\\\\\'))
103
104
  else # Otherwise, append the new cron entries after any existing ones
104
105
  [read_crontab, whenever_cron].join("\n\n")
105
- end
106
+ end.gsub(/\n{3,}/, "\n\n") # More than two newlines becomes just two.
106
107
  end
107
108
 
108
109
  def prepare(contents)
109
- contents.split("\n")[@options[:cut]..-1].join("\n")
110
+ # Strip n lines from the top of the file as specified by the :cut option.
111
+ # Use split with a -1 limit option to ensure the join is able to rebuild
112
+ # the file with all of the original seperators in-tact.
113
+ stripped_contents = contents.split($/,-1)[@options[:cut]..-1].join($/)
114
+
115
+ # Some cron implementations require all non-comment lines to be newline-
116
+ # terminated. (issue #95) Strip all newlines and replace with the default
117
+ # platform record seperator ($/)
118
+ stripped_contents.gsub!(/\s+$/, $/)
110
119
  end
111
120
 
112
121
  def comment_base
@@ -120,6 +129,5 @@ module Whenever
120
129
  def comment_close
121
130
  "# End #{comment_base}"
122
131
  end
123
-
124
132
  end
125
133
  end