andrew-whenever 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -0,0 +1,161 @@
1
+ module Whenever
2
+ class JobList
3
+
4
+ def initialize(options)
5
+ @jobs = Hash.new
6
+ @env = Hash.new
7
+
8
+ case options
9
+ when String
10
+ config = options
11
+ when Hash
12
+ config = if options[:string]
13
+ options[:string]
14
+ elsif options[:file]
15
+ File.read(options[:file])
16
+ end
17
+ pre_set(options[:set])
18
+ end
19
+
20
+ eval(config)
21
+ end
22
+
23
+ def set(variable, value)
24
+ return if instance_variable_defined?("@#{variable}".to_sym)
25
+
26
+ instance_variable_set("@#{variable}".to_sym, value)
27
+ self.class.send(:attr_reader, variable.to_sym)
28
+ end
29
+
30
+ def env(variable, value)
31
+ @env[variable.to_s] = value
32
+ end
33
+
34
+ def every(frequency, options = {})
35
+ @current_time_scope = frequency
36
+ @options = options
37
+ yield
38
+ end
39
+
40
+ def command(task, options = {})
41
+ # :cron_log was an old option for output redirection, it remains for backwards compatibility
42
+ options[:output] = (options[:cron_log] || @cron_log) if defined?(@cron_log) || options.has_key?(:cron_log)
43
+ # :output is the newer, more flexible option.
44
+ options[:output] = @output if defined?(@output) && !options.has_key?(:output)
45
+ options[:class] ||= Whenever::Job::Default
46
+ @jobs[@current_time_scope] ||= []
47
+ @jobs[@current_time_scope] << options[:class].new(@options.merge(:task => task).merge(options))
48
+ end
49
+
50
+ def runner(task, options = {})
51
+ options.reverse_merge!(:environment => @environment, :path => @path)
52
+ options[:class] = Whenever::Job::Runner
53
+ command(task, options)
54
+ end
55
+
56
+ def rake(task, options = {})
57
+ options.reverse_merge!(:environment => @environment, :path => @path)
58
+ options[:class] = Whenever::Job::RakeTask
59
+ command(task, options)
60
+ end
61
+
62
+ def generate_cron_output
63
+ set_path_environment_variable
64
+
65
+ [environment_variables, cron_jobs].compact.join
66
+ end
67
+
68
+ private
69
+
70
+ #
71
+ # Takes a string like: "variable1=something&variable2=somethingelse"
72
+ # and breaks it into variable/value pairs. Used for setting variables at runtime from the command line.
73
+ # Only works for setting values as strings.
74
+ #
75
+ def pre_set(variable_string = nil)
76
+ return if variable_string.blank?
77
+
78
+ pairs = variable_string.split('&')
79
+ pairs.each do |pair|
80
+ next unless pair.index('=')
81
+ variable, value = *pair.split('=')
82
+ set(variable.strip, value.strip) unless variable.blank? || value.blank?
83
+ end
84
+ end
85
+
86
+ def set_path_environment_variable
87
+ return if path_should_not_be_set_automatically?
88
+ @env[:PATH] = read_path unless read_path.blank?
89
+ end
90
+
91
+ def read_path
92
+ ENV['PATH'] if ENV
93
+ end
94
+
95
+ def path_should_not_be_set_automatically?
96
+ @set_path_automatically === false || @env[:PATH] || @env["PATH"]
97
+ end
98
+
99
+ def environment_variables
100
+ return if @env.empty?
101
+
102
+ output = []
103
+ @env.each do |key, val|
104
+ output << "#{key}=#{val}\n"
105
+ end
106
+ output << "\n"
107
+
108
+ output.join
109
+ end
110
+
111
+ #
112
+ # Takes the standard cron output that Whenever generates and finds
113
+ # similar entries that can be combined. For example: If a job should run
114
+ # at 3:02am and 4:02am, instead of creating two jobs this method combines
115
+ # them into one that runs on the 2nd minute at the 3rd and 4th hour.
116
+ #
117
+ def combine(entries)
118
+ entries.map! { |entry| entry.split(/ +/, 6) }
119
+ 0.upto(4) do |f|
120
+ (entries.length-1).downto(1) do |i|
121
+ next if entries[i][f] == '*'
122
+ comparison = entries[i][0...f] + entries[i][f+1..-1]
123
+ (i-1).downto(0) do |j|
124
+ next if entries[j][f] == '*'
125
+ if comparison == entries[j][0...f] + entries[j][f+1..-1]
126
+ entries[j][f] += ',' + entries[i][f]
127
+ entries.delete_at(i)
128
+ break
129
+ end
130
+ end
131
+ end
132
+ end
133
+
134
+ entries.map { |entry| entry.join(' ') }
135
+ end
136
+
137
+ def cron_jobs
138
+ return if @jobs.empty?
139
+
140
+ shortcut_jobs = []
141
+ regular_jobs = []
142
+
143
+ @jobs.each do |time, jobs|
144
+ jobs.each do |job|
145
+ Whenever::Output::Cron.output(time, job) do |cron|
146
+ cron << "\n\n"
147
+
148
+ if cron.starts_with?("@")
149
+ shortcut_jobs << cron
150
+ else
151
+ regular_jobs << cron
152
+ end
153
+ end
154
+ end
155
+ end
156
+
157
+ shortcut_jobs.join + combine(regular_jobs).join
158
+ end
159
+
160
+ end
161
+ 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
@@ -0,0 +1,12 @@
1
+ module Whenever
2
+ module Job
3
+ class RakeTask < Whenever::Job::Default
4
+
5
+ def output
6
+ path_required
7
+ "cd #{@path} && RAILS_ENV=#{@environment} /usr/bin/env rake #{task}"
8
+ end
9
+
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module Whenever
2
+ module Job
3
+ class Runner < Whenever::Job::Default
4
+
5
+ def output
6
+ path_required
7
+ %Q(cd #{File.join(@path)} && script/runner -e #{@environment} #{task.inspect})
8
+ end
9
+
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,140 @@
1
+ module Whenever
2
+ module Output
3
+
4
+ class Cron
5
+
6
+ attr_accessor :time, :task
7
+
8
+ def initialize(time = nil, task = nil, at = nil, output_redirection = nil)
9
+ @time = time
10
+ @task = task
11
+ @at = at.is_a?(String) ? (Chronic.parse(at) || 0) : (at || 0)
12
+ @output_redirection = output_redirection
13
+ end
14
+
15
+ def self.enumerate(item)
16
+ if item and item.is_a?(String)
17
+ items = item.split(',')
18
+ else
19
+ items = item
20
+ items = [items] unless items and items.respond_to?(:each)
21
+ end
22
+ items
23
+ end
24
+
25
+ def self.output(times, job)
26
+ enumerate(times).each do |time|
27
+ enumerate(job.at).each do |at|
28
+ yield new(time, job.output, at, job.output_redirection).output
29
+ end
30
+ end
31
+ end
32
+
33
+ def output
34
+ [time_in_cron_syntax, task, output_redirection].compact.join(' ').strip
35
+ end
36
+
37
+ def time_in_cron_syntax
38
+ case @time
39
+ when Symbol then parse_symbol
40
+ when String then parse_as_string
41
+ else parse_time
42
+ end
43
+ end
44
+
45
+ def output_redirection
46
+ OutputRedirection.new(@output_redirection).to_s unless @output_redirection == :not_set
47
+ end
48
+
49
+ protected
50
+
51
+ def parse_symbol
52
+ shortcut = case @time
53
+ when :reboot then '@reboot'
54
+ when :year, :yearly then '@annually'
55
+ when :day, :daily then '@daily'
56
+ when :midnight then '@midnight'
57
+ when :month, :monthly then '@monthly'
58
+ when :week, :weekly then '@weekly'
59
+ when :hour, :hourly then '@hourly'
60
+ end
61
+
62
+ if shortcut
63
+ if @at.is_a?(Time) || (@at.is_a?(Numeric) && @at>0)
64
+ raise ArgumentError, "You cannot specify an ':at' when using the shortcuts for times."
65
+ else
66
+ return shortcut
67
+ end
68
+ else
69
+ parse_as_string
70
+ end
71
+ end
72
+
73
+ def parse_time
74
+ timing = Array.new(5, '*')
75
+ case @time
76
+ when 0.seconds...1.minute
77
+ raise ArgumentError, "Time must be in minutes or higher"
78
+ when 1.minute...1.hour
79
+ minute_frequency = @time / 60
80
+ timing[0] = comma_separated_timing(minute_frequency, 59)
81
+ timing[1] = @at.is_a?(Time) ? @at.min : @at if @at > 0
82
+ when 1.hour...1.day
83
+ hour_frequency = (@time / 60 / 60).round
84
+ timing[0] = @at.is_a?(Time) ? @at.min : @at
85
+ timing[1] = comma_separated_timing(hour_frequency, 23)
86
+ when 1.day...1.month
87
+ day_frequency = (@time / 24 / 60 / 60).round
88
+ timing[0] = @at.is_a?(Time) ? @at.min : 0
89
+ timing[1] = @at.is_a?(Time) ? @at.hour : @at
90
+ timing[2] = comma_separated_timing(day_frequency, 31, 1)
91
+ when 1.month..12.months
92
+ month_frequency = (@time / 30 / 24 / 60 / 60).round
93
+ timing[0] = @at.is_a?(Time) ? @at.min : 0
94
+ timing[1] = @at.is_a?(Time) ? @at.hour : 0
95
+ timing[2] = @at.is_a?(Time) ? @at.day : (@at.zero? ? 1 : @at)
96
+ timing[3] = comma_separated_timing(month_frequency, 12, 1)
97
+ else
98
+ return parse_as_string
99
+ end
100
+ timing.join(' ')
101
+ end
102
+
103
+ def parse_as_string
104
+ return unless @time
105
+ string = @time.to_s
106
+
107
+ timing = Array.new(4, '*')
108
+ timing[0] = @at.is_a?(Time) ? @at.min : 0
109
+ timing[1] = @at.is_a?(Time) ? @at.hour : 0
110
+
111
+ return (timing << '1-5') * " " if string.downcase.index('weekday')
112
+ return (timing << '6,0') * " " if string.downcase.index('weekend')
113
+
114
+ %w(sun mon tue wed thu fri sat).each_with_index do |day, i|
115
+ return (timing << i) * " " if string.downcase.index(day)
116
+ end
117
+
118
+ raise ArgumentError, "Couldn't parse: #{@time}"
119
+ end
120
+
121
+ def comma_separated_timing(frequency, max, start = 0)
122
+ return start if frequency.blank? || frequency.zero?
123
+ return '*' if frequency == 1
124
+ return frequency if frequency > (max * 0.5).ceil
125
+
126
+ original_start = start
127
+
128
+ start += frequency unless (max + 1).modulo(frequency).zero? || start > 0
129
+ output = (start..max).step(frequency).to_a
130
+
131
+ max_occurances = (max.to_f / (frequency.to_f)).round
132
+ max_occurances += 1 if original_start.zero?
133
+
134
+ output[0, max_occurances].join(',')
135
+ end
136
+
137
+ end
138
+
139
+ end
140
+ end