taskmaster 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -9,8 +9,9 @@ gem 'activesupport'
9
9
  # Add dependencies to develop your gem here.
10
10
  # Include everything needed to run rake, tests, features, etc.
11
11
  group :development do
12
- gem "bundler", "~> 1.0.0"
12
+ gem "bundler", "~> 1"
13
13
  gem "jeweler", "~> 1.6.2"
14
14
  gem "rcov", ">= 0"
15
15
  gem "rake", "0.8.7"
16
+ gem "mocha", "~> 0.9"
16
17
  end
@@ -33,7 +33,7 @@ Check the documentation on `Taskmaster::Henchman.every` for details on all the a
33
33
 
34
34
  == TODO
35
35
 
36
- * add a Rake task to output and save crontab for all Taskmaster-including classes (fall back to whenever)
36
+ * add a binary script to fall back to whenever (for writing/updating crontab)
37
37
 
38
38
  == Contributing to taskmaster
39
39
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -4,12 +4,47 @@ require 'taskmaster/henchman'
4
4
  require 'taskmaster/railtie'
5
5
 
6
6
  module Taskmaster
7
+ def self.aggregate
8
+ load_rails_models
9
+ hash = Henchman.included_in.inject({}) do |hash, klass|
10
+ hash[klass.name] = klass.cron_output.strip
11
+ hash
12
+ end
13
+ hash
14
+ end
15
+
16
+ def self.aggregate_whenever
17
+ load_rails_models
18
+ array = Henchman.included_in.inject([]) do |arr, klass|
19
+ arr << klass.scheduled_jobs
20
+ arr
21
+ end
22
+ array.flatten.join("\n")
23
+ end
24
+
25
+ def self.section(key, cron)
26
+ buffer = []
27
+ buffer << "#-- begin Taskmaster cron for #{application} - #{key}"
28
+ buffer << cron
29
+ buffer << "#-- end Taskmaster cron for #{application} - #{key}\n"
30
+ buffer
31
+ end
32
+
7
33
  def self.cron_output
8
- # make sure all the models that could include Henchman are loaded
34
+ raw_output = aggregate
35
+ raw_output.keys.map { |key| section(key, raw_output[key]) }.join("\n")
36
+ end
37
+
38
+ def self.application
39
+ Rails.root.basename.to_s
40
+ rescue
41
+ "application"
42
+ end
43
+
44
+ private
45
+ def self.load_rails_models
9
46
  if defined?(Rails)
10
47
  Dir[Rails.root.join('app', 'models', '**', '*.rb')].each { |file| require file }
11
48
  end
12
-
13
- Henchman.included_in.map { |klass| klass.cron_output.strip }.join("\n")
14
49
  end
15
50
  end
@@ -30,6 +30,10 @@ end"
30
30
  def cron_output
31
31
  Whenever::JobList.new(@scheduled_jobs.join("\n")).generate_cron_output
32
32
  end
33
+
34
+ def scheduled_jobs
35
+ @scheduled_jobs
36
+ end
33
37
  end
34
38
  end
35
39
  end
@@ -1,8 +1,20 @@
1
- namespace :taskmaster do
1
+ require 'fileutils'
2
2
 
3
- desc "Generate crontab"
4
- task :generate do
3
+ namespace :taskmaster do
4
+ desc "Preview the generated crontab"
5
+ task :preview do
5
6
  output = Taskmaster.cron_output
6
7
  puts output
7
8
  end
9
+
10
+ desc "Write the generated crontab to config/schedule.rb -- suitable for whenever to write it to the system"
11
+ task :write do
12
+ output = Taskmaster.aggregate_whenever
13
+ FileUtils.mkdir_p 'config'
14
+ FileUtils.touch 'config/schedule.rb'
15
+ File.open('config/schedule.rb', File::WRONLY) do |file|
16
+ file << output
17
+ end
18
+ puts "Your crontab has been written to config/schedule.rb. Please use the whenever script to write it to your system crontab."
19
+ end
8
20
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{taskmaster}
8
- s.version = "0.1.1"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ben Scofield"]
@@ -37,7 +37,7 @@ Gem::Specification.new do |s|
37
37
  s.homepage = %q{http://github.com/bscofield/taskmaster}
38
38
  s.licenses = ["MIT"]
39
39
  s.require_paths = ["lib"]
40
- s.rubygems_version = %q{1.6.2}
40
+ s.rubygems_version = %q{1.3.9.2}
41
41
  s.summary = %q{Make your Ruby classes cron-aware}
42
42
 
43
43
  if s.respond_to? :specification_version then
@@ -46,25 +46,28 @@ Gem::Specification.new do |s|
46
46
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
47
  s.add_runtime_dependency(%q<whenever>, [">= 0"])
48
48
  s.add_runtime_dependency(%q<activesupport>, [">= 0"])
49
- s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
49
+ s.add_development_dependency(%q<bundler>, ["~> 1"])
50
50
  s.add_development_dependency(%q<jeweler>, ["~> 1.6.2"])
51
51
  s.add_development_dependency(%q<rcov>, [">= 0"])
52
52
  s.add_development_dependency(%q<rake>, ["= 0.8.7"])
53
+ s.add_development_dependency(%q<mocha>, ["~> 0.9"])
53
54
  else
54
55
  s.add_dependency(%q<whenever>, [">= 0"])
55
56
  s.add_dependency(%q<activesupport>, [">= 0"])
56
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
57
+ s.add_dependency(%q<bundler>, ["~> 1"])
57
58
  s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
58
59
  s.add_dependency(%q<rcov>, [">= 0"])
59
60
  s.add_dependency(%q<rake>, ["= 0.8.7"])
61
+ s.add_dependency(%q<mocha>, ["~> 0.9"])
60
62
  end
61
63
  else
62
64
  s.add_dependency(%q<whenever>, [">= 0"])
63
65
  s.add_dependency(%q<activesupport>, [">= 0"])
64
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
66
+ s.add_dependency(%q<bundler>, ["~> 1"])
65
67
  s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
66
68
  s.add_dependency(%q<rcov>, [">= 0"])
67
69
  s.add_dependency(%q<rake>, ["= 0.8.7"])
70
+ s.add_dependency(%q<mocha>, ["~> 0.9"])
68
71
  end
69
72
  end
70
73
 
@@ -8,6 +8,7 @@ rescue Bundler::BundlerError => e
8
8
  exit e.status_code
9
9
  end
10
10
  require 'test/unit'
11
+ require 'mocha'
11
12
 
12
13
  $LOAD_PATH.unshift(File.dirname(__FILE__))
13
14
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
@@ -1,5 +1,11 @@
1
1
  require 'helper'
2
2
 
3
+ module Rails
4
+ def self.root
5
+ Pathname.new('/root/hydra')
6
+ end
7
+ end
8
+
3
9
  class TestTaskmaster < Test::Unit::TestCase
4
10
  def test_constants_should_exist
5
11
  assert_nothing_raised do
@@ -8,11 +14,80 @@ class TestTaskmaster < Test::Unit::TestCase
8
14
  end
9
15
  end
10
16
 
17
+ def test_taskmaster_should_know_generated_whenever_syntax
18
+ expected = <<-WHENEVERSYNTAX
19
+ every 600, {} do
20
+ runner 'FootSoldier.run'
21
+ end
22
+ every 3600, {} do
23
+ runner 'SpecialSoldier.specialty'
24
+ end
25
+ every 60, {} do
26
+ runner 'MultiSoldier.run'
27
+ end
28
+ every 3600, {} do
29
+ runner 'MultiSoldier.specialty'
30
+ end
31
+ every 3600, {:at=>20} do
32
+ runner 'SpecificSoldier.run'
33
+ end
34
+ every 3600, {} do
35
+ command 'CommandSoldier.run'
36
+ end
37
+ every 3600, {} do
38
+ command 'FrequencySoldier.run'
39
+ end
40
+ WHENEVERSYNTAX
41
+ assert_equal expected.strip, Taskmaster.aggregate_whenever.strip
42
+ end
43
+
11
44
  def test_taskmaster_should_know_all_output
12
- output = Taskmaster.cron_output.split(/\n+/)
13
- assert_equal "0,10,20,30,40,50 * * * * /bin/bash -l -c 'cd /Users/benscofield/personal/util/taskmaster && script/runner -e production '\\''FootSoldier.run'\\'''", output[0]
14
- assert_equal "0 * * * * /bin/bash -l -c 'cd /Users/benscofield/personal/util/taskmaster && script/runner -e production '\\''SpecialSoldier.specialty'\\'''", output[1]
15
- assert_equal "* * * * * /bin/bash -l -c 'cd /Users/benscofield/personal/util/taskmaster && script/runner -e production '\\''MultiSoldier.run'\\'''", output[2]
16
- assert_equal "0 * * * * /bin/bash -l -c 'cd /Users/benscofield/personal/util/taskmaster && script/runner -e production '\\''MultiSoldier.specialty'\\'''", output[3]
45
+ output = Taskmaster.aggregate
46
+
47
+ assert_equal "0,10,20,30,40,50 * * * * /bin/bash -l -c 'cd /Users/benscofield/personal/util/taskmaster && script/runner -e production '\\''FootSoldier.run'\\'''", output['FootSoldier']
48
+ assert_equal "0 * * * * /bin/bash -l -c 'cd /Users/benscofield/personal/util/taskmaster && script/runner -e production '\\''SpecialSoldier.specialty'\\'''", output['SpecialSoldier']
49
+ assert_equal "* * * * * /bin/bash -l -c 'cd /Users/benscofield/personal/util/taskmaster && script/runner -e production '\\''MultiSoldier.run'\\'''", output['MultiSoldier'].split(/\n+/)[0]
50
+ assert_equal "0 * * * * /bin/bash -l -c 'cd /Users/benscofield/personal/util/taskmaster && script/runner -e production '\\''MultiSoldier.specialty'\\'''", output['MultiSoldier'].split(/\n+/)[1]
17
51
  end
52
+
53
+ def test_taskmaster_should_not_know_an_app_name_when_not_in_rails
54
+ Rails.expects(:root).raises(StandardError)
55
+ assert_equal "application", Taskmaster.application
56
+ end
57
+
58
+ def test_taskmaster_should_know_a_rails_app_name
59
+ assert_equal "hydra", Taskmaster.application
60
+ end
61
+
62
+ def test_taskmaster_should_generate_sectioned_crontab_format
63
+ expected = <<-OUTOUTDAMNEDSPOT
64
+ #-- begin Taskmaster cron for hydra - FootSoldier
65
+ 0,10,20,30,40,50 * * * * /bin/bash -l -c 'cd /Users/benscofield/personal/util/taskmaster && script/runner -e production '\\''FootSoldier.run'\\'''
66
+ #-- end Taskmaster cron for hydra - FootSoldier
67
+
68
+ #-- begin Taskmaster cron for hydra - SpecialSoldier
69
+ 0 * * * * /bin/bash -l -c 'cd /Users/benscofield/personal/util/taskmaster && script/runner -e production '\\''SpecialSoldier.specialty'\\'''
70
+ #-- end Taskmaster cron for hydra - SpecialSoldier
71
+
72
+ #-- begin Taskmaster cron for hydra - MultiSoldier
73
+ * * * * * /bin/bash -l -c 'cd /Users/benscofield/personal/util/taskmaster && script/runner -e production '\\''MultiSoldier.run'\\'''
74
+
75
+ 0 * * * * /bin/bash -l -c 'cd /Users/benscofield/personal/util/taskmaster && script/runner -e production '\\''MultiSoldier.specialty'\\'''
76
+ #-- end Taskmaster cron for hydra - MultiSoldier
77
+
78
+ #-- begin Taskmaster cron for hydra - SpecificSoldier
79
+ 20 * * * * /bin/bash -l -c 'cd /Users/benscofield/personal/util/taskmaster && script/runner -e production '\\''SpecificSoldier.run'\\'''
80
+ #-- end Taskmaster cron for hydra - SpecificSoldier
81
+
82
+ #-- begin Taskmaster cron for hydra - CommandSoldier
83
+ 0 * * * * /bin/bash -l -c 'CommandSoldier.run'
84
+ #-- end Taskmaster cron for hydra - CommandSoldier
85
+
86
+ #-- begin Taskmaster cron for hydra - FrequencySoldier
87
+ 0 * * * * /bin/bash -l -c 'FrequencySoldier.run'
88
+ #-- end Taskmaster cron for hydra - FrequencySoldier
89
+ OUTOUTDAMNEDSPOT
90
+
91
+ assert_equal expected, Taskmaster.cron_output
92
+ end
18
93
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: taskmaster
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: whenever
17
- requirement: &2152144960 !ruby/object:Gem::Requirement
17
+ requirement: &2152243520 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *2152144960
25
+ version_requirements: *2152243520
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: activesupport
28
- requirement: &2152143680 !ruby/object:Gem::Requirement
28
+ requirement: &2152243040 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,21 +33,21 @@ dependencies:
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *2152143680
36
+ version_requirements: *2152243040
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: bundler
39
- requirement: &2152142280 !ruby/object:Gem::Requirement
39
+ requirement: &2152242560 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ~>
43
43
  - !ruby/object:Gem::Version
44
- version: 1.0.0
44
+ version: '1'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *2152142280
47
+ version_requirements: *2152242560
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: jeweler
50
- requirement: &2152140880 !ruby/object:Gem::Requirement
50
+ requirement: &2152242080 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ~>
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: 1.6.2
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *2152140880
58
+ version_requirements: *2152242080
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: rcov
61
- requirement: &2152139660 !ruby/object:Gem::Requirement
61
+ requirement: &2152241600 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ! '>='
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: '0'
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *2152139660
69
+ version_requirements: *2152241600
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: rake
72
- requirement: &2152138620 !ruby/object:Gem::Requirement
72
+ requirement: &2152241120 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - =
@@ -77,7 +77,18 @@ dependencies:
77
77
  version: 0.8.7
78
78
  type: :development
79
79
  prerelease: false
80
- version_requirements: *2152138620
80
+ version_requirements: *2152241120
81
+ - !ruby/object:Gem::Dependency
82
+ name: mocha
83
+ requirement: &2152240640 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ~>
87
+ - !ruby/object:Gem::Version
88
+ version: '0.9'
89
+ type: :development
90
+ prerelease: false
91
+ version_requirements: *2152240640
81
92
  description: Taskmaster allows you to easily make your Ruby classes aware of cron.
82
93
  email: git@turrean.com
83
94
  executables: []
@@ -118,7 +129,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
118
129
  version: '0'
119
130
  segments:
120
131
  - 0
121
- hash: -4405291791740595885
132
+ hash: 3484259382040423965
122
133
  required_rubygems_version: !ruby/object:Gem::Requirement
123
134
  none: false
124
135
  requirements:
@@ -127,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
138
  version: '0'
128
139
  requirements: []
129
140
  rubyforge_project:
130
- rubygems_version: 1.6.2
141
+ rubygems_version: 1.3.9.2
131
142
  signing_key:
132
143
  specification_version: 3
133
144
  summary: Make your Ruby classes cron-aware