javan-whenever 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,10 @@
1
+ == 0.1.1 / February 16th, 2009
2
+
3
+ * Added 'rake' helper for defining scheduled rake tasks. [Javan Makhmali]
4
+
5
+ * Renamed :cron_environment and :cron_path to :enviroment and :path for better (word) compatibility with rake tasks. [Javan Makhmali]
6
+
7
+
8
+ == 0.1.0 / February 15th, 2009
9
+
10
+ * Initial release [Javan Makhmali]
data/README.rdoc CHANGED
@@ -1,9 +1,11 @@
1
- = Whenever
1
+ == Introduction
2
2
 
3
- Whenever is a ruby gem that provides a ruby syntax for defining cron jobs. It was designed to work well with Rails applications, but can be used independently as well.
3
+ Whenever is a ruby gem that provides a ruby syntax for defining cron jobs. It is designed to work well with Rails applications, but can be used independently as well.
4
4
 
5
5
  == Installation
6
6
 
7
+ NOTE: Requiring the whenever gem inside your Rails application is technically optional. However, if you plan to use something like Capistrano to automatically deploy and write your crontab file, you'll need to have the gem installed on your servers, and requiring it in your app is one to ensure this. If you plan to manually install the gem on your servers or you don't care about Rails, deploying, etc., you can skip the next step.
8
+
7
9
  To install Whenever in a Rails (2.1 or greater) application:
8
10
 
9
11
  in your "config/environment.rb" file:
@@ -16,8 +18,8 @@ To install this gem (and all other missing gem dependencies), run rake gems:inst
16
18
 
17
19
  In older versions of Rails:
18
20
 
19
- $ gem sources -a http://gems.github.com
20
- $ gem install whenever
21
+ $ gem sources -a http://gems.github.com
22
+ $ gem install javan-whenever
21
23
 
22
24
  in your "config/environment.rb" file:
23
25
 
@@ -29,20 +31,20 @@ in your "config/environment.rb" file:
29
31
 
30
32
  == Getting started
31
33
 
32
- $ cd /my/rails/app
33
- $ wheneverize .
34
+ $ cd /my/rails/app
35
+ $ wheneverize .
34
36
 
35
37
  This will create an initial "config/schedule.rb" file you.
36
38
 
37
39
  == Example schedule.rb file
38
40
 
39
- set :runner_path, '/var/www/apps/my_app' # Whenever will try to use your RAILS_ROOT if this isn't set
40
- set :runner_environment, :production # Whenever defaults to production so only set this if you want to use a different environment.
41
- set :cron_log, '/path/to/my/cronlog.log' # Where to log (this should NOT be your Rails log)
42
-
41
+ set :path, '/var/www/apps/my_app' # Whenever will try to use your RAILS_ROOT if this isn't set
42
+ set :environment, :production # Whenever defaults to production so you only need to set this for something different
43
+ set :cron_log, '/my/cronlog.log' # Where to log (this should NOT be your Rails log)
43
44
 
44
45
  every 2.hours do
45
46
  runner "MyModel.some_process" # runners are the script/runners you know and love
47
+ rake "my:rake:task" # conveniently run rake tasks
46
48
  command "/usr/local/bin/my_great_command" # commands are any unix command
47
49
  end
48
50
 
@@ -60,18 +62,23 @@ This will create an initial "config/schedule.rb" file you.
60
62
 
61
63
  == Cron output
62
64
 
63
- $ cd /my/rails/app
64
- $ whenever
65
+ $ cd /my/rails/app
66
+ $ whenever
65
67
 
66
68
  And you'll see your schedule.rb converted to cron sytax
67
69
 
68
70
  == Capistrano integration
69
71
 
70
- Use the "whenever:write_cron" task to automatically write your crontab file with each deploy.
71
-
72
72
  in your "config/deploy.rb" file do something like:
73
73
 
74
- after "deploy:symlink", "whenever:write_cron"
74
+ after "deploy:symlink", "deploy:write_crontab"
75
+
76
+ namespace :deploy do
77
+ desc "write the crontab file"
78
+ task :write_crontab, :roles => :app do
79
+ run "cd #{release_path} && whenever -c"
80
+ end
81
+ end
75
82
 
76
83
  THIS WILL COMPLETELY OVERWRITE ANY EXISTING CRONTAB ENTRIES!
77
84
  ------------------------------------------------------------
data/lib/base.rb CHANGED
@@ -1,12 +1,15 @@
1
- require 'job_list'
2
- require 'job_types/default'
3
- require 'job_types/runner'
4
- require 'outputs/cron'
5
-
6
1
  module Whenever
7
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
8
3
 
9
4
  def self.cron(options)
10
5
  Whenever::JobList.new(options).generate_cron_output
11
6
  end
7
+
8
+ def self.path
9
+ if defined?(RAILS_ROOT)
10
+ RAILS_ROOT
11
+ elsif defined?(::RAILS_ROOT)
12
+ ::RAILS_ROOT
13
+ end
14
+ end
12
15
  end
data/lib/job_list.rb CHANGED
@@ -41,10 +41,16 @@ module Whenever
41
41
  end
42
42
 
43
43
  def runner(task, options = {})
44
- options.reverse_merge!(:environment => @runner_environment, :path => @runner_path)
44
+ options.reverse_merge!(:environment => @environment, :path => @path)
45
45
  options[:class] = Whenever::Job::Runner
46
46
  command(task, options)
47
47
  end
48
+
49
+ def rake(task, options = {})
50
+ options.reverse_merge!(:environment => @environment, :path => @path)
51
+ options[:class] = Whenever::Job::RakeTask
52
+ command(task, options)
53
+ end
48
54
 
49
55
  def generate_cron_output
50
56
  [environment_variables, cron_jobs].compact.join
@@ -1,17 +1,27 @@
1
1
  module Whenever
2
2
  module Job
3
3
  class Default
4
+
4
5
  attr_accessor :task, :at, :cron_log
5
6
 
6
7
  def initialize(options = {})
7
- @task = options[:task]
8
- @at = options[:at]
9
- @cron_log = options[:cron_log]
8
+ @task = options[:task]
9
+ @at = options[:at]
10
+ @cron_log = options[:cron_log]
11
+ @environment = options[:environment] || :production
12
+ @path = options[:path] || Whenever.path
10
13
  end
11
14
 
12
15
  def output
13
16
  task
14
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
+
15
25
  end
16
26
  end
17
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
@@ -1,30 +1,12 @@
1
1
  module Whenever
2
2
  module Job
3
3
  class Runner < Whenever::Job::Default
4
-
5
- def initialize(options = {})
6
- super(options)
7
-
8
- @environment = options[:environment] || :production
9
-
10
- if [Whenever::Job::Runner.rails_root, options[:path]].all?(&:blank?)
11
- raise ArgumentError, "no cron_path available for runner to use"
12
- else
13
- @path = options[:path] || Whenever::Job::Runner.rails_root
14
- end
15
- end
16
-
17
- def self.rails_root
18
- if defined?(RAILS_ROOT)
19
- RAILS_ROOT
20
- elsif defined?(::RAILS_ROOT)
21
- ::RAILS_ROOT
22
- end
23
- end
24
4
 
25
5
  def output
6
+ path_required
26
7
  %Q(#{File.join(@path, 'script', 'runner')} -e #{@environment} "#{task}")
27
8
  end
9
+
28
10
  end
29
11
  end
30
12
  end
data/lib/whenever.rb CHANGED
@@ -22,3 +22,8 @@ require 'activesupport'
22
22
  require 'chronic'
23
23
 
24
24
  require 'base'
25
+ require 'job_list'
26
+ require 'job_types/default'
27
+ require 'job_types/rake_task'
28
+ require 'job_types/runner'
29
+ require 'outputs/cron'
@@ -4,7 +4,7 @@ class OutputCommandTest < Test::Unit::TestCase
4
4
 
5
5
  context "A plain command" do
6
6
  setup do
7
- @output = load_whenever_output \
7
+ @output = Whenever.cron \
8
8
  <<-file
9
9
  every 2.hours do
10
10
  command "blahblah"
@@ -19,7 +19,7 @@ class OutputCommandTest < Test::Unit::TestCase
19
19
 
20
20
  context "A command when the cron_log is set" do
21
21
  setup do
22
- @output = load_whenever_output \
22
+ @output = Whenever.cron \
23
23
  <<-file
24
24
  set :cron_log, 'logfile.log'
25
25
  every 2.hours do
@@ -35,7 +35,7 @@ class OutputCommandTest < Test::Unit::TestCase
35
35
 
36
36
  context "A command when the cron_log is set and the comand overrides it" do
37
37
  setup do
38
- @output = load_whenever_output \
38
+ @output = Whenever.cron \
39
39
  <<-file
40
40
  set :cron_log, 'logfile.log'
41
41
  every 2.hours do
@@ -52,7 +52,7 @@ class OutputCommandTest < Test::Unit::TestCase
52
52
 
53
53
  context "A command when the cron_log is set and the comand rejects it" do
54
54
  setup do
55
- @output = load_whenever_output \
55
+ @output = Whenever.cron \
56
56
  <<-file
57
57
  set :cron_log, 'logfile.log'
58
58
  every 2.hours do
@@ -4,7 +4,7 @@ class OutputEnvTest < Test::Unit::TestCase
4
4
 
5
5
  context "The output from Whenever with environment variables set" do
6
6
  setup do
7
- @output = load_whenever_output \
7
+ @output = Whenever.cron \
8
8
  <<-file
9
9
  env :MYVAR, 'blah'
10
10
  env 'MAILTO', "someone@example.com"
@@ -0,0 +1,74 @@
1
+ require 'test_helper'
2
+
3
+ class OutputRakeTest < Test::Unit::TestCase
4
+
5
+ # Rake are generated in an almost identical way to runners so we
6
+ # only need some basic tests to ensure they are output correctly
7
+
8
+ context "A rake command with path set" do
9
+ setup do
10
+ @output = Whenever.cron \
11
+ <<-file
12
+ set :path, '/my/path'
13
+ every 2.hours do
14
+ rake "blahblah"
15
+ end
16
+ file
17
+ end
18
+
19
+ should "output the rake command using that path" do
20
+ assert_match two_hours + ' cd /my/path && RAILS_ENV=production /usr/bin/env rake blahblah', @output
21
+ end
22
+ end
23
+
24
+ context "A rake command that overrides the path set" do
25
+ setup do
26
+ @output = Whenever.cron \
27
+ <<-file
28
+ set :path, '/my/path'
29
+ every 2.hours do
30
+ rake "blahblah", :path => '/some/other/path'
31
+ end
32
+ file
33
+ end
34
+
35
+ should "output the rake command using that path" do
36
+ assert_match two_hours + ' cd /some/other/path && RAILS_ENV=production /usr/bin/env rake blahblah', @output
37
+ end
38
+ end
39
+
40
+ context "A rake command with environment set" do
41
+ setup do
42
+ @output = Whenever.cron \
43
+ <<-file
44
+ set :environment, :silly
45
+ set :path, '/my/path'
46
+ every 2.hours do
47
+ rake "blahblah"
48
+ end
49
+ file
50
+ end
51
+
52
+ should "output the rake command using that environment" do
53
+ assert_match two_hours + ' cd /my/path && RAILS_ENV=silly /usr/bin/env rake blahblah', @output
54
+ end
55
+ end
56
+
57
+ context "A rake command that overrides the environment set" do
58
+ setup do
59
+ @output = Whenever.cron \
60
+ <<-file
61
+ set :environment, :silly
62
+ set :path, '/my/path'
63
+ every 2.hours do
64
+ rake "blahblah", :environment => :serious
65
+ end
66
+ file
67
+ end
68
+
69
+ should "output the rake command using that environment" do
70
+ assert_match two_hours + ' cd /my/path && RAILS_ENV=serious /usr/bin/env rake blahblah', @output
71
+ end
72
+ end
73
+
74
+ end
@@ -2,11 +2,11 @@ require 'test_helper'
2
2
 
3
3
  class OutputRunnerTest < Test::Unit::TestCase
4
4
 
5
- context "A runner with runner_path set" do
5
+ context "A runner with path set" do
6
6
  setup do
7
- @output = load_whenever_output \
7
+ @output = Whenever.cron \
8
8
  <<-file
9
- set :runner_path, '/my/path'
9
+ set :path, '/my/path'
10
10
  every 2.hours do
11
11
  runner "blahblah"
12
12
  end
@@ -18,11 +18,27 @@ class OutputRunnerTest < Test::Unit::TestCase
18
18
  end
19
19
  end
20
20
 
21
- context "A runner with no runner_path set and RAILS_ROOT defined" do
21
+ context "A runner that overrides the path set" do
22
22
  setup do
23
- Whenever::Job::Runner.stubs(:rails_root).returns('/my/path')
23
+ @output = Whenever.cron \
24
+ <<-file
25
+ set :path, '/my/path'
26
+ every 2.hours do
27
+ runner "blahblah", :path => '/some/other/path'
28
+ end
29
+ file
30
+ end
31
+
32
+ should "output the runner using that path" do
33
+ assert_match two_hours + ' /some/other/path/script/runner -e production "blahblah"', @output
34
+ end
35
+ end
36
+
37
+ context "A runner with no path set and RAILS_ROOT defined" do
38
+ setup do
39
+ Whenever.stubs(:path).returns('/my/path')
24
40
 
25
- @output = load_whenever_output \
41
+ @output = Whenever.cron \
26
42
  <<-file
27
43
  every 2.hours do
28
44
  runner "blahblah"
@@ -35,28 +51,28 @@ class OutputRunnerTest < Test::Unit::TestCase
35
51
  end
36
52
  end
37
53
 
38
- context "A runner with runner_path set AND RAILS_ROOT defined" do
54
+ context "A runner with path set AND RAILS_ROOT defined" do
39
55
  setup do
40
- Whenever::Job::Runner.stubs(:rails_root).returns('/my/path')
56
+ Whenever.stubs(:path).returns('/my/rails/path')
41
57
 
42
- @output = load_whenever_output \
58
+ @output = Whenever.cron \
43
59
  <<-file
44
- set :runner_path, '/my/path'
60
+ set :path, '/my/path'
45
61
  every 2.hours do
46
62
  runner "blahblah"
47
63
  end
48
64
  file
49
65
  end
50
66
 
51
- should "use the runner_path" do
67
+ should "use the path" do
52
68
  assert_match two_hours + ' /my/path/script/runner -e production "blahblah"', @output
53
69
  assert_no_match /\/rails\/path/, @output
54
70
  end
55
71
  end
56
72
 
57
- context "A runner with no runner_path set and no RAILS_ROOT defined" do
73
+ context "A runner with no path set and no RAILS_ROOT defined" do
58
74
  setup do
59
- Whenever::Job::Runner.stubs(:rails_root).returns(nil)
75
+ Whenever.stubs(:path).returns(nil)
60
76
 
61
77
  @input = <<-file
62
78
  every 2.hours do
@@ -67,9 +83,43 @@ class OutputRunnerTest < Test::Unit::TestCase
67
83
 
68
84
  should "raise an exception" do
69
85
  assert_raises ArgumentError do
70
- load_whenever_output(@input)
86
+ Whenever.cron(@input)
71
87
  end
72
88
  end
73
89
  end
74
90
 
91
+ context "A runner with an environment set" do
92
+ setup do
93
+ @output = Whenever.cron \
94
+ <<-file
95
+ set :environment, :silly
96
+ set :path, '/my/path'
97
+ every 2.hours do
98
+ runner "blahblah"
99
+ end
100
+ file
101
+ end
102
+
103
+ should "output the runner using that environment" do
104
+ assert_match two_hours + ' /my/path/script/runner -e silly "blahblah"', @output
105
+ end
106
+ end
107
+
108
+ context "A runner that overrides the environment set" do
109
+ setup do
110
+ @output = Whenever.cron \
111
+ <<-file
112
+ set :environment, :silly
113
+ set :path, '/my/path'
114
+ every 2.hours do
115
+ runner "blahblah", :environment => :serious
116
+ end
117
+ file
118
+ end
119
+
120
+ should "output the runner using that environment" do
121
+ assert_match two_hours + ' /my/path/script/runner -e serious "blahblah"', @output
122
+ end
123
+ end
124
+
75
125
  end
data/test/test_helper.rb CHANGED
@@ -21,13 +21,10 @@ require 'whenever'
21
21
 
22
22
  module TestExtensions
23
23
 
24
- def load_whenever_output(input)
25
- Whenever.cron(input)
26
- end
27
-
28
24
  def two_hours
29
25
  "0 0,2,4,6,8,10,12,14,16,18,20,22 * * *"
30
26
  end
27
+
31
28
  end
32
29
 
33
30
  class Test::Unit::TestCase
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.1.0"
5
+ s.version = "0.1.1"
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-02-15}
9
+ s.date = %q{2009-02-16}
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
- s.extra_rdoc_files = ["bin/whenever", "bin/wheneverize", "lib/base.rb", "lib/job_list.rb", "lib/job_types/default.rb", "lib/job_types/runner.rb", "lib/outputs/cron.rb", "lib/tasks/whenever.rake", "lib/whenever.rb", "README.rdoc"]
14
- s.files = ["bin/whenever", "bin/wheneverize", "lib/base.rb", "lib/job_list.rb", "lib/job_types/default.rb", "lib/job_types/runner.rb", "lib/outputs/cron.rb", "lib/tasks/whenever.rake", "lib/whenever.rb", "Manifest", "Rakefile", "README.rdoc", "test/cron_test.rb", "test/output_command_test.rb", "test/output_env_test.rb", "test/output_runner_test.rb", "test/test_helper.rb", "whenever.gemspec"]
13
+ s.extra_rdoc_files = ["bin/whenever", "bin/wheneverize", "CHANGELOG.rdoc", "lib/base.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/tasks/whenever.rake", "lib/whenever.rb", "README.rdoc"]
14
+ s.files = ["bin/whenever", "bin/wheneverize", "CHANGELOG.rdoc", "lib/base.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/tasks/whenever.rake", "lib/whenever.rb", "Manifest", "Rakefile", "README.rdoc", "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"]
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/cron_test.rb", "test/output_command_test.rb", "test/output_env_test.rb", "test/output_runner_test.rb", "test/test_helper.rb"]
22
+ s.test_files = ["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"]
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.1.0
4
+ version: 0.1.1
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-02-15 00:00:00 -08:00
12
+ date: 2009-02-16 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -42,9 +42,11 @@ extensions: []
42
42
  extra_rdoc_files:
43
43
  - bin/whenever
44
44
  - bin/wheneverize
45
+ - CHANGELOG.rdoc
45
46
  - lib/base.rb
46
47
  - lib/job_list.rb
47
48
  - lib/job_types/default.rb
49
+ - lib/job_types/rake_task.rb
48
50
  - lib/job_types/runner.rb
49
51
  - lib/outputs/cron.rb
50
52
  - lib/tasks/whenever.rake
@@ -53,9 +55,11 @@ extra_rdoc_files:
53
55
  files:
54
56
  - bin/whenever
55
57
  - bin/wheneverize
58
+ - CHANGELOG.rdoc
56
59
  - lib/base.rb
57
60
  - lib/job_list.rb
58
61
  - lib/job_types/default.rb
62
+ - lib/job_types/rake_task.rb
59
63
  - lib/job_types/runner.rb
60
64
  - lib/outputs/cron.rb
61
65
  - lib/tasks/whenever.rake
@@ -66,6 +70,7 @@ files:
66
70
  - test/cron_test.rb
67
71
  - test/output_command_test.rb
68
72
  - test/output_env_test.rb
73
+ - test/output_rake_test.rb
69
74
  - test/output_runner_test.rb
70
75
  - test/test_helper.rb
71
76
  - whenever.gemspec
@@ -104,5 +109,6 @@ test_files:
104
109
  - test/cron_test.rb
105
110
  - test/output_command_test.rb
106
111
  - test/output_env_test.rb
112
+ - test/output_rake_test.rb
107
113
  - test/output_runner_test.rb
108
114
  - test/test_helper.rb