moneypools-whenever 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/CHANGELOG.rdoc +75 -0
- data/README.rdoc +115 -0
- data/Rakefile +32 -0
- data/VERSION +1 -0
- data/bin/whenever +30 -0
- data/bin/wheneverize +69 -0
- data/lib/whenever/base.rb +15 -0
- data/lib/whenever/command_line.rb +108 -0
- data/lib/whenever/job_list.rb +183 -0
- data/lib/whenever/job_types/cap_task.rb +12 -0
- data/lib/whenever/job_types/default.rb +27 -0
- data/lib/whenever/job_types/rake_task.rb +12 -0
- data/lib/whenever/job_types/runner.rb +12 -0
- data/lib/whenever/outputs/cron/output_redirection.rb +62 -0
- data/lib/whenever/outputs/cron.rb +139 -0
- data/lib/whenever/version.rb +3 -0
- data/lib/whenever.rb +33 -0
- data/test/command_line_test.rb +101 -0
- data/test/cron_test.rb +226 -0
- data/test/output_at_test.rb +180 -0
- data/test/output_cap_test.rb +23 -0
- data/test/output_command_test.rb +37 -0
- data/test/output_env_test.rb +56 -0
- data/test/output_rake_test.rb +74 -0
- data/test/output_redirection_test.rb +319 -0
- data/test/output_runner_test.rb +209 -0
- data/test/test_helper.rb +30 -0
- metadata +101 -0
data/.gitignore
ADDED
data/CHANGELOG.rdoc
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
== 0.4.0 / October 20th, 2009
|
2
|
+
|
3
|
+
* New output option replaces the old cron_log option for output redirection and is much more flexible. #31 [Peer Allan]
|
4
|
+
|
5
|
+
* Reorganized the lib files (http://weblog.rubyonrails.org/2009/9/1/gem-packaging-best-practices) and switched to Jeweler from Echoe.
|
6
|
+
|
7
|
+
|
8
|
+
== 0.3.7 / September 4th, 2009
|
9
|
+
|
10
|
+
* No longer tries (and fails) to combine @shortcut jobs. #20 [Javan Makhmali]
|
11
|
+
|
12
|
+
|
13
|
+
== 0.3.6 / June 15th, 2009
|
14
|
+
|
15
|
+
* Setting a PATH in the crontab automatically based on the user's PATH. [Javan Makhmali]
|
16
|
+
|
17
|
+
|
18
|
+
== 0.3.5 / June 13th, 2009
|
19
|
+
|
20
|
+
* Added ability to accept lists of every's and at's and intelligently group them. (ex: every 'monday, wednesday', :at => ['3pm', '6am']). [Sam Ruby]
|
21
|
+
|
22
|
+
* Fixed issue with new lines. #18 [Javan Makhmali]
|
23
|
+
|
24
|
+
== 0.3.1 / June 25th, 2009
|
25
|
+
|
26
|
+
* Removed activesupport gem dependency. #1 [Javan Makhmali]
|
27
|
+
|
28
|
+
* Switched to numeric days of the week for Solaris support (and probably others). #8 [Roger Ertesvåg]
|
29
|
+
|
30
|
+
|
31
|
+
== 0.3.0 / June 2nd, 2009
|
32
|
+
|
33
|
+
* Added ability to set variables on the fly from the command line (ex: whenever --set environment=staging). [Javan Makhmali]
|
34
|
+
|
35
|
+
|
36
|
+
== 0.2.2 / April 30th, 2009
|
37
|
+
|
38
|
+
* Days of week jobs can now accept an :at directive (ex: every :monday, :at => '5pm'). [David Eisinger]
|
39
|
+
|
40
|
+
* Fixed command line test so it runs without a config/schedule.rb present. [Javan Makhmali]
|
41
|
+
|
42
|
+
* 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]
|
43
|
+
|
44
|
+
|
45
|
+
== 0.1.7 / March 5th, 2009
|
46
|
+
|
47
|
+
* 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]
|
48
|
+
|
49
|
+
|
50
|
+
== 0.1.5 / February 19th, 2009
|
51
|
+
|
52
|
+
* Fixed load path so Whenever's files don't conflict with anything in Rails. Thanks Ryan Koopmans. [Javan Makhmali]
|
53
|
+
|
54
|
+
|
55
|
+
== 0.1.4 / February 17th, 2009
|
56
|
+
|
57
|
+
* Added --load-file and --user opts to whenever binary. [Javan Makhmali]
|
58
|
+
|
59
|
+
|
60
|
+
== 0.1.3 / February 16th, 2009
|
61
|
+
|
62
|
+
* Added 'rake' helper for defining scheduled rake tasks. [Javan Makhmali]
|
63
|
+
|
64
|
+
* Renamed :cron_environment and :cron_path to :enviroment and :path for better (word) compatibility with rake tasks. [Javan Makhmali]
|
65
|
+
|
66
|
+
* Improved test load paths so tests can be run individually. [Javan Makhmali]
|
67
|
+
|
68
|
+
* Got rid of already initialized constant warning. [Javan Makhmali]
|
69
|
+
|
70
|
+
* Requiring specific gem versions: Chronic >=0.2.3 and activesupport >= 1.3.0 [Javan Makhmali]
|
71
|
+
|
72
|
+
|
73
|
+
== 0.1.0 / February 15th, 2009
|
74
|
+
|
75
|
+
* Initial release [Javan Makhmali]
|
data/README.rdoc
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
== Introduction
|
2
|
+
|
3
|
+
Whenever is a Ruby gem that provides a clear syntax for defining cron jobs. It outputs valid cron syntax and can even write your crontab file for you. It is designed to work well with Rails applications and can be deployed with Capistrano. Whenever works fine independently as well.
|
4
|
+
|
5
|
+
Ryan Bates created a great Railscast about Whenever: http://railscasts.com/episodes/164-cron-in-ruby
|
6
|
+
|
7
|
+
Discussion: http://groups.google.com/group/whenever-gem
|
8
|
+
|
9
|
+
== Installation
|
10
|
+
|
11
|
+
If you haven't already, get set up with http://gemcutter.org
|
12
|
+
|
13
|
+
$ sudo gem install whenever
|
14
|
+
|
15
|
+
In a Rails (2.1 or greater) application:
|
16
|
+
|
17
|
+
in your "config/environment.rb" file:
|
18
|
+
|
19
|
+
Rails::Initializer.run do |config|
|
20
|
+
config.gem 'whenever', :lib => false, :source => 'http://gemcutter.org/'
|
21
|
+
end
|
22
|
+
|
23
|
+
== Getting started
|
24
|
+
|
25
|
+
$ cd /my/rails/app
|
26
|
+
$ wheneverize .
|
27
|
+
|
28
|
+
This will create an initial "config/schedule.rb" file you.
|
29
|
+
|
30
|
+
== Example schedule.rb file
|
31
|
+
|
32
|
+
every 3.hours do
|
33
|
+
runner "MyModel.some_process"
|
34
|
+
rake "my:rake:task"
|
35
|
+
command "/usr/bin/my_great_command"
|
36
|
+
end
|
37
|
+
|
38
|
+
every 1.day, :at => '4:30 am' do
|
39
|
+
runner "MyModel.task_to_run_at_four_thirty_in_the_morning"
|
40
|
+
end
|
41
|
+
|
42
|
+
every :hour do # Many shortcuts available: :hour, :day, :month, :year, :reboot
|
43
|
+
runner "SomeModel.ladeeda"
|
44
|
+
end
|
45
|
+
|
46
|
+
every :sunday, :at => '12pm' do # Use any day of the week or :weekend, :weekday
|
47
|
+
runner "Task.do_something_great"
|
48
|
+
end
|
49
|
+
|
50
|
+
More examples on the wiki: http://wiki.github.com/javan/whenever/instructions-and-examples
|
51
|
+
|
52
|
+
== Cron output
|
53
|
+
|
54
|
+
$ cd /my/rails/app
|
55
|
+
$ whenever
|
56
|
+
|
57
|
+
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.
|
58
|
+
|
59
|
+
== Capistrano integration
|
60
|
+
|
61
|
+
In your "config/deploy.rb" file do something like:
|
62
|
+
|
63
|
+
after "deploy:symlink", "deploy:update_crontab"
|
64
|
+
|
65
|
+
namespace :deploy do
|
66
|
+
desc "Update the crontab file"
|
67
|
+
task :update_crontab, :roles => :db do
|
68
|
+
run "cd #{release_path} && whenever --update-crontab #{application}"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
This will update your crontab file, leaving any existing entries unharmed. When using the <code>--update-crontab</code> option, Whenever will only update the entries in your crontab file related to the current schedule.rb file. You can replace the <code>#{application}</code> with any identifying string you'd like. You can have any number of apps deploy to the same crontab file peacefully given they each use a different identifier.
|
73
|
+
|
74
|
+
If you wish to simply overwrite your crontab file each time you deploy, use the <code>--write-crontab</code> option. This is ideal if you are only working with one app and every crontab entry is contained in a single schedule.rb file.
|
75
|
+
|
76
|
+
By mixing and matching the <code>--load-file</code> and <code>--user</code> options with your various :roles in Capistrano it is entirely possible to deploy different crontab schedules under different users to all your various servers. Get creative!
|
77
|
+
|
78
|
+
If you want to override a variable (like your environment) at the time of deployment you can do so with the <code>--set</code> option: http://wiki.github.com/javan/whenever/setting-variables-on-the-fly
|
79
|
+
|
80
|
+
== Credit
|
81
|
+
|
82
|
+
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
|
83
|
+
|
84
|
+
While building Whenever, I learned a lot by digging through the source code of Capistrano - http://github.com/jamis/capistrano
|
85
|
+
|
86
|
+
== Discussion / Feedback / Issues / Bugs
|
87
|
+
|
88
|
+
For general discussion and questions, please use the google group: http://groups.google.com/group/whenever-gem
|
89
|
+
|
90
|
+
If you've found a genuine bug or issue, please use the Issues section on github: http://github.com/javan/whenever/issues
|
91
|
+
|
92
|
+
== License
|
93
|
+
|
94
|
+
Copyright (c) 2009 Javan Makhmali
|
95
|
+
|
96
|
+
Permission is hereby granted, free of charge, to any person
|
97
|
+
obtaining a copy of this software and associated documentation
|
98
|
+
files (the "Software"), to deal in the Software without
|
99
|
+
restriction, including without limitation the rights to use,
|
100
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
101
|
+
copies of the Software, and to permit persons to whom the
|
102
|
+
Software is furnished to do so, subject to the following
|
103
|
+
conditions:
|
104
|
+
|
105
|
+
The above copyright notice and this permission notice shall be
|
106
|
+
included in all copies or substantial portions of the Software.
|
107
|
+
|
108
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
109
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
110
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
111
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
112
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
113
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
114
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
115
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
require 'lib/whenever/version.rb'
|
5
|
+
|
6
|
+
begin
|
7
|
+
require 'jeweler'
|
8
|
+
Jeweler::Tasks.new do |gemspec|
|
9
|
+
gemspec.name = "moneypools-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("chronic", '>= 0.2.3')
|
17
|
+
end
|
18
|
+
Jeweler::GemcutterTasks.new
|
19
|
+
rescue LoadError
|
20
|
+
puts "Jeweler not available. Install it with: sudo gem install jeweler -s http://gemcutter.org"
|
21
|
+
end
|
22
|
+
|
23
|
+
require 'rake/testtask'
|
24
|
+
Rake::TestTask.new(:test) do |test|
|
25
|
+
test.libs << 'lib' << 'test'
|
26
|
+
test.pattern = 'test/*.rb'
|
27
|
+
test.verbose = true
|
28
|
+
end
|
29
|
+
|
30
|
+
task :test => :check_dependencies
|
31
|
+
|
32
|
+
task :default => :test
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.0
|
data/bin/whenever
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'optparse'
|
5
|
+
require 'fileutils'
|
6
|
+
require 'tempfile'
|
7
|
+
require 'whenever'
|
8
|
+
|
9
|
+
options = Hash.new
|
10
|
+
|
11
|
+
OptionParser.new do |opts|
|
12
|
+
opts.banner = "Usage: whenever [options]"
|
13
|
+
opts.on('-v', '--version') { puts "Whenever v#{Whenever::VERSION}"; exit }
|
14
|
+
opts.on('-w', '--write-crontab') { options[:write] = true }
|
15
|
+
opts.on('-i', '--update-crontab [identifier]', 'Default: full path to schedule.rb file') do |identifier|
|
16
|
+
options[:update] = true
|
17
|
+
options[:identifier] = identifier if identifier
|
18
|
+
end
|
19
|
+
opts.on('-f', '--load-file [schedule file]', 'Default: config/schedule.rb') do |file|
|
20
|
+
options[:file] = file if file
|
21
|
+
end
|
22
|
+
opts.on('-u', '--user [user]', 'Default: current user') do |user|
|
23
|
+
options[:user] = user if user
|
24
|
+
end
|
25
|
+
opts.on('-s', '--set [variables]', 'Example: --set environment=staging&path=/my/sweet/path') do |set|
|
26
|
+
options[:set] = set if set
|
27
|
+
end
|
28
|
+
end.parse!
|
29
|
+
|
30
|
+
Whenever::CommandLine.execute(options)
|
data/bin/wheneverize
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# This file is based heavily on Capistrano's `capify` command
|
4
|
+
|
5
|
+
require 'optparse'
|
6
|
+
require 'fileutils'
|
7
|
+
|
8
|
+
OptionParser.new do |opts|
|
9
|
+
opts.banner = "Usage: #{File.basename($0)} [path]"
|
10
|
+
|
11
|
+
begin
|
12
|
+
opts.parse!(ARGV)
|
13
|
+
rescue OptionParser::ParseError => e
|
14
|
+
warn e.message
|
15
|
+
puts opts
|
16
|
+
exit 1
|
17
|
+
end
|
18
|
+
end
|
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."
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
content = <<-FILE
|
32
|
+
# Use this file to easily define all of your cron jobs.
|
33
|
+
#
|
34
|
+
# It's helpful, but not entirely necessary to understand cron before proceeding.
|
35
|
+
# http://en.wikipedia.org/wiki/Cron
|
36
|
+
|
37
|
+
# Example:
|
38
|
+
#
|
39
|
+
# set :output, "/path/to/my/cron_log.log"
|
40
|
+
#
|
41
|
+
# every 2.hours do
|
42
|
+
# command "/usr/bin/some_great_command"
|
43
|
+
# runner "MyModel.some_method"
|
44
|
+
# rake "some:great:rake:task"
|
45
|
+
# end
|
46
|
+
#
|
47
|
+
# every 4.days do
|
48
|
+
# runner "AnotherModel.prune_old_records"
|
49
|
+
# end
|
50
|
+
|
51
|
+
# Learn more: http://github.com/javan/whenever
|
52
|
+
FILE
|
53
|
+
|
54
|
+
file = 'config/schedule.rb'
|
55
|
+
base = ARGV.shift
|
56
|
+
|
57
|
+
file = File.join(base, file)
|
58
|
+
if File.exists?(file)
|
59
|
+
warn "[skip] `#{file}' already exists"
|
60
|
+
elsif File.exists?(file.downcase)
|
61
|
+
warn "[skip] `#{file.downcase}' exists, which could conflict with `#{file}'"
|
62
|
+
elsif !File.exists?(File.dirname(file))
|
63
|
+
warn "[skip] directory `#{File.dirname(file)}' does not exist"
|
64
|
+
else
|
65
|
+
puts "[add] writing `#{file}'"
|
66
|
+
File.open(file, "w") { |f| f.write(content) }
|
67
|
+
end
|
68
|
+
|
69
|
+
puts "[done] wheneverized!"
|
@@ -0,0 +1,108 @@
|
|
1
|
+
module Whenever
|
2
|
+
class CommandLine
|
3
|
+
|
4
|
+
def self.execute(options={})
|
5
|
+
new(options).run
|
6
|
+
end
|
7
|
+
|
8
|
+
def initialize(options={})
|
9
|
+
@options = options
|
10
|
+
|
11
|
+
@options[:file] ||= 'config/schedule.rb'
|
12
|
+
@options[:identifier] ||= default_identifier
|
13
|
+
|
14
|
+
unless File.exists?(@options[:file])
|
15
|
+
warn("[fail] Can't find file: #{@options[:file]}")
|
16
|
+
exit(1)
|
17
|
+
end
|
18
|
+
|
19
|
+
if @options[:update] && @options[:write]
|
20
|
+
warn("[fail] Can't update AND write. choose one.")
|
21
|
+
exit(1)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def run
|
26
|
+
if @options[:update]
|
27
|
+
write_crontab(updated_crontab)
|
28
|
+
elsif @options[:write]
|
29
|
+
write_crontab(whenever_cron)
|
30
|
+
else
|
31
|
+
puts Whenever.cron(@options)
|
32
|
+
exit
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
protected
|
37
|
+
|
38
|
+
def default_identifier
|
39
|
+
File.expand_path(@options[:file])
|
40
|
+
end
|
41
|
+
|
42
|
+
def whenever_cron
|
43
|
+
@whenever_cron ||= [comment_open, Whenever.cron(@options), comment_close].join("\n") + "\n"
|
44
|
+
end
|
45
|
+
|
46
|
+
def read_crontab
|
47
|
+
return @current_crontab if @current_crontab
|
48
|
+
|
49
|
+
command = ['crontab -l']
|
50
|
+
command << "-u #{@options[:user]}" if @options[:user]
|
51
|
+
|
52
|
+
command_results = %x[#{command.join(' ')} 2> /dev/null]
|
53
|
+
@current_crontab = $?.exitstatus.zero? ? command_results : ''
|
54
|
+
end
|
55
|
+
|
56
|
+
def write_crontab(contents)
|
57
|
+
tmp_cron_file = Tempfile.new('whenever_tmp_cron').path
|
58
|
+
File.open(tmp_cron_file, File::WRONLY | File::APPEND) do |file|
|
59
|
+
file << contents
|
60
|
+
end
|
61
|
+
|
62
|
+
command = ['crontab']
|
63
|
+
command << "-u #{@options[:user]}" if @options[:user]
|
64
|
+
command << tmp_cron_file
|
65
|
+
|
66
|
+
if system(command.join(' '))
|
67
|
+
action = 'written' if @options[:write]
|
68
|
+
action = 'updated' if @options[:update]
|
69
|
+
puts "[write] crontab file #{action}"
|
70
|
+
exit
|
71
|
+
else
|
72
|
+
warn "[fail] Couldn't write crontab; try running `whenever' with no options to ensure your schedule file is valid."
|
73
|
+
exit(1)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def updated_crontab
|
78
|
+
# Check for unopened or unclosed identifier blocks
|
79
|
+
if read_crontab.index(comment_open) && !read_crontab.index(comment_close)
|
80
|
+
warn "[fail] Unclosed indentifier; Your crontab file contains '#{comment_open}', but no '#{comment_close}'"
|
81
|
+
exit(1)
|
82
|
+
elsif !read_crontab.index(comment_open) && read_crontab.index(comment_close)
|
83
|
+
warn "[fail] Unopened indentifier; Your crontab file contains '#{comment_close}', but no '#{comment_open}'"
|
84
|
+
exit(1)
|
85
|
+
end
|
86
|
+
|
87
|
+
# If an existing identier block is found, replace it with the new cron entries
|
88
|
+
if read_crontab.index(comment_open) && read_crontab.index(comment_close)
|
89
|
+
read_crontab.gsub(Regexp.new("#{comment_open}.+#{comment_close}", Regexp::MULTILINE), whenever_cron.chomp)
|
90
|
+
else # Otherwise, append the new cron entries after any existing ones
|
91
|
+
[read_crontab, whenever_cron].join("\n\n")
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def comment_base
|
96
|
+
"Whenever generated tasks for: #{@options[:identifier]}"
|
97
|
+
end
|
98
|
+
|
99
|
+
def comment_open
|
100
|
+
"# Begin #{comment_base}"
|
101
|
+
end
|
102
|
+
|
103
|
+
def comment_close
|
104
|
+
"# End #{comment_base}"
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,183 @@
|
|
1
|
+
module Whenever
|
2
|
+
class JobList
|
3
|
+
|
4
|
+
def initialize(options)
|
5
|
+
@jobs = Hash.new
|
6
|
+
@env = Hash.new
|
7
|
+
@sequences = Hash.new
|
8
|
+
|
9
|
+
case options
|
10
|
+
when String
|
11
|
+
config = options
|
12
|
+
when Hash
|
13
|
+
config = if options[:string]
|
14
|
+
options[:string]
|
15
|
+
elsif options[:file]
|
16
|
+
File.read(options[:file])
|
17
|
+
end
|
18
|
+
pre_set(options[:set])
|
19
|
+
end
|
20
|
+
|
21
|
+
eval(config)
|
22
|
+
end
|
23
|
+
|
24
|
+
def set(variable, value)
|
25
|
+
return if instance_variable_defined?("@#{variable}".to_sym)
|
26
|
+
|
27
|
+
instance_variable_set("@#{variable}".to_sym, value)
|
28
|
+
self.class.send(:attr_reader, variable.to_sym)
|
29
|
+
end
|
30
|
+
|
31
|
+
def env(variable, value)
|
32
|
+
@env[variable.to_s] = value
|
33
|
+
end
|
34
|
+
|
35
|
+
def every(frequency, options = {})
|
36
|
+
@current_time_scope = frequency
|
37
|
+
@options = options
|
38
|
+
yield
|
39
|
+
end
|
40
|
+
|
41
|
+
def in_sequence(options={})
|
42
|
+
@current_sequence = JobSequence.new(options)
|
43
|
+
yield
|
44
|
+
@jobs[@current_time_scope] << @current_sequence
|
45
|
+
@current_sequence = nil
|
46
|
+
end
|
47
|
+
|
48
|
+
def command(task, options = {})
|
49
|
+
# :cron_log was an old option for output redirection, it remains for backwards compatibility
|
50
|
+
options[:output] = (options[:cron_log] || @cron_log) if defined?(@cron_log) || options.has_key?(:cron_log)
|
51
|
+
# :output is the newer, more flexible option.
|
52
|
+
options[:output] = @output if defined?(@output) && !options.has_key?(:output)
|
53
|
+
options[:class] ||= Whenever::Job::Default
|
54
|
+
|
55
|
+
@jobs[@current_time_scope] ||= []
|
56
|
+
|
57
|
+
if @current_sequence
|
58
|
+
@current_sequence << options[:class].new(@options.merge(:task => task).merge(options))
|
59
|
+
else
|
60
|
+
@jobs[@current_time_scope] << options[:class].new(@options.merge(:task => task).merge(options))
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def runner(task, options = {})
|
65
|
+
options.reverse_merge!(:environment => @environment, :path => @path)
|
66
|
+
options[:class] = Whenever::Job::Runner
|
67
|
+
command(task, options)
|
68
|
+
end
|
69
|
+
|
70
|
+
def rake(task, options = {})
|
71
|
+
options.reverse_merge!(:environment => @environment, :path => @path)
|
72
|
+
options[:class] = Whenever::Job::RakeTask
|
73
|
+
command(task, options)
|
74
|
+
end
|
75
|
+
|
76
|
+
def cap(task, options = {})
|
77
|
+
options.reverse_merge!(:environment => @environment, :path => @path)
|
78
|
+
options[:class] = Whenever::Job::CapTask
|
79
|
+
command(task, options)
|
80
|
+
end
|
81
|
+
|
82
|
+
def generate_cron_output
|
83
|
+
set_path_environment_variable
|
84
|
+
|
85
|
+
[environment_variables, cron_jobs].compact.join
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
|
90
|
+
#
|
91
|
+
# Takes a string like: "variable1=something&variable2=somethingelse"
|
92
|
+
# and breaks it into variable/value pairs. Used for setting variables at runtime from the command line.
|
93
|
+
# Only works for setting values as strings.
|
94
|
+
#
|
95
|
+
def pre_set(variable_string = nil)
|
96
|
+
return if variable_string.blank?
|
97
|
+
|
98
|
+
pairs = variable_string.split('&')
|
99
|
+
pairs.each do |pair|
|
100
|
+
next unless pair.index('=')
|
101
|
+
variable, value = *pair.split('=')
|
102
|
+
set(variable.strip, value.strip) unless variable.blank? || value.blank?
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def set_path_environment_variable
|
107
|
+
return if path_should_not_be_set_automatically?
|
108
|
+
@env[:PATH] = read_path unless read_path.blank?
|
109
|
+
end
|
110
|
+
|
111
|
+
def read_path
|
112
|
+
ENV['PATH'] if ENV
|
113
|
+
end
|
114
|
+
|
115
|
+
def path_should_not_be_set_automatically?
|
116
|
+
@set_path_automatically === false || @env[:PATH] || @env["PATH"]
|
117
|
+
end
|
118
|
+
|
119
|
+
def environment_variables
|
120
|
+
return if @env.empty?
|
121
|
+
|
122
|
+
output = []
|
123
|
+
@env.each do |key, val|
|
124
|
+
output << "#{key}=#{val}\n"
|
125
|
+
end
|
126
|
+
output << "\n"
|
127
|
+
|
128
|
+
output.join
|
129
|
+
end
|
130
|
+
|
131
|
+
#
|
132
|
+
# Takes the standard cron output that Whenever generates and finds
|
133
|
+
# similar entries that can be combined. For example: If a job should run
|
134
|
+
# at 3:02am and 4:02am, instead of creating two jobs this method combines
|
135
|
+
# them into one that runs on the 2nd minute at the 3rd and 4th hour.
|
136
|
+
#
|
137
|
+
def combine(entries)
|
138
|
+
entries.map! { |entry| entry.split(/ +/, 6) }
|
139
|
+
0.upto(4) do |f|
|
140
|
+
(entries.length-1).downto(1) do |i|
|
141
|
+
next if entries[i][f] == '*'
|
142
|
+
comparison = entries[i][0...f] + entries[i][f+1..-1]
|
143
|
+
(i-1).downto(0) do |j|
|
144
|
+
next if entries[j][f] == '*'
|
145
|
+
if comparison == entries[j][0...f] + entries[j][f+1..-1]
|
146
|
+
entries[j][f] += ',' + entries[i][f]
|
147
|
+
entries.delete_at(i)
|
148
|
+
break
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
entries.map { |entry| entry.join(' ') }
|
155
|
+
end
|
156
|
+
|
157
|
+
def cron_jobs
|
158
|
+
return if @jobs.empty?
|
159
|
+
|
160
|
+
shortcut_jobs = []
|
161
|
+
regular_jobs = []
|
162
|
+
|
163
|
+
@jobs.each do |time, jobs|
|
164
|
+
jobs.each do |job|
|
165
|
+
job = job.to_single_job if job.is_a?(Whenever::JobSequence)
|
166
|
+
|
167
|
+
Whenever::Output::Cron.output(time, job) do |cron|
|
168
|
+
cron << "\n\n"
|
169
|
+
|
170
|
+
if cron.starts_with?("@")
|
171
|
+
shortcut_jobs << cron
|
172
|
+
else
|
173
|
+
regular_jobs << cron
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
shortcut_jobs.join + combine(regular_jobs).join
|
180
|
+
end
|
181
|
+
|
182
|
+
end
|
183
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Whenever
|
2
|
+
module Job
|
3
|
+
class Default
|
4
|
+
|
5
|
+
attr_accessor :task, :at, :output, :output_redirection
|
6
|
+
|
7
|
+
def initialize(options = {})
|
8
|
+
@task = options[:task]
|
9
|
+
@at = options[:at]
|
10
|
+
@output_redirection = options.has_key?(:output) ? options[:output] : :not_set
|
11
|
+
@environment = options[:environment] || :production
|
12
|
+
@path = options[:path] || Whenever.path
|
13
|
+
end
|
14
|
+
|
15
|
+
def output
|
16
|
+
task
|
17
|
+
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
|
21
|
+
def path_required
|
22
|
+
raise ArgumentError, "No path available; set :path, '/your/path' in your schedule file" if @path.blank?
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|