javan-whenever 0.3.1 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,9 @@
1
+ == 0.3.5 / June 13th, 2009
2
+
3
+ * Added ability to accept lists of every's and at's and intelligently group them. (ex: every 'monday, wednesday', :at => ['3pm, 6am']). [Sam Ruby]
4
+
5
+ * Fixed issue with new lines. #18 [Javan Makhmali]
6
+
1
7
  == 0.3.1 / June 25th, 2009
2
8
 
3
9
  * Removed activesupport gem dependency. #1 [Javan Makhmali]
data/Manifest CHANGED
@@ -15,9 +15,9 @@ Rakefile
15
15
  README.rdoc
16
16
  test/command_line_test.rb
17
17
  test/cron_test.rb
18
+ test/output_at_test.rb
18
19
  test/output_command_test.rb
19
20
  test/output_env_test.rb
20
21
  test/output_rake_test.rb
21
22
  test/output_runner_test.rb
22
23
  test/test_helper.rb
23
- whenever.gemspec
data/lib/command_line.rb CHANGED
@@ -40,7 +40,7 @@ module Whenever
40
40
  end
41
41
 
42
42
  def whenever_cron
43
- @whenever_cron ||= [comment_open, Whenever.cron(@options), comment_close].join("\n")
43
+ @whenever_cron ||= [comment_open, Whenever.cron(@options), comment_close].join("\n") + "\n"
44
44
  end
45
45
 
46
46
  def read_crontab
@@ -86,7 +86,7 @@ module Whenever
86
86
 
87
87
  # If an existing identier block is found, replace it with the new cron entries
88
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)
89
+ read_crontab.gsub(Regexp.new("#{comment_open}.+#{comment_close}", Regexp::MULTILINE), whenever_cron.chomp)
90
90
  else # Otherwise, append the new cron entries after any existing ones
91
91
  [read_crontab, whenever_cron].join("\n\n")
92
92
  end
data/lib/job_list.rb CHANGED
@@ -62,9 +62,11 @@ module Whenever
62
62
 
63
63
  private
64
64
 
65
+ #
65
66
  # Takes a string like: "variable1=something&variable2=somethingelse"
66
67
  # and breaks it into variable/value pairs. Used for setting variables at runtime from the command line.
67
68
  # Only works for setting values as strings.
69
+ #
68
70
  def pre_set(variable_string = nil)
69
71
  return if variable_string.blank?
70
72
 
@@ -88,21 +90,48 @@ module Whenever
88
90
  output.join
89
91
  end
90
92
 
93
+ #
94
+ # Takes the standard cron output that Whenever generates and finds
95
+ # similar entries that can be combined. For example: If a job should run
96
+ # at 3:02am and 4:02am, instead of creating two jobs this method combines
97
+ # them into one that runs on the 2nd minute at the 3rd and 4th hour.
98
+ #
99
+ def combine(entries)
100
+ entries.map! { |entry| entry.split(/ +/,6 )}
101
+ 0.upto(4) do |f|
102
+ (entries.length-1).downto(1) do |i|
103
+ next if entries[i][f] == '*'
104
+ comparison = entries[i][0...f] + entries[i][f+1..-1]
105
+ (i-1).downto(0) do |j|
106
+ next if entries[j][f] == '*'
107
+ if comparison == entries[j][0...f] + entries[j][f+1..-1]
108
+ entries[j][f] += ',' + entries[i][f]
109
+ entries.delete_at(i)
110
+ break
111
+ end
112
+ end
113
+ end
114
+ end
115
+
116
+ entries.map { |entry| entry.join(' ') }
117
+ end
118
+
91
119
  def cron_jobs
92
120
  return if @jobs.empty?
93
121
 
94
122
  output = []
95
123
  @jobs.each do |time, jobs|
96
124
  jobs.each do |job|
97
- cron = Whenever::Output::Cron.output(time, job)
98
- cron << " >> #{job.cron_log} 2>&1" if job.cron_log
99
- cron << "\n\n"
100
- output << cron
125
+ Whenever::Output::Cron.output(time, job) do |cron|
126
+ cron << " >> #{job.cron_log} 2>&1" if job.cron_log
127
+ cron << "\n\n"
128
+ output << cron
129
+ end
101
130
  end
102
131
  end
103
132
 
104
- output.join
133
+ combine(output).join
105
134
  end
106
135
 
107
136
  end
108
- end
137
+ end
@@ -4,7 +4,7 @@ module Whenever
4
4
 
5
5
  def output
6
6
  path_required
7
- %Q(#{File.join(@path, 'script', 'runner')} -e #{@environment} "#{task}")
7
+ %Q(#{File.join(@path, 'script', 'runner')} -e #{@environment} #{task.inspect})
8
8
  end
9
9
 
10
10
  end
data/lib/outputs/cron.rb CHANGED
@@ -11,9 +11,23 @@ module Whenever
11
11
  @at = at.is_a?(String) ? (Chronic.parse(at) || 0) : (at || 0)
12
12
  end
13
13
 
14
- def self.output(time, job)
15
- out = new(time, job.output, job.at)
16
- "#{out.time_in_cron_syntax} #{out.task}"
14
+ def self.enumerate(item)
15
+ if item and item.is_a?(String)
16
+ items = item.split(',')
17
+ else
18
+ items = item
19
+ items = [items] unless items and items.respond_to?(:each)
20
+ end
21
+ items
22
+ end
23
+
24
+ def self.output(times, job)
25
+ enumerate(times).each do |time|
26
+ enumerate(job.at).each do |at|
27
+ out = new(time, job.output, at)
28
+ yield "#{out.time_in_cron_syntax} #{out.task}"
29
+ end
30
+ end
17
31
  end
18
32
 
19
33
  def time_in_cron_syntax
@@ -114,4 +128,4 @@ module Whenever
114
128
  end
115
129
 
116
130
  end
117
- end
131
+ end
data/lib/version.rb CHANGED
@@ -2,7 +2,7 @@ module Whenever
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 3
5
- TINY = 1
5
+ TINY = 5
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -11,13 +11,13 @@ class CommandLineTest < Test::Unit::TestCase
11
11
  end
12
12
 
13
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
14
+ output = <<-EXPECTED
15
+ # Begin Whenever generated tasks for: My identifier
16
+ #{@task}
17
+ # End Whenever generated tasks for: My identifier
18
+ EXPECTED
19
19
 
20
- assert_equal unindent(output).chomp, @command.send(:whenever_cron).chomp
20
+ assert_equal output, @command.send(:whenever_cron)
21
21
  end
22
22
 
23
23
  should "write the crontab when run" do
@@ -38,49 +38,50 @@ class CommandLineTest < Test::Unit::TestCase
38
38
  existing = '# Existing crontab'
39
39
  @command.expects(:read_crontab).at_least_once.returns(existing)
40
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
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
48
 
49
- assert_equal unindent(new_cron).chomp, @command.send(:updated_crontab).chomp
49
+ assert_equal new_cron, @command.send(:updated_crontab)
50
50
 
51
- @command.expects(:write_crontab).with(unindent(new_cron)).returns(true)
51
+ @command.expects(:write_crontab).with(new_cron).returns(true)
52
52
  assert @command.run
53
53
  end
54
54
 
55
55
  should "replace an existing block if the identifier matches" do
56
- existing = <<-existing
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
67
- @command.expects(:read_crontab).at_least_once.returns(unindent(existing))
68
-
69
- new_cron = <<-new_cron
70
- # Something
71
-
72
- # Begin Whenever generated tasks for: My identifier
73
- #{@task}
74
- # End Whenever generated tasks for: My identifier
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)
75
69
 
76
- # Begin Whenever generated tasks for: Other identifier
77
- This shouldn't get replaced
78
- # End Whenever generated tasks for: Other identifier
79
- new_cron
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
80
81
 
81
- assert_equal unindent(new_cron).chomp, @command.send(:updated_crontab).chomp
82
+ assert_equal new_cron, @command.send(:updated_crontab)
82
83
 
83
- @command.expects(:write_crontab).with(unindent(new_cron)).returns(true)
84
+ @command.expects(:write_crontab).with(new_cron).returns(true)
84
85
  assert @command.run
85
86
  end
86
87
  end
@@ -97,11 +98,4 @@ class CommandLineTest < Test::Unit::TestCase
97
98
  end
98
99
  end
99
100
 
100
- private
101
-
102
- def unindent(string)
103
- indentation = string[/\A\s*/]
104
- string.strip.gsub(/^#{indentation}/, "")
105
- end
106
-
107
101
  end
@@ -0,0 +1,137 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/test_helper")
2
+
3
+ class OutputAtTest < Test::Unit::TestCase
4
+
5
+ context "weekday at a (single) given time" do
6
+ setup do
7
+ @output = Whenever.cron \
8
+ <<-file
9
+ every "weekday", :at => '5:02am' do
10
+ command "blahblah"
11
+ end
12
+ file
13
+ end
14
+
15
+ should "output the command using that time" do
16
+ assert_match '2 5 * * 1-5 blahblah', @output
17
+ end
18
+ end
19
+
20
+ context "weekday at a multiple diverse times, via an array" do
21
+ setup do
22
+ @output = Whenever.cron \
23
+ <<-file
24
+ every "weekday", :at => %w(5:02am 3:52pm) do
25
+ command "blahblah"
26
+ end
27
+ file
28
+ end
29
+
30
+ should "output the commands for both times given" do
31
+ assert_match '2 5 * * 1-5 blahblah', @output
32
+ assert_match '52 15 * * 1-5 blahblah', @output
33
+ end
34
+ end
35
+
36
+ context "weekday at a multiple diverse times, comma separated" do
37
+ setup do
38
+ @output = Whenever.cron \
39
+ <<-file
40
+ every "weekday", :at => '5:02am, 3:52pm' do
41
+ command "blahblah"
42
+ end
43
+ file
44
+ end
45
+
46
+ should "output the commands for both times given" do
47
+ assert_match '2 5 * * 1-5 blahblah', @output
48
+ assert_match '52 15 * * 1-5 blahblah', @output
49
+ end
50
+ end
51
+
52
+ context "weekday at a multiple aligned times" do
53
+ setup do
54
+ @output = Whenever.cron \
55
+ <<-file
56
+ every "weekday", :at => '5:02am, 3:02pm' do
57
+ command "blahblah"
58
+ end
59
+ file
60
+ end
61
+
62
+ should "output the command using one entry because the times are aligned" do
63
+ assert_match '2 5,15 * * 1-5 blahblah', @output
64
+ end
65
+ end
66
+
67
+ context "various days at a various aligned times" do
68
+ setup do
69
+ @output = Whenever.cron \
70
+ <<-file
71
+ every "mon,wed,fri", :at => '5:02am, 3:02pm' do
72
+ command "blahblah"
73
+ end
74
+ file
75
+ end
76
+
77
+ should "output the command using one entry because the times are aligned" do
78
+ assert_match '2 5,15 * * 1,3,5 blahblah', @output
79
+ end
80
+ end
81
+
82
+ context "various days at a various aligned times using a runner" do
83
+ setup do
84
+ @output = Whenever.cron \
85
+ <<-file
86
+ set :path, '/your/path'
87
+ every "mon,wed,fri", :at => '5:02am, 3:02pm' do
88
+ runner "blahblah"
89
+ end
90
+ file
91
+ end
92
+
93
+ should "output the runner using one entry because the times are aligned" do
94
+ assert_match '2 5,15 * * 1,3,5 /your/path/script/runner -e production "blahblah"', @output
95
+ end
96
+ end
97
+
98
+ context "various days at a various aligned times using a rake task" do
99
+ setup do
100
+ @output = Whenever.cron \
101
+ <<-file
102
+ set :path, '/your/path'
103
+ every "mon,wed,fri", :at => '5:02am, 3:02pm' do
104
+ rake "blah:blah"
105
+ end
106
+ file
107
+ end
108
+
109
+ should "output the rake task using one entry because the times are aligned" do
110
+ assert_match '2 5,15 * * 1,3,5 cd /your/path && RAILS_ENV=production /usr/bin/env rake blah:blah', @output
111
+ end
112
+ end
113
+
114
+ context "A command every 1.month at very diverse times" do
115
+ setup do
116
+ @output = Whenever.cron \
117
+ <<-file
118
+ every [1.month, 1.day], :at => 'beginning of the month at 5:02am, june 17th at 2:22pm, june 3rd at 3:33am' do
119
+ command "blahblah"
120
+ end
121
+ file
122
+ end
123
+
124
+ should "output 6 commands since none align" do
125
+ # The 1.month commands
126
+ assert_match '2 5 1 * * blahblah', @output
127
+ assert_match '22 14 17 * * blahblah', @output
128
+ assert_match '33 3 3 * * blahblah', @output
129
+
130
+ # The 1.day commands
131
+ assert_match '2 5 * * * blahblah', @output
132
+ assert_match '22 14 * * * blahblah', @output
133
+ assert_match '33 3 * * * blahblah', @output
134
+ end
135
+ end
136
+
137
+ end
@@ -84,4 +84,21 @@ class OutputCommandTest < Test::Unit::TestCase
84
84
  end
85
85
  end
86
86
 
87
+ context "An every statement with two commands in it" do
88
+ setup do
89
+ @output = Whenever.cron \
90
+ <<-file
91
+ every 1.hour do
92
+ command "first"
93
+ command "second"
94
+ end
95
+ file
96
+ end
97
+
98
+ should "output both commands" do
99
+ assert_match "0 * * * * first", @output
100
+ assert_match "0 * * * * second", @output
101
+ end
102
+ end
103
+
87
104
  end
@@ -190,4 +190,20 @@ class OutputRunnerTest < Test::Unit::TestCase
190
190
  end
191
191
  end
192
192
 
193
- end
193
+ context "A runner which makes use of double quotes" do
194
+ setup do
195
+ @output = Whenever.cron \
196
+ <<-file
197
+ set :path, '/my/path'
198
+ every 2.hours do
199
+ runner 'Product.import("http://example.com/product.xml")'
200
+ end
201
+ file
202
+ end
203
+
204
+ should "output the runner using the original environmnet" do
205
+ assert_match two_hours + ' /my/path/script/runner -e production "Product.import(\"http://example.com/product.xml\")"', @output
206
+ end
207
+ end
208
+
209
+ end
data/whenever.gemspec CHANGED
@@ -2,16 +2,16 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{whenever}
5
- s.version = "0.3.1"
5
+ s.version = "0.3.5"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Javan Makhmali"]
9
- s.date = %q{2009-06-25}
9
+ s.date = %q{2009-07-13}
10
10
  s.description = %q{Provides clean ruby syntax for defining messy cron jobs and running them Whenever.}
11
11
  s.email = %q{javan@javan.us}
12
12
  s.executables = ["whenever", "wheneverize"]
13
13
  s.extra_rdoc_files = ["bin/whenever", "bin/wheneverize", "CHANGELOG.rdoc", "lib/base.rb", "lib/command_line.rb", "lib/job_list.rb", "lib/job_types/default.rb", "lib/job_types/rake_task.rb", "lib/job_types/runner.rb", "lib/outputs/cron.rb", "lib/version.rb", "lib/whenever.rb", "README.rdoc"]
14
- s.files = ["bin/whenever", "bin/wheneverize", "CHANGELOG.rdoc", "lib/base.rb", "lib/command_line.rb", "lib/job_list.rb", "lib/job_types/default.rb", "lib/job_types/rake_task.rb", "lib/job_types/runner.rb", "lib/outputs/cron.rb", "lib/version.rb", "lib/whenever.rb", "Manifest", "Rakefile", "README.rdoc", "test/command_line_test.rb", "test/cron_test.rb", "test/output_command_test.rb", "test/output_env_test.rb", "test/output_rake_test.rb", "test/output_runner_test.rb", "test/test_helper.rb", "whenever.gemspec"]
14
+ s.files = ["bin/whenever", "bin/wheneverize", "CHANGELOG.rdoc", "lib/base.rb", "lib/command_line.rb", "lib/job_list.rb", "lib/job_types/default.rb", "lib/job_types/rake_task.rb", "lib/job_types/runner.rb", "lib/outputs/cron.rb", "lib/version.rb", "lib/whenever.rb", "Manifest", "Rakefile", "README.rdoc", "test/command_line_test.rb", "test/cron_test.rb", "test/output_at_test.rb", "test/output_command_test.rb", "test/output_env_test.rb", "test/output_rake_test.rb", "test/output_runner_test.rb", "test/test_helper.rb", "whenever.gemspec"]
15
15
  s.has_rdoc = true
16
16
  s.homepage = %q{http://github.com/javan/whenever}
17
17
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Whenever", "--main", "README.rdoc"]
@@ -19,7 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.rubyforge_project = %q{whenever}
20
20
  s.rubygems_version = %q{1.3.1}
21
21
  s.summary = %q{Provides clean ruby syntax for defining messy cron jobs and running them Whenever.}
22
- s.test_files = ["test/command_line_test.rb", "test/cron_test.rb", "test/output_command_test.rb", "test/output_env_test.rb", "test/output_rake_test.rb", "test/output_runner_test.rb", "test/test_helper.rb"]
22
+ s.test_files = ["test/command_line_test.rb", "test/cron_test.rb", "test/output_at_test.rb", "test/output_command_test.rb", "test/output_env_test.rb", "test/output_rake_test.rb", "test/output_runner_test.rb", "test/test_helper.rb"]
23
23
 
24
24
  if s.respond_to? :specification_version then
25
25
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: javan-whenever
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Javan Makhmali
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-25 00:00:00 -07:00
12
+ date: 2009-07-13 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -61,6 +61,7 @@ files:
61
61
  - README.rdoc
62
62
  - test/command_line_test.rb
63
63
  - test/cron_test.rb
64
+ - test/output_at_test.rb
64
65
  - test/output_command_test.rb
65
66
  - test/output_env_test.rb
66
67
  - test/output_rake_test.rb
@@ -101,6 +102,7 @@ summary: Provides clean ruby syntax for defining messy cron jobs and running the
101
102
  test_files:
102
103
  - test/command_line_test.rb
103
104
  - test/cron_test.rb
105
+ - test/output_at_test.rb
104
106
  - test/output_command_test.rb
105
107
  - test/output_env_test.rb
106
108
  - test/output_rake_test.rb