le1t0-whenever 0.4.2.001

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .DS_Store
2
+ pkg
3
+ docle1t0-whenever.gemspec
4
+ pkg/*
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,93 @@
1
+ == 0.4.2 / April 26th, 2010
2
+
3
+ * runners now cd into the app's directory and then execute. [Michael Guterl]
4
+
5
+ * Fix STDERR output redirection to file to append instead of overwrite. [weplay]
6
+
7
+ * Move require of tempfile lib to file that actually uses it. [Finn Smith]
8
+
9
+ * bugfix: comparison Time with 0 failed. #32 [Dan Hixon]
10
+
11
+
12
+ == 0.4.1 / November 30th, 2009
13
+
14
+ * exit(0) instead of just exit to make JRuby happy. [Elan Meng]
15
+
16
+ * Fixed activesupport deprecation warning by requiring active_support. #37 [Andrew Nesbitt]
17
+
18
+
19
+ == 0.4.0 / October 20th, 2009
20
+
21
+ * New output option replaces the old cron_log option for output redirection and is much more flexible. #31 [Peer Allan]
22
+
23
+ * Reorganized the lib files (http://weblog.rubyonrails.org/2009/9/1/gem-packaging-best-practices) and switched to Jeweler from Echoe.
24
+
25
+
26
+ == 0.3.7 / September 4th, 2009
27
+
28
+ * No longer tries (and fails) to combine @shortcut jobs. #20 [Javan Makhmali]
29
+
30
+
31
+ == 0.3.6 / June 15th, 2009
32
+
33
+ * Setting a PATH in the crontab automatically based on the user's PATH. [Javan Makhmali]
34
+
35
+
36
+ == 0.3.5 / June 13th, 2009
37
+
38
+ * Added ability to accept lists of every's and at's and intelligently group them. (ex: every 'monday, wednesday', :at => ['3pm', '6am']). [Sam Ruby]
39
+
40
+ * Fixed issue with new lines. #18 [Javan Makhmali]
41
+
42
+ == 0.3.1 / June 25th, 2009
43
+
44
+ * Removed activesupport gem dependency. #1 [Javan Makhmali]
45
+
46
+ * Switched to numeric days of the week for Solaris support (and probably others). #8 [Roger Ertesvåg]
47
+
48
+
49
+ == 0.3.0 / June 2nd, 2009
50
+
51
+ * Added ability to set variables on the fly from the command line (ex: whenever --set environment=staging). [Javan Makhmali]
52
+
53
+
54
+ == 0.2.2 / April 30th, 2009
55
+
56
+ * Days of week jobs can now accept an :at directive (ex: every :monday, :at => '5pm'). [David Eisinger]
57
+
58
+ * Fixed command line test so it runs without a config/schedule.rb present. [Javan Makhmali]
59
+
60
+ * 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]
61
+
62
+
63
+ == 0.1.7 / March 5th, 2009
64
+
65
+ * 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]
66
+
67
+
68
+ == 0.1.5 / February 19th, 2009
69
+
70
+ * Fixed load path so Whenever's files don't conflict with anything in Rails. Thanks Ryan Koopmans. [Javan Makhmali]
71
+
72
+
73
+ == 0.1.4 / February 17th, 2009
74
+
75
+ * Added --load-file and --user opts to whenever binary. [Javan Makhmali]
76
+
77
+
78
+ == 0.1.3 / February 16th, 2009
79
+
80
+ * Added 'rake' helper for defining scheduled rake tasks. [Javan Makhmali]
81
+
82
+ * Renamed :cron_environment and :cron_path to :enviroment and :path for better (word) compatibility with rake tasks. [Javan Makhmali]
83
+
84
+ * Improved test load paths so tests can be run individually. [Javan Makhmali]
85
+
86
+ * Got rid of already initialized constant warning. [Javan Makhmali]
87
+
88
+ * Requiring specific gem versions: Chronic >=0.2.3 and activesupport >= 1.3.0 [Javan Makhmali]
89
+
90
+
91
+ == 0.1.0 / February 15th, 2009
92
+
93
+ * Initial release [Javan Makhmali]
data/README.rdoc ADDED
@@ -0,0 +1,105 @@
1
+ == Introduction
2
+
3
+ Whenever is a Ruby gem that provides a clear syntax for defining cron jobs. It outputs valid cron syntax and can even write your crontab file for you. It is designed to work well with Rails applications and can be deployed with Capistrano. Whenever works fine independently as well.
4
+
5
+ Ryan Bates created a great Railscast about Whenever: http://railscasts.com/episodes/164-cron-in-ruby
6
+
7
+ Discussion: http://groups.google.com/group/whenever-gem
8
+
9
+ == Installation
10
+
11
+ $ sudo gem install whenever
12
+
13
+ == Getting started
14
+
15
+ $ cd /my/rails/app
16
+ $ wheneverize .
17
+
18
+ This will create an initial "config/schedule.rb" file you.
19
+
20
+ == Example schedule.rb file
21
+
22
+ every 3.hours do
23
+ runner "MyModel.some_process"
24
+ rake "my:rake:task"
25
+ command "/usr/bin/my_great_command"
26
+ end
27
+
28
+ every 1.day, :at => '4:30 am' do
29
+ runner "MyModel.task_to_run_at_four_thirty_in_the_morning"
30
+ end
31
+
32
+ every :hour do # Many shortcuts available: :hour, :day, :month, :year, :reboot
33
+ runner "SomeModel.ladeeda"
34
+ end
35
+
36
+ every :sunday, :at => '12pm' do # Use any day of the week or :weekend, :weekday
37
+ runner "Task.do_something_great"
38
+ end
39
+
40
+ More examples on the wiki: http://wiki.github.com/javan/whenever/instructions-and-examples
41
+
42
+ == Cron output
43
+
44
+ $ cd /my/rails/app
45
+ $ whenever
46
+
47
+ And you'll see your schedule.rb converted to cron sytax. Note: running `whenever' with no options does not display your current crontab file, it simply shows you the output of converting your schedule.rb file.
48
+
49
+ == Capistrano integration
50
+
51
+ In your "config/deploy.rb" file do something like:
52
+
53
+ after "deploy:symlink", "deploy:update_crontab"
54
+
55
+ namespace :deploy do
56
+ desc "Update the crontab file"
57
+ task :update_crontab, :roles => :db do
58
+ run "cd #{release_path} && whenever --update-crontab #{application}"
59
+ end
60
+ end
61
+
62
+ This will update your crontab file, leaving any existing entries unharmed. When using the <code>--update-crontab</code> option, Whenever will only update the entries in your crontab file related to the current schedule.rb file. You can replace the <code>#{application}</code> with any identifying string you'd like. You can have any number of apps deploy to the same crontab file peacefully given they each use a different identifier.
63
+
64
+ If you wish to simply overwrite your crontab file each time you deploy, use the <code>--write-crontab</code> option. This is ideal if you are only working with one app and every crontab entry is contained in a single schedule.rb file.
65
+
66
+ By mixing and matching the <code>--load-file</code> and <code>--user</code> options with your various :roles in Capistrano it is entirely possible to deploy different crontab schedules under different users to all your various servers. Get creative!
67
+
68
+ If you want to override a variable (like your environment) at the time of deployment you can do so with the <code>--set</code> option: http://wiki.github.com/javan/whenever/setting-variables-on-the-fly
69
+
70
+ == Credit
71
+
72
+ Whenever was created for use at Inkling (http://inklingmarkets.com) where I work. Their take on it: http://blog.inklingmarkets.com/2009/02/whenever-easy-way-to-do-cron-jobs-from.html
73
+
74
+ While building Whenever, I learned a lot by digging through the source code of Capistrano - http://github.com/jamis/capistrano
75
+
76
+ == Discussion / Feedback / Issues / Bugs
77
+
78
+ For general discussion and questions, please use the google group: http://groups.google.com/group/whenever-gem
79
+
80
+ If you've found a genuine bug or issue, please use the Issues section on github: http://github.com/javan/whenever/issues
81
+
82
+ == License
83
+
84
+ Copyright (c) 2009+ Javan Makhmali
85
+
86
+ Permission is hereby granted, free of charge, to any person
87
+ obtaining a copy of this software and associated documentation
88
+ files (the "Software"), to deal in the Software without
89
+ restriction, including without limitation the rights to use,
90
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
91
+ copies of the Software, and to permit persons to whom the
92
+ Software is furnished to do so, subject to the following
93
+ conditions:
94
+
95
+ The above copyright notice and this permission notice shall be
96
+ included in all copies or substantial portions of the Software.
97
+
98
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
99
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
100
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
101
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
102
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
103
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
104
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
105
+ OTHER DEALINGS IN THE SOFTWARE.
data/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 = "le1t0-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 = "dev@ewout.to"
14
+ gemspec.homepage = "http://github.com/le1t0/whenever"
15
+ gemspec.authors = ["Le1t0"]
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/bin/whenever ADDED
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'optparse'
5
+ require 'whenever'
6
+
7
+ options = Hash.new
8
+
9
+ OptionParser.new do |opts|
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('-i', '--update-crontab [identifier]', 'Default: full path to schedule.rb file') do |identifier|
14
+ options[:update] = true
15
+ options[:identifier] = identifier if identifier
16
+ end
17
+ opts.on('-f', '--load-file [schedule file]', 'Default: config/schedule.rb') do |file|
18
+ options[:file] = file if file
19
+ end
20
+ opts.on('-u', '--user [user]', 'Default: current user') do |user|
21
+ options[:user] = user if user
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
26
+ end.parse!
27
+
28
+ 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!"
data/lib/whenever.rb ADDED
@@ -0,0 +1,31 @@
1
+ require 'chronic'
2
+
3
+ # Hoping to load Rails' Rakefile
4
+ begin
5
+ load 'Rakefile'
6
+ rescue LoadError
7
+ nil
8
+ end
9
+
10
+ # If Rails' rakefile was loaded than so was active_support, but
11
+ # if this is being used in a non-rails enviroment we need to require it.
12
+ # It was previously defined as a dependency of this gem, but that became
13
+ # problematic. See: http://github.com/javan/whenever/issues#issue/1
14
+ begin
15
+ require 'active_support/all'
16
+ rescue LoadError
17
+ warn 'To use Whenever you need the active_support gem:'
18
+ warn '$ gem install activesupport'
19
+ exit(1)
20
+ end
21
+
22
+ # Whenever files
23
+ require 'whenever/base'
24
+ require 'whenever/job_list'
25
+ require 'whenever/job_types/default'
26
+ require 'whenever/job_types/rake_task'
27
+ require 'whenever/job_types/runner'
28
+ require 'whenever/outputs/cron'
29
+ require 'whenever/outputs/cron/output_redirection'
30
+ require 'whenever/command_line'
31
+ require 'whenever/version'
@@ -0,0 +1,15 @@
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
+ if defined?(RAILS_ROOT)
9
+ RAILS_ROOT
10
+ elsif defined?(::RAILS_ROOT)
11
+ ::RAILS_ROOT
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,111 @@
1
+ require 'fileutils'
2
+ require 'tempfile'
3
+
4
+ module Whenever
5
+ class CommandLine
6
+
7
+ def self.execute(options={})
8
+ new(options).run
9
+ end
10
+
11
+ def initialize(options={})
12
+ @options = options
13
+
14
+ @options[:file] ||= 'config/schedule.rb'
15
+ @options[:identifier] ||= default_identifier
16
+
17
+ unless File.exists?(@options[:file])
18
+ warn("[fail] Can't find file: #{@options[:file]}")
19
+ exit(1)
20
+ end
21
+
22
+ if @options[:update] && @options[:write]
23
+ warn("[fail] Can't update AND write. choose one.")
24
+ exit(1)
25
+ end
26
+ end
27
+
28
+ def run
29
+ if @options[:update]
30
+ write_crontab(updated_crontab)
31
+ elsif @options[:write]
32
+ write_crontab(whenever_cron)
33
+ else
34
+ puts Whenever.cron(@options)
35
+ exit(0)
36
+ end
37
+ end
38
+
39
+ protected
40
+
41
+ def default_identifier
42
+ File.expand_path(@options[:file])
43
+ end
44
+
45
+ def whenever_cron
46
+ @whenever_cron ||= [comment_open, Whenever.cron(@options), comment_close].join("\n") + "\n"
47
+ end
48
+
49
+ def read_crontab
50
+ return @current_crontab if @current_crontab
51
+
52
+ command = ['crontab -l']
53
+ command << "-u #{@options[:user]}" if @options[:user]
54
+
55
+ command_results = %x[#{command.join(' ')} 2> /dev/null]
56
+ @current_crontab = $?.exitstatus.zero? ? command_results : ''
57
+ end
58
+
59
+ def write_crontab(contents)
60
+ tmp_cron_file = Tempfile.new('whenever_tmp_cron').path
61
+ File.open(tmp_cron_file, File::WRONLY | File::APPEND) do |file|
62
+ file << contents
63
+ end
64
+
65
+ command = ['crontab']
66
+ command << "-u #{@options[:user]}" if @options[:user]
67
+ command << tmp_cron_file
68
+
69
+ if system(command.join(' '))
70
+ action = 'written' if @options[:write]
71
+ action = 'updated' if @options[:update]
72
+ puts "[write] crontab file #{action}"
73
+ exit(0)
74
+ else
75
+ warn "[fail] Couldn't write crontab; try running `whenever' with no options to ensure your schedule file is valid."
76
+ exit(1)
77
+ end
78
+ end
79
+
80
+ def updated_crontab
81
+ # Check for unopened or unclosed identifier blocks
82
+ if read_crontab.index(comment_open) && !read_crontab.index(comment_close)
83
+ warn "[fail] Unclosed indentifier; Your crontab file contains '#{comment_open}', but no '#{comment_close}'"
84
+ exit(1)
85
+ elsif !read_crontab.index(comment_open) && read_crontab.index(comment_close)
86
+ warn "[fail] Unopened indentifier; Your crontab file contains '#{comment_close}', but no '#{comment_open}'"
87
+ exit(1)
88
+ end
89
+
90
+ # If an existing identier block is found, replace it with the new cron entries
91
+ if read_crontab.index(comment_open) && read_crontab.index(comment_close)
92
+ read_crontab.gsub(Regexp.new("#{comment_open}.+#{comment_close}", Regexp::MULTILINE), whenever_cron.chomp)
93
+ else # Otherwise, append the new cron entries after any existing ones
94
+ [read_crontab, whenever_cron].join("\n\n")
95
+ end
96
+ end
97
+
98
+ def comment_base
99
+ "Whenever generated tasks for: #{@options[:identifier]}"
100
+ end
101
+
102
+ def comment_open
103
+ "# Begin #{comment_base}"
104
+ end
105
+
106
+ def comment_close
107
+ "# End #{comment_base}"
108
+ end
109
+
110
+ end
111
+ end