whenever 0.5.3 → 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,11 +1,61 @@
1
- == 0.5.3 / September 24th, 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
43
+
44
+ * Detect script/rails file and change runner to Rails 3 style if found. [Javan Makhmali]
45
+
46
+ * Created a new :job_template system that can be applied to all commands. Wraps all in bash -l -c 'command..' by default now for better RVM support. Stopped automatically setting the PATH too. [Javan Makhmali]
47
+
48
+ * Added a built-in Capistrano recipe. [Javan Makhmali]
49
+
50
+
51
+ ### 0.5.3 / September 24th, 2010
2
52
 
3
53
  * Better regexes for replacing Whenever blocks in the crontab. #45 [Javan Makhmali]
4
54
 
5
55
  * Preserving backslashes when updating existing crontab. #82 [Javan Makhmali]
6
56
 
7
57
 
8
- == 0.5.2 / September 15th, 2010
58
+ ### 0.5.2 / September 15th, 2010
9
59
 
10
60
  * Quotes automatically escaped in jobs. [Jay Adkisson]
11
61
 
@@ -16,7 +66,7 @@
16
66
  * Lots of internal reorganizing; tests broken into unit and functional. [Javan Makhmali]
17
67
 
18
68
 
19
- == 0.5.0 / June 28th, 2010
69
+ ### 0.5.0 / June 28th, 2010
20
70
 
21
71
  * New job_type API for writing custom jobs. Internals use this to define command, runner, and rake. [Javan Makhmali - inspired by idlefingers (Damien)]
22
72
 
@@ -25,7 +75,7 @@
25
75
  * --clear option to remove crontab entries for a specific [identifier]. [mraidel (Michael Raidel)]
26
76
 
27
77
 
28
- == 0.4.2 / April 26th, 2010
78
+ ### 0.4.2 / April 26th, 2010
29
79
 
30
80
  * runners now cd into the app's directory and then execute. [Michael Guterl]
31
81
 
@@ -36,49 +86,49 @@
36
86
  * bugfix: comparison Time with 0 failed. #32 [Dan Hixon]
37
87
 
38
88
 
39
- == 0.4.1 / November 30th, 2009
89
+ ### 0.4.1 / November 30th, 2009
40
90
 
41
91
  * exit(0) instead of just exit to make JRuby happy. [Elan Meng]
42
92
 
43
93
  * Fixed activesupport deprecation warning by requiring active_support. #37 [Andrew Nesbitt]
44
94
 
45
95
 
46
- == 0.4.0 / October 20th, 2009
96
+ ### 0.4.0 / October 20th, 2009
47
97
 
48
98
  * New output option replaces the old cron_log option for output redirection and is much more flexible. #31 [Peer Allan]
49
99
 
50
100
  * Reorganized the lib files (http://weblog.rubyonrails.org/2009/9/1/gem-packaging-best-practices) and switched to Jeweler from Echoe.
51
101
 
52
102
 
53
- == 0.3.7 / September 4th, 2009
103
+ ### 0.3.7 / September 4th, 2009
54
104
 
55
105
  * No longer tries (and fails) to combine @shortcut jobs. #20 [Javan Makhmali]
56
106
 
57
107
 
58
- == 0.3.6 / June 15th, 2009
108
+ ### 0.3.6 / June 15th, 2009
59
109
 
60
110
  * Setting a PATH in the crontab automatically based on the user's PATH. [Javan Makhmali]
61
111
 
62
112
 
63
- == 0.3.5 / June 13th, 2009
113
+ ### 0.3.5 / June 13th, 2009
64
114
 
65
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]
66
116
 
67
117
  * Fixed issue with new lines. #18 [Javan Makhmali]
68
118
 
69
- == 0.3.1 / June 25th, 2009
119
+ ### 0.3.1 / June 25th, 2009
70
120
 
71
121
  * Removed activesupport gem dependency. #1 [Javan Makhmali]
72
122
 
73
123
  * Switched to numeric days of the week for Solaris support (and probably others). #8 [Roger Ertesvåg]
74
124
 
75
125
 
76
- == 0.3.0 / June 2nd, 2009
126
+ ### 0.3.0 / June 2nd, 2009
77
127
 
78
128
  * Added ability to set variables on the fly from the command line (ex: whenever --set environment=staging). [Javan Makhmali]
79
129
 
80
130
 
81
- == 0.2.2 / April 30th, 2009
131
+ ### 0.2.2 / April 30th, 2009
82
132
 
83
133
  * Days of week jobs can now accept an :at directive (ex: every :monday, :at => '5pm'). [David Eisinger]
84
134
 
@@ -87,22 +137,22 @@
87
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]
88
138
 
89
139
 
90
- == 0.1.7 / March 5th, 2009
140
+ ### 0.1.7 / March 5th, 2009
91
141
 
92
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]
93
143
 
94
144
 
95
- == 0.1.5 / February 19th, 2009
145
+ ### 0.1.5 / February 19th, 2009
96
146
 
97
147
  * Fixed load path so Whenever's files don't conflict with anything in Rails. Thanks Ryan Koopmans. [Javan Makhmali]
98
148
 
99
149
 
100
- == 0.1.4 / February 17th, 2009
150
+ ### 0.1.4 / February 17th, 2009
101
151
 
102
152
  * Added --load-file and --user opts to whenever binary. [Javan Makhmali]
103
153
 
104
154
 
105
- == 0.1.3 / February 16th, 2009
155
+ ### 0.1.3 / February 16th, 2009
106
156
 
107
157
  * Added 'rake' helper for defining scheduled rake tasks. [Javan Makhmali]
108
158
 
@@ -115,6 +165,6 @@
115
165
  * Requiring specific gem versions: Chronic >=0.2.3 and activesupport >= 1.3.0 [Javan Makhmali]
116
166
 
117
167
 
118
- == 0.1.0 / February 15th, 2009
168
+ ### 0.1.0 / February 15th, 2009
119
169
 
120
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
data/README.md ADDED
@@ -0,0 +1,150 @@
1
+ ### Introduction
2
+
3
+ Whenever is a Ruby gem that provides a clear syntax for writing and deploying cron jobs.
4
+
5
+ ### Installation
6
+
7
+ $ gem install whenever
8
+
9
+ Or with Bundler in your Gemfile.
10
+
11
+ gem 'whenever', :require => false
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
+ every '0 0 27-31 * *' do
41
+ command "echo 'you can use raw cron sytax too'"
42
+ end
43
+
44
+ More examples on the wiki: <http://wiki.github.com/javan/whenever/instructions-and-examples>
45
+
46
+ ### Define your own job types
47
+
48
+ Whenever ships with three pre-defined job types: command, runner, and rake. You can define your own with `job_type`.
49
+
50
+ For example:
51
+
52
+ job_type :awesome, '/usr/local/bin/awesome :task :fun_level'
53
+
54
+ every 2.hours do
55
+ awesome "party", :fun_level => "extreme"
56
+ end
57
+
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`.
59
+
60
+ The default job types that ship with Whenever are defined like so:
61
+
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"
65
+
66
+ If a script/rails file is detected (like in a Rails 3 app), runner will be defined to fit:
67
+
68
+ job_type :runner, "cd :path && script/rails runner -e :environment ':task' :output"
69
+
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>
71
+
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>
73
+
74
+ You can change this by setting your own `:job_template`.
75
+
76
+ set :job_template, "bash -l -c ':job'"
77
+
78
+ Or set the job_template to nil to have your jobs execute normally.
79
+
80
+ set :job_template, nil
81
+
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.
83
+
84
+ ### Capistrano integration
85
+
86
+ Use the built-in Capistrano recipe for easy crontab updates with deploys.
87
+
88
+ In your "config/deploy.rb" file:
89
+
90
+ require "whenever/capistrano"
91
+
92
+ Take a look at the recipe for options you can set. <http://github.com/javan/whenever/blob/master/lib/whenever/capistrano.rb>
93
+ For example, if you're using bundler do this:
94
+
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.
104
+
105
+ ### The `whenever` command
106
+
107
+ $ cd /my/rails/app
108
+ $ whenever
109
+
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.
111
+
112
+ ### Credit
113
+
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>
115
+
116
+ Thanks to all the contributors who have made it even better: <http://github.com/javan/whenever/contributors>
117
+
118
+ ### Discussion / Feedback / Issues / Bugs
119
+
120
+ For general discussion and questions, please use the google group: <http://groups.google.com/group/whenever-gem>
121
+
122
+ If you've found a genuine bug or issue, please use the Issues section on github: <http://github.com/javan/whenever/issues>
123
+
124
+ Ryan Bates created a great Railscast about Whenever: <http://railscasts.com/episodes/164-cron-in-ruby>
125
+ It's a little bit dated now, but remains a good introduction.
126
+
127
+ ### License
128
+
129
+ Copyright (c) 2009+ Javan Makhmali
130
+
131
+ Permission is hereby granted, free of charge, to any person
132
+ obtaining a copy of this software and associated documentation
133
+ files (the "Software"), to deal in the Software without
134
+ restriction, including without limitation the rights to use,
135
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
136
+ copies of the Software, and to permit persons to whom the
137
+ Software is furnished to do so, subject to the following
138
+ conditions:
139
+
140
+ The above copyright notice and this permission notice shall be
141
+ included in all copies or substantial portions of the Software.
142
+
143
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
144
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
145
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
146
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
147
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
148
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
149
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
150
+ OTHER DEALINGS IN THE SOFTWARE.
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 defining and deploying messy cron jobs."
12
- gemspec.description = "Clean ruby syntax for defining and deploying messy cron jobs."
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
@@ -4,32 +4,35 @@ require 'rubygems'
4
4
  require 'optparse'
5
5
  require 'whenever'
6
6
 
7
- options = Hash.new
7
+ options = {}
8
8
 
9
9
  OptionParser.new do |opts|
10
10
  opts.banner = "Usage: whenever [options]"
11
- opts.on('-v', '--version') { puts "Whenever v#{Whenever::VERSION}"; exit(0) }
12
- opts.on('-w', '--write-crontab') { options[:write] = true }
13
- opts.on('-c', '--cut [lines]', 'Cut lines from the top of the cronfile') do |lines|
14
- options[:cut] = lines.to_i if lines
15
- end
16
11
  opts.on('-i', '--update-crontab [identifier]', 'Default: full path to schedule.rb file') do |identifier|
17
12
  options[:update] = true
18
13
  options[:identifier] = identifier if identifier
19
14
  end
15
+ opts.on('-w', '--write-crontab [identifier]', 'Default: full path to schedule.rb file') do |identifier|
16
+ options[:write] = true
17
+ options[:identifier] = identifier if identifier
18
+ end
20
19
  opts.on('-c', '--clear-crontab [identifier]') do |identifier|
21
20
  options[:clear] = true
22
21
  options[:identifier] = identifier if identifier
23
22
  end
23
+ opts.on('-s', '--set [variables]', 'Example: --set environment=staging&path=/my/sweet/path') do |set|
24
+ options[:set] = set if set
25
+ end
24
26
  opts.on('-f', '--load-file [schedule file]', 'Default: config/schedule.rb') do |file|
25
27
  options[:file] = file if file
26
28
  end
27
29
  opts.on('-u', '--user [user]', 'Default: current user') do |user|
28
30
  options[:user] = user if user
29
31
  end
30
- opts.on('-s', '--set [variables]', 'Example: --set environment=staging&path=/my/sweet/path') do |set|
31
- options[:set] = set if set
32
+ opts.on('-k', '--cut [lines]', 'Cut lines from the top of the cronfile') do |lines|
33
+ options[:cut] = lines.to_i if lines
32
34
  end
35
+ opts.on('-v', '--version') { puts "Whenever v#{Whenever::VERSION}"; exit(0) }
33
36
  end.parse!
34
37
 
35
38
  Whenever::CommandLine.execute(options)
data/bin/wheneverize CHANGED
File without changes
@@ -0,0 +1,32 @@
1
+ Capistrano::Configuration.instance(:must_exist).load do
2
+
3
+ _cset(:whenever_roles) { :db }
4
+ _cset(:whenever_command) { "whenever" }
5
+ _cset(:whenever_identifier) { application }
6
+ _cset(:whenever_environment) { "production" }
7
+ _cset(:whenever_update_flags) { "--update-crontab #{whenever_identifier} --set environment=#{whenever_environment}" }
8
+ _cset(:whenever_clear_flags) { "--clear-crontab #{whenever_identifier}" }
9
+
10
+ # Disable cron jobs at the begining of a deploy.
11
+ after "deploy:update_code", "whenever:clear_crontab"
12
+ # Write the new cron jobs near the end.
13
+ after "deploy:symlink", "whenever:update_crontab"
14
+ # If anything goes wrong, undo.
15
+ after "deploy:rollback", "whenever:update_crontab"
16
+
17
+ namespace :whenever do
18
+ desc "Update application's crontab entries using Whenever"
19
+ task :update_crontab, :roles => whenever_roles do
20
+ # Hack by Jamis to skip a task if the role has no servers defined. http://tinyurl.com/ckjgnz
21
+ next if find_servers_for_task(current_task).empty?
22
+ run "cd #{current_path} && #{whenever_command} #{whenever_update_flags}"
23
+ end
24
+
25
+ desc "Clear application's crontab entries using Whenever"
26
+ task :clear_crontab, :roles => whenever_roles do
27
+ next if find_servers_for_task(current_task).empty?
28
+ run "cd #{release_path} && #{whenever_command} #{whenever_clear_flags}"
29
+ end
30
+ end
31
+
32
+ end
@@ -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,19 +1,24 @@
1
1
  module Whenever
2
2
  module Output
3
3
  class Cron
4
+ REGEX = /^.+ .+ .+ .+ .+.?$/
4
5
 
5
6
  attr_accessor :time, :task
6
7
 
7
- def initialize(time = nil, task = nil, at = nil, output_redirection = nil)
8
+ def initialize(time = nil, task = nil, at = nil)
8
9
  @time = time
9
10
  @task = task
10
11
  @at = at.is_a?(String) ? (Chronic.parse(at) || 0) : (at || 0)
11
- @output_redirection = output_redirection
12
12
  end
13
13
 
14
- def self.enumerate(item)
14
+ def self.enumerate(item, detect_cron = true)
15
15
  if item and item.is_a?(String)
16
- items = item.split(',')
16
+ items =
17
+ if detect_cron && item =~ REGEX
18
+ [item]
19
+ else
20
+ item.split(',')
21
+ end
17
22
  else
18
23
  items = item
19
24
  items = [items] unless items and items.respond_to?(:each)
@@ -23,42 +28,48 @@ module Whenever
23
28
 
24
29
  def self.output(times, job)
25
30
  enumerate(times).each do |time|
26
- enumerate(job.at).each do |at|
27
- yield new(time, job.output, at, job.output_redirection).output
31
+ enumerate(job.at, false).each do |at|
32
+ yield new(time, job.output, at).output
28
33
  end
29
34
  end
30
35
  end
31
36
 
32
37
  def output
33
- [time_in_cron_syntax, task, output_redirection].compact.join(' ').strip
38
+ [time_in_cron_syntax, task].compact.join(' ').strip
34
39
  end
35
40
 
36
41
  def time_in_cron_syntax
37
42
  case @time
43
+ when REGEX then @time # raw cron sytax given
38
44
  when Symbol then parse_symbol
39
45
  when String then parse_as_string
40
46
  else parse_time
41
47
  end
42
48
  end
43
-
44
- def output_redirection
45
- Whenever::Output::Cron::OutputRedirection.new(@output_redirection).to_s unless @output_redirection == :not_set
46
- end
47
49
 
48
50
  protected
49
51
 
50
52
  def parse_symbol
51
53
  shortcut = case @time
52
- when :reboot then '@reboot'
53
- when :year, :yearly then '@annually'
54
- when :day, :daily then '@daily'
55
- when :midnight then '@midnight'
56
- when :month, :monthly then '@monthly'
57
- when :week, :weekly then '@weekly'
58
- 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'
59
67
  end
60
68
 
61
- if shortcut
69
+ if shortcut.is_a?(Numeric)
70
+ @time = shortcut
71
+ parse_time
72
+ elsif shortcut
62
73
  if @at.is_a?(Time) || (@at.is_a?(Numeric) && @at > 0)
63
74
  raise ArgumentError, "You cannot specify an ':at' when using the shortcuts for times."
64
75
  else
data/lib/whenever/job.rb CHANGED
@@ -1,21 +1,29 @@
1
1
  module Whenever
2
2
  class Job
3
3
 
4
- attr_reader :at, :output_redirection
4
+ attr_reader :at
5
5
 
6
6
  def initialize(options = {})
7
7
  @options = options
8
-
9
- @at = options[:at]
10
- @output_redirection = options.has_key?(:output) ? options[:output] : :not_set
8
+ @at = options.delete(:at)
9
+ @template = options.delete(:template)
10
+ @job_template = options.delete(:job_template) || ":job"
11
+ @options[:output] = Whenever::Output::Redirection.new(options[:output]).to_s if options.has_key?(:output)
11
12
  @options[:environment] ||= :production
12
13
  @options[:path] ||= Whenever.path
13
14
  end
14
15
 
15
16
  def output
16
- @options[:template].dup.gsub(/:\w+/) do |key|
17
+ job = process_template(@template, @options).strip
18
+ process_template(@job_template, { :job => job }).strip
19
+ end
20
+
21
+ protected
22
+
23
+ def process_template(template, options)
24
+ template.gsub(/:\w+/) do |key|
17
25
  before_and_after = [$`[-1..-1], $'[0..0]]
18
- option = @options[key.sub(':', '').to_sym]
26
+ option = options[key.sub(':', '').to_sym]
19
27
 
20
28
  if before_and_after.all? { |c| c == "'" }
21
29
  escape_single_quotes(option)
@@ -26,8 +34,6 @@ module Whenever
26
34
  end
27
35
  end
28
36
  end
29
-
30
- protected
31
37
 
32
38
  def escape_single_quotes(str)
33
39
  str.gsub(/'/) { "'\\''" }