merb_whenever 0.4.1
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 +3 -0
- data/CHANGELOG.rdoc +82 -0
- data/README.rdoc +115 -0
- data/Rakefile +32 -0
- data/bin/whenever +30 -0
- data/bin/wheneverize +69 -0
- data/lib/merb_whenever.rb +15 -0
- data/lib/whenever/base.rb +15 -0
- data/lib/whenever/command_line.rb +108 -0
- data/lib/whenever/job_list.rb +161 -0
- data/lib/whenever/job_types/default.rb +29 -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 +54 -0
- data/lib/whenever/outputs/cron.rb +139 -0
- data/lib/whenever/version.rb +3 -0
- data/merb_whenever.gemspec +77 -0
- data/test/command_line_test.rb +101 -0
- data/test/cron_test.rb +226 -0
- data/test/output_at_test.rb +178 -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 +289 -0
- data/test/output_runner_test.rb +209 -0
- data/test/test_helper.rb +33 -0
- metadata +99 -0
@@ -0,0 +1,54 @@
|
|
1
|
+
module Whenever
|
2
|
+
module Output
|
3
|
+
class Cron
|
4
|
+
class OutputRedirection
|
5
|
+
|
6
|
+
def initialize(output)
|
7
|
+
@output = output
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_s
|
11
|
+
return '' unless defined?(@output)
|
12
|
+
case @output
|
13
|
+
when String then redirect_from_string
|
14
|
+
when Hash then redirect_from_hash
|
15
|
+
when NilClass then ">> /dev/null 2>&1"
|
16
|
+
else ''
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
protected
|
21
|
+
|
22
|
+
def stdout
|
23
|
+
return unless @output.has_key?(:standard)
|
24
|
+
@output[:standard].nil? ? '/dev/null' : @output[:standard]
|
25
|
+
end
|
26
|
+
|
27
|
+
def stderr
|
28
|
+
return unless @output.has_key?(:error)
|
29
|
+
@output[:error].nil? ? '/dev/null' : @output[:error]
|
30
|
+
end
|
31
|
+
|
32
|
+
def redirect_from_hash
|
33
|
+
case
|
34
|
+
when stdout == '/dev/null' && stderr == '/dev/null'
|
35
|
+
">> /dev/null 2>&1"
|
36
|
+
when stdout && stderr
|
37
|
+
">> #{stdout} 2> #{stderr}"
|
38
|
+
when stderr
|
39
|
+
"2> #{stderr}"
|
40
|
+
when stdout
|
41
|
+
">> #{stdout}"
|
42
|
+
else
|
43
|
+
''
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def redirect_from_string
|
48
|
+
">> #{@output} 2>&1"
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,139 @@
|
|
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 > 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
|
+
when 1.hour...1.day
|
82
|
+
hour_frequency = (@time / 60 / 60).round
|
83
|
+
timing[0] = @at.is_a?(Time) ? @at.min : @at
|
84
|
+
timing[1] = comma_separated_timing(hour_frequency, 23)
|
85
|
+
when 1.day...1.month
|
86
|
+
day_frequency = (@time / 24 / 60 / 60).round
|
87
|
+
timing[0] = @at.is_a?(Time) ? @at.min : 0
|
88
|
+
timing[1] = @at.is_a?(Time) ? @at.hour : @at
|
89
|
+
timing[2] = comma_separated_timing(day_frequency, 31, 1)
|
90
|
+
when 1.month..12.months
|
91
|
+
month_frequency = (@time / 30 / 24 / 60 / 60).round
|
92
|
+
timing[0] = @at.is_a?(Time) ? @at.min : 0
|
93
|
+
timing[1] = @at.is_a?(Time) ? @at.hour : 0
|
94
|
+
timing[2] = @at.is_a?(Time) ? @at.day : (@at.zero? ? 1 : @at)
|
95
|
+
timing[3] = comma_separated_timing(month_frequency, 12, 1)
|
96
|
+
else
|
97
|
+
return parse_as_string
|
98
|
+
end
|
99
|
+
timing.join(' ')
|
100
|
+
end
|
101
|
+
|
102
|
+
def parse_as_string
|
103
|
+
return unless @time
|
104
|
+
string = @time.to_s
|
105
|
+
|
106
|
+
timing = Array.new(4, '*')
|
107
|
+
timing[0] = @at.is_a?(Time) ? @at.min : 0
|
108
|
+
timing[1] = @at.is_a?(Time) ? @at.hour : 0
|
109
|
+
|
110
|
+
return (timing << '1-5') * " " if string.downcase.index('weekday')
|
111
|
+
return (timing << '6,0') * " " if string.downcase.index('weekend')
|
112
|
+
|
113
|
+
%w(sun mon tue wed thu fri sat).each_with_index do |day, i|
|
114
|
+
return (timing << i) * " " if string.downcase.index(day)
|
115
|
+
end
|
116
|
+
|
117
|
+
raise ArgumentError, "Couldn't parse: #{@time}"
|
118
|
+
end
|
119
|
+
|
120
|
+
def comma_separated_timing(frequency, max, start = 0)
|
121
|
+
return start if frequency.blank? || frequency.zero?
|
122
|
+
return '*' if frequency == 1
|
123
|
+
return frequency if frequency > (max * 0.5).ceil
|
124
|
+
|
125
|
+
original_start = start
|
126
|
+
|
127
|
+
start += frequency unless (max + 1).modulo(frequency).zero? || start > 0
|
128
|
+
output = (start..max).step(frequency).to_a
|
129
|
+
|
130
|
+
max_occurances = (max.to_f / (frequency.to_f)).round
|
131
|
+
max_occurances += 1 if original_start.zero?
|
132
|
+
|
133
|
+
output[0, max_occurances].join(',')
|
134
|
+
end
|
135
|
+
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
139
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{merb_whenever}
|
8
|
+
s.version = "0.4.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Javan Makhmali", "Roberto Thais"]
|
12
|
+
s.date = %q{2009-11-30}
|
13
|
+
s.description = %q{Clean ruby syntax for defining and deploying messy cron jobs. Modified for use with merb}
|
14
|
+
s.email = %q{javan@javan.us}
|
15
|
+
s.executables = ["whenever", "wheneverize"]
|
16
|
+
s.extra_rdoc_files = [
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".gitignore",
|
21
|
+
"CHANGELOG.rdoc",
|
22
|
+
"README.rdoc",
|
23
|
+
"Rakefile",
|
24
|
+
"bin/whenever",
|
25
|
+
"bin/wheneverize",
|
26
|
+
"lib/merb_whenever.rb",
|
27
|
+
"lib/whenever/base.rb",
|
28
|
+
"lib/whenever/command_line.rb",
|
29
|
+
"lib/whenever/job_list.rb",
|
30
|
+
"lib/whenever/job_types/default.rb",
|
31
|
+
"lib/whenever/job_types/rake_task.rb",
|
32
|
+
"lib/whenever/job_types/runner.rb",
|
33
|
+
"lib/whenever/outputs/cron.rb",
|
34
|
+
"lib/whenever/outputs/cron/output_redirection.rb",
|
35
|
+
"lib/whenever/version.rb",
|
36
|
+
"test/command_line_test.rb",
|
37
|
+
"test/cron_test.rb",
|
38
|
+
"test/output_at_test.rb",
|
39
|
+
"test/output_command_test.rb",
|
40
|
+
"test/output_env_test.rb",
|
41
|
+
"test/output_rake_test.rb",
|
42
|
+
"test/output_redirection_test.rb",
|
43
|
+
"test/output_runner_test.rb",
|
44
|
+
"test/test_helper.rb",
|
45
|
+
"merb_whenever.gemspec"
|
46
|
+
]
|
47
|
+
s.homepage = %q{http://github.com/javan/whenever}
|
48
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
49
|
+
s.require_paths = ["lib"]
|
50
|
+
s.rubygems_version = %q{1.3.5}
|
51
|
+
s.summary = %q{Clean ruby syntax for defining and deploying messy cron jobs.}
|
52
|
+
s.test_files = [
|
53
|
+
"test/command_line_test.rb",
|
54
|
+
"test/cron_test.rb",
|
55
|
+
"test/output_at_test.rb",
|
56
|
+
"test/output_command_test.rb",
|
57
|
+
"test/output_env_test.rb",
|
58
|
+
"test/output_rake_test.rb",
|
59
|
+
"test/output_redirection_test.rb",
|
60
|
+
"test/output_runner_test.rb",
|
61
|
+
"test/test_helper.rb"
|
62
|
+
]
|
63
|
+
|
64
|
+
if s.respond_to? :specification_version then
|
65
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
66
|
+
s.specification_version = 3
|
67
|
+
|
68
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
69
|
+
s.add_runtime_dependency(%q<chronic>, [">= 0.2.3"])
|
70
|
+
else
|
71
|
+
s.add_dependency(%q<chronic>, [">= 0.2.3"])
|
72
|
+
end
|
73
|
+
else
|
74
|
+
s.add_dependency(%q<chronic>, [">= 0.2.3"])
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/test_helper")
|
2
|
+
|
3
|
+
class CommandLineTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "A command line write" do
|
6
|
+
setup do
|
7
|
+
File.expects(:exists?).with('config/schedule.rb').returns(true)
|
8
|
+
@command = Whenever::CommandLine.new(:write => true, :identifier => 'My identifier')
|
9
|
+
@task = "#{two_hours} /my/command"
|
10
|
+
Whenever.expects(:cron).returns(@task)
|
11
|
+
end
|
12
|
+
|
13
|
+
should "output the cron job with identifier blocks" do
|
14
|
+
output = <<-EXPECTED
|
15
|
+
# Begin Whenever generated tasks for: My identifier
|
16
|
+
#{@task}
|
17
|
+
# End Whenever generated tasks for: My identifier
|
18
|
+
EXPECTED
|
19
|
+
|
20
|
+
assert_equal output, @command.send(:whenever_cron)
|
21
|
+
end
|
22
|
+
|
23
|
+
should "write the crontab when run" do
|
24
|
+
@command.expects(:write_crontab).returns(true)
|
25
|
+
assert @command.run
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context "A command line update" do
|
30
|
+
setup do
|
31
|
+
File.expects(:exists?).with('config/schedule.rb').returns(true)
|
32
|
+
@command = Whenever::CommandLine.new(:update => true, :identifier => 'My identifier')
|
33
|
+
@task = "#{two_hours} /my/command"
|
34
|
+
Whenever.expects(:cron).returns(@task)
|
35
|
+
end
|
36
|
+
|
37
|
+
should "add the cron to the end of the file if there is no existing identifier block" do
|
38
|
+
existing = '# Existing crontab'
|
39
|
+
@command.expects(:read_crontab).at_least_once.returns(existing)
|
40
|
+
|
41
|
+
new_cron = <<-EXPECTED
|
42
|
+
#{existing}
|
43
|
+
|
44
|
+
# Begin Whenever generated tasks for: My identifier
|
45
|
+
#{@task}
|
46
|
+
# End Whenever generated tasks for: My identifier
|
47
|
+
EXPECTED
|
48
|
+
|
49
|
+
assert_equal new_cron, @command.send(:updated_crontab)
|
50
|
+
|
51
|
+
@command.expects(:write_crontab).with(new_cron).returns(true)
|
52
|
+
assert @command.run
|
53
|
+
end
|
54
|
+
|
55
|
+
should "replace an existing block if the identifier matches" do
|
56
|
+
existing = <<-EXISTING_CRON
|
57
|
+
# Something
|
58
|
+
|
59
|
+
# Begin Whenever generated tasks for: My identifier
|
60
|
+
My whenever job that was already here
|
61
|
+
# End Whenever generated tasks for: My identifier
|
62
|
+
|
63
|
+
# Begin Whenever generated tasks for: Other identifier
|
64
|
+
This shouldn't get replaced
|
65
|
+
# End Whenever generated tasks for: Other identifier
|
66
|
+
EXISTING_CRON
|
67
|
+
|
68
|
+
@command.expects(:read_crontab).at_least_once.returns(existing)
|
69
|
+
|
70
|
+
new_cron = <<-NEW_CRON
|
71
|
+
# Something
|
72
|
+
|
73
|
+
# Begin Whenever generated tasks for: My identifier
|
74
|
+
#{@task}
|
75
|
+
# End Whenever generated tasks for: My identifier
|
76
|
+
|
77
|
+
# Begin Whenever generated tasks for: Other identifier
|
78
|
+
This shouldn't get replaced
|
79
|
+
# End Whenever generated tasks for: Other identifier
|
80
|
+
NEW_CRON
|
81
|
+
|
82
|
+
assert_equal new_cron, @command.send(:updated_crontab)
|
83
|
+
|
84
|
+
@command.expects(:write_crontab).with(new_cron).returns(true)
|
85
|
+
assert @command.run
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context "A command line update with no identifier" do
|
90
|
+
setup do
|
91
|
+
File.expects(:exists?).with('config/schedule.rb').returns(true)
|
92
|
+
Whenever::CommandLine.any_instance.expects(:default_identifier).returns('DEFAULT')
|
93
|
+
@command = Whenever::CommandLine.new(:update => true, :file => @file)
|
94
|
+
end
|
95
|
+
|
96
|
+
should "use the default identifier" do
|
97
|
+
assert_equal "Whenever generated tasks for: DEFAULT", @command.send(:comment_base)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
data/test/cron_test.rb
ADDED
@@ -0,0 +1,226 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/test_helper")
|
2
|
+
|
3
|
+
class CronTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "When parsing time in minutes" do
|
6
|
+
should "raise if less than 1 minute" do
|
7
|
+
assert_raises ArgumentError do
|
8
|
+
parse_time(59.seconds)
|
9
|
+
end
|
10
|
+
|
11
|
+
assert_raises ArgumentError do
|
12
|
+
parse_time(0.minutes)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# For santity, do some tests on straight String
|
17
|
+
should "parse correctly" do
|
18
|
+
assert_equal '* * * * *', parse_time(1.minute)
|
19
|
+
assert_equal '0,5,10,15,20,25,30,35,40,45,50,55 * * * *', parse_time(5.minutes)
|
20
|
+
assert_equal '7,14,21,28,35,42,49,56 * * * *', parse_time(7.minutes)
|
21
|
+
assert_equal '0,30 * * * *', parse_time(30.minutes)
|
22
|
+
assert_equal '32 * * * *', parse_time(32.minutes)
|
23
|
+
assert_not_equal '60 * * * *', parse_time(60.minutes) # 60 minutes bumps up into the hour range
|
24
|
+
end
|
25
|
+
|
26
|
+
# Test all minutes
|
27
|
+
(2..59).each do |num|
|
28
|
+
should "parse correctly for #{num} minutes" do
|
29
|
+
start = 0
|
30
|
+
start += num unless 60.modulo(num).zero?
|
31
|
+
minutes = (start..59).step(num).to_a
|
32
|
+
|
33
|
+
assert_equal "#{minutes.join(',')} * * * *", parse_time(num.minutes)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context "When parsing time in hours" do
|
39
|
+
should "parse correctly" do
|
40
|
+
assert_equal '0 * * * *', parse_time(1.hour)
|
41
|
+
assert_equal '0 0,2,4,6,8,10,12,14,16,18,20,22 * * *', parse_time(2.hours)
|
42
|
+
assert_equal '0 0,3,6,9,12,15,18,21 * * *', parse_time(3.hours)
|
43
|
+
assert_equal '0 5,10,15,20 * * *', parse_time(5.hours)
|
44
|
+
assert_equal '0 17 * * *', parse_time(17.hours)
|
45
|
+
assert_not_equal '0 24 * * *', parse_time(24.hours) # 24 hours bumps up into the day range
|
46
|
+
end
|
47
|
+
|
48
|
+
(2..23).each do |num|
|
49
|
+
should "parse correctly for #{num} hours" do
|
50
|
+
start = 0
|
51
|
+
start += num unless 24.modulo(num).zero?
|
52
|
+
hours = (start..23).step(num).to_a
|
53
|
+
|
54
|
+
assert_equal "0 #{hours.join(',')} * * *", parse_time(num.hours)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
should "parse correctly when given an 'at' with minutes as an Integer" do
|
59
|
+
assert_minutes_equals "1", 1
|
60
|
+
assert_minutes_equals "14", 14
|
61
|
+
assert_minutes_equals "27", 27
|
62
|
+
assert_minutes_equals "55", 55
|
63
|
+
end
|
64
|
+
|
65
|
+
should "parse correctly when given an 'at' with minutes as a Time" do
|
66
|
+
# Basically just testing that Chronic parses some times and we get the minutes out of it
|
67
|
+
assert_minutes_equals "1", '3:01am'
|
68
|
+
assert_minutes_equals "1", 'January 21 2:01 PM'
|
69
|
+
assert_minutes_equals "0", 'midnight'
|
70
|
+
assert_minutes_equals "59", '13:59'
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context "When parsing time in days (of month)" do
|
75
|
+
should "parse correctly" do
|
76
|
+
assert_equal '0 0 * * *', parse_time(1.days)
|
77
|
+
assert_equal '0 0 1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31 * *', parse_time(2.days)
|
78
|
+
assert_equal '0 0 1,5,9,13,17,21,25,29 * *', parse_time(4.days)
|
79
|
+
assert_equal '0 0 1,8,15,22 * *', parse_time(7.days)
|
80
|
+
assert_equal '0 0 1,17 * *', parse_time(16.days)
|
81
|
+
assert_equal '0 0 17 * *', parse_time(17.days)
|
82
|
+
assert_equal '0 0 29 * *', parse_time(29.days)
|
83
|
+
assert_not_equal '0 0 30 * *', parse_time(30.days) # 30 days bumps into the month range
|
84
|
+
end
|
85
|
+
|
86
|
+
should "parse correctly when given an 'at' with hours, minutes as a Time" do
|
87
|
+
# first param is an array with [hours, minutes]
|
88
|
+
assert_hours_and_minutes_equals %w(3 45), '3:45am'
|
89
|
+
assert_hours_and_minutes_equals %w(20 1), '8:01pm'
|
90
|
+
assert_hours_and_minutes_equals %w(0 0), 'midnight'
|
91
|
+
assert_hours_and_minutes_equals %w(1 23), '1:23 AM'
|
92
|
+
assert_hours_and_minutes_equals %w(23 59), 'March 21 11:59 pM'
|
93
|
+
end
|
94
|
+
|
95
|
+
should "parse correctly when given an 'at' with hours as an Integer" do
|
96
|
+
# first param is an array with [hours, minutes]
|
97
|
+
assert_hours_and_minutes_equals %w(1 0), 1
|
98
|
+
assert_hours_and_minutes_equals %w(3 0), 3
|
99
|
+
assert_hours_and_minutes_equals %w(15 0), 15
|
100
|
+
assert_hours_and_minutes_equals %w(19 0), 19
|
101
|
+
assert_hours_and_minutes_equals %w(23 0), 23
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
context "When parsing time in months" do
|
106
|
+
should "parse correctly" do
|
107
|
+
assert_equal '0 0 1 * *', parse_time(1.month)
|
108
|
+
assert_equal '0 0 1 1,3,5,7,9,11 *', parse_time(2.months)
|
109
|
+
assert_equal '0 0 1 1,4,7,10 *', parse_time(3.months)
|
110
|
+
assert_equal '0 0 1 1,5,9 *', parse_time(4.months)
|
111
|
+
assert_equal '0 0 1 1,6 *', parse_time(5.months)
|
112
|
+
assert_equal '0 0 1 7 *', parse_time(7.months)
|
113
|
+
assert_equal '0 0 1 8 *', parse_time(8.months)
|
114
|
+
assert_equal '0 0 1 9 *', parse_time(9.months)
|
115
|
+
assert_equal '0 0 1 10 *', parse_time(10.months)
|
116
|
+
assert_equal '0 0 1 11 *', parse_time(11.months)
|
117
|
+
assert_equal '0 0 1 12 *', parse_time(12.months)
|
118
|
+
end
|
119
|
+
|
120
|
+
should "parse correctly when given an 'at' with days, hours, minutes as a Time" do
|
121
|
+
# first param is an array with [days, hours, minutes]
|
122
|
+
assert_days_and_hours_and_minutes_equals %w(1 3 45), 'January 1st 3:45am'
|
123
|
+
assert_days_and_hours_and_minutes_equals %w(11 23 0), 'Feb 11 11PM'
|
124
|
+
assert_days_and_hours_and_minutes_equals %w(22 1 1), 'march 22nd at 1:01 am'
|
125
|
+
assert_days_and_hours_and_minutes_equals %w(23 0 0), 'march 22nd at midnight' # looks like midnight means the next day
|
126
|
+
end
|
127
|
+
|
128
|
+
should "parse correctly when given an 'at' with days as an Integer" do
|
129
|
+
# first param is an array with [days, hours, minutes]
|
130
|
+
assert_days_and_hours_and_minutes_equals %w(1 0 0), 1
|
131
|
+
assert_days_and_hours_and_minutes_equals %w(15 0 0), 15
|
132
|
+
assert_days_and_hours_and_minutes_equals %w(29 0 0), 29
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
context "When parsing time in days (of week)" do
|
137
|
+
should "parse days of the week correctly" do
|
138
|
+
{
|
139
|
+
'0' => %w(sun Sunday SUNDAY SUN),
|
140
|
+
'1' => %w(mon Monday MONDAY MON),
|
141
|
+
'2' => %w(tue tues Tuesday TUESDAY TUE),
|
142
|
+
'3' => %w(wed Wednesday WEDNESDAY WED),
|
143
|
+
'4' => %w(thu thurs thur Thursday THURSDAY THU),
|
144
|
+
'5' => %w(fri Friday FRIDAY FRI),
|
145
|
+
'6' => %w(sat Saturday SATURDAY SAT)
|
146
|
+
}.each do |day, day_tests|
|
147
|
+
day_tests.each do |day_test|
|
148
|
+
assert_equal "0 0 * * #{day}", parse_time(day_test)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
should "allow additional directives" do
|
154
|
+
assert_equal '30 13 * * 5', parse_time('friday', nil, "1:30 pm")
|
155
|
+
assert_equal '22 2 * * 1', parse_time('Monday', nil, "2:22am")
|
156
|
+
assert_equal '55 17 * * 4', parse_time('THU', nil, "5:55PM")
|
157
|
+
end
|
158
|
+
|
159
|
+
should "parse weekday correctly" do
|
160
|
+
assert_equal '0 0 * * 1-5', parse_time('weekday')
|
161
|
+
assert_equal '0 0 * * 1-5', parse_time('Weekdays')
|
162
|
+
assert_equal '0 1 * * 1-5', parse_time('Weekdays', nil, "1:00 am")
|
163
|
+
assert_equal '59 5 * * 1-5', parse_time('Weekdays', nil, "5:59 am")
|
164
|
+
end
|
165
|
+
|
166
|
+
should "parse weekend correctly" do
|
167
|
+
assert_equal '0 0 * * 6,0', parse_time('weekend')
|
168
|
+
assert_equal '0 0 * * 6,0', parse_time('Weekends')
|
169
|
+
assert_equal '0 7 * * 6,0', parse_time('Weekends', nil, "7am")
|
170
|
+
assert_equal '2 18 * * 6,0', parse_time('Weekends', nil, "6:02PM")
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
context "When parsing time using the cron shortcuts" do
|
175
|
+
should "parse a :symbol into the correct shortcut" do
|
176
|
+
assert_equal '@reboot', parse_time(:reboot)
|
177
|
+
assert_equal '@annually', parse_time(:year)
|
178
|
+
assert_equal '@annually', parse_time(:yearly)
|
179
|
+
assert_equal '@daily', parse_time(:day)
|
180
|
+
assert_equal '@daily', parse_time(:daily)
|
181
|
+
assert_equal '@midnight', parse_time(:midnight)
|
182
|
+
assert_equal '@monthly', parse_time(:month)
|
183
|
+
assert_equal '@monthly', parse_time(:monthly)
|
184
|
+
assert_equal '@hourly', parse_time(:hour)
|
185
|
+
assert_equal '@hourly', parse_time(:hourly)
|
186
|
+
end
|
187
|
+
|
188
|
+
should "raise an exception if a valid shortcut is given but also an :at" do
|
189
|
+
assert_raises ArgumentError do
|
190
|
+
parse_time(:hour, nil, "1:00 am")
|
191
|
+
end
|
192
|
+
|
193
|
+
assert_raises ArgumentError do
|
194
|
+
parse_time(:reboot, nil, 5)
|
195
|
+
end
|
196
|
+
|
197
|
+
assert_raises ArgumentError do
|
198
|
+
parse_time(:day, nil, '4:20pm')
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
private
|
204
|
+
|
205
|
+
def assert_days_and_hours_and_minutes_equals(expected, time)
|
206
|
+
cron = parse_time(2.months, 'some task', time)
|
207
|
+
minutes, hours, days, *garbage = cron.split(' ')
|
208
|
+
assert_equal expected, [days, hours, minutes]
|
209
|
+
end
|
210
|
+
|
211
|
+
def assert_hours_and_minutes_equals(expected, time)
|
212
|
+
cron = parse_time(2.days, 'some task', time)
|
213
|
+
minutes, hours, *garbage = cron.split(' ')
|
214
|
+
assert_equal expected, [hours, minutes]
|
215
|
+
end
|
216
|
+
|
217
|
+
def assert_minutes_equals(expected, time)
|
218
|
+
cron = parse_time(2.hours, 'some task', time)
|
219
|
+
assert_equal expected, cron.split(' ')[0]
|
220
|
+
end
|
221
|
+
|
222
|
+
def parse_time(time = nil, task = nil, at = nil)
|
223
|
+
Whenever::Output::Cron.new(time, task, at).time_in_cron_syntax
|
224
|
+
end
|
225
|
+
|
226
|
+
end
|