diff --git a/lib/base.rb b/lib/base.rb index de58448..118c8f3 100644 --- a/lib/base.rb +++ b/lib/base.rb @@ -1,12 +1,15 @@ -require 'job_list' -require 'job_types/default' -require 'job_types/runner' -require 'outputs/cron' - module Whenever VERSION = '0.1.0' def self.cron(options) Whenever::JobList.new(options).generate_cron_output end + + def self.path + if defined?(RAILS_ROOT) + RAILS_ROOT + elsif defined?(::RAILS_ROOT) + ::RAILS_ROOT + end + end end \ No newline at end of file diff --git a/lib/job_list.rb b/lib/job_list.rb index d634de1..21a799f 100644 --- a/lib/job_list.rb +++ b/lib/job_list.rb @@ -41,10 +41,16 @@ module Whenever end def runner(task, options = {}) - options.reverse_merge!(:environment => @runner_environment, :path => @runner_path) + options.reverse_merge!(:environment => @environment, :path => @path) options[:class] = Whenever::Job::Runner command(task, options) end + + def rake(task, options = {}) + options.reverse_merge!(:environment => @environment, :path => @path) + options[:class] = Whenever::Job::RakeTask + command(task, options) + end def generate_cron_output [environment_variables, cron_jobs].compact.join diff --git a/lib/job_types/default.rb b/lib/job_types/default.rb index b787d93..d857923 100644 --- a/lib/job_types/default.rb +++ b/lib/job_types/default.rb @@ -1,17 +1,27 @@ module Whenever module Job class Default + attr_accessor :task, :at, :cron_log def initialize(options = {}) - @task = options[:task] - @at = options[:at] - @cron_log = options[:cron_log] + @task = options[:task] + @at = options[:at] + @cron_log = options[:cron_log] + @environment = options[:environment] || :production + @path = options[:path] || Whenever.path end def output task end + + protected + + def path_required + raise ArgumentError, "No path available; set :path, '/your/path' in your schedule file" if @path.blank? + end + end end end \ No newline at end of file diff --git a/lib/job_types/rake_task.rb b/lib/job_types/rake_task.rb new file mode 100644 index 0000000..360e173 --- /dev/null +++ b/lib/job_types/rake_task.rb @@ -0,0 +1,12 @@ +module Whenever + module Job + class RakeTask < Whenever::Job::Default + + def output + path_required + "cd #{@path} && RAILS_ENV=#{@environment} /usr/bin/env rake #{task}" + end + + end + end +end diff --git a/lib/job_types/runner.rb b/lib/job_types/runner.rb index 8d6049d..ade9ea7 100644 --- a/lib/job_types/runner.rb +++ b/lib/job_types/runner.rb @@ -1,30 +1,12 @@ module Whenever module Job class Runner < Whenever::Job::Default - - def initialize(options = {}) - super(options) - - @environment = options[:environment] || :production - - if [Whenever::Job::Runner.rails_root, options[:path]].all?(&:blank?) - raise ArgumentError, "no cron_path available for runner to use" - else - @path = options[:path] || Whenever::Job::Runner.rails_root - end - end - - def self.rails_root - if defined?(RAILS_ROOT) - RAILS_ROOT - elsif defined?(::RAILS_ROOT) - ::RAILS_ROOT - end - end def output + path_required %Q(#{File.join(@path, 'script', 'runner')} -e #{@environment} "#{task}") end + end end end diff --git a/lib/whenever.rb b/lib/whenever.rb index fd24b29..6d2cb28 100644 --- a/lib/whenever.rb +++ b/lib/whenever.rb @@ -22,3 +22,8 @@ require 'activesupport' require 'chronic' require 'base' +require 'job_list' +require 'job_types/default' +require 'job_types/rake_task' +require 'job_types/runner' +require 'outputs/cron' diff --git a/test/output_command_test.rb b/test/output_command_test.rb index 110dd3f..35ad617 100644 --- a/test/output_command_test.rb +++ b/test/output_command_test.rb @@ -4,7 +4,7 @@ class OutputCommandTest < Test::Unit::TestCase context "A plain command" do setup do - @output = load_whenever_output \ + @output = Whenever.cron \ <<-file every 2.hours do command "blahblah" @@ -19,7 +19,7 @@ class OutputCommandTest < Test::Unit::TestCase context "A command when the cron_log is set" do setup do - @output = load_whenever_output \ + @output = Whenever.cron \ <<-file set :cron_log, 'logfile.log' every 2.hours do @@ -35,7 +35,7 @@ class OutputCommandTest < Test::Unit::TestCase context "A command when the cron_log is set and the comand overrides it" do setup do - @output = load_whenever_output \ + @output = Whenever.cron \ <<-file set :cron_log, 'logfile.log' every 2.hours do @@ -52,7 +52,7 @@ class OutputCommandTest < Test::Unit::TestCase context "A command when the cron_log is set and the comand rejects it" do setup do - @output = load_whenever_output \ + @output = Whenever.cron \ <<-file set :cron_log, 'logfile.log' every 2.hours do diff --git a/test/output_env_test.rb b/test/output_env_test.rb index 9d2ec5d..1c643f4 100644 --- a/test/output_env_test.rb +++ b/test/output_env_test.rb @@ -4,7 +4,7 @@ class OutputEnvTest < Test::Unit::TestCase context "The output from Whenever with environment variables set" do setup do - @output = load_whenever_output \ + @output = Whenever.cron \ <<-file env :MYVAR, 'blah' env 'MAILTO', "someone@example.com" diff --git a/test/output_rake_test.rb b/test/output_rake_test.rb new file mode 100644 index 0000000..4e56d5f --- /dev/null +++ b/test/output_rake_test.rb @@ -0,0 +1,74 @@ +require 'test_helper' + +class OutputRakeTest < Test::Unit::TestCase + + # Rake are generated in an almost identical way to runners so we + # only need some basic tests to ensure they are output correctly + + context "A rake command with path set" do + setup do + @output = Whenever.cron \ + <<-file + set :path, '/my/path' + every 2.hours do + rake "blahblah" + end + file + end + + should "output the rake command using that path" do + assert_match two_hours + ' cd /my/path && RAILS_ENV=production /usr/bin/env rake blahblah', @output + end + end + + context "A rake command that overrides the path set" do + setup do + @output = Whenever.cron \ + <<-file + set :path, '/my/path' + every 2.hours do + rake "blahblah", :path => '/some/other/path' + end + file + end + + should "output the rake command using that path" do + assert_match two_hours + ' cd /some/other/path && RAILS_ENV=production /usr/bin/env rake blahblah', @output + end + end + + context "A rake command with environment set" do + setup do + @output = Whenever.cron \ + <<-file + set :environment, :silly + set :path, '/my/path' + every 2.hours do + rake "blahblah" + end + file + end + + should "output the rake command using that environment" do + assert_match two_hours + ' cd /my/path && RAILS_ENV=silly /usr/bin/env rake blahblah', @output + end + end + + context "A rake command that overrides the environment set" do + setup do + @output = Whenever.cron \ + <<-file + set :environment, :silly + set :path, '/my/path' + every 2.hours do + rake "blahblah", :environment => :serious + end + file + end + + should "output the rake command using that environment" do + assert_match two_hours + ' cd /my/path && RAILS_ENV=serious /usr/bin/env rake blahblah', @output + end + end + +end \ No newline at end of file diff --git a/test/output_runner_test.rb b/test/output_runner_test.rb index 5622362..3acc17d 100644 --- a/test/output_runner_test.rb +++ b/test/output_runner_test.rb @@ -2,11 +2,11 @@ require 'test_helper' class OutputRunnerTest < Test::Unit::TestCase - context "A runner with runner_path set" do + context "A runner with path set" do setup do - @output = load_whenever_output \ + @output = Whenever.cron \ <<-file - set :runner_path, '/my/path' + set :path, '/my/path' every 2.hours do runner "blahblah" end @@ -18,11 +18,27 @@ class OutputRunnerTest < Test::Unit::TestCase end end - context "A runner with no runner_path set and RAILS_ROOT defined" do + context "A runner that overrides the path set" do setup do - Whenever::Job::Runner.stubs(:rails_root).returns('/my/path') + @output = Whenever.cron \ + <<-file + set :path, '/my/path' + every 2.hours do + runner "blahblah", :path => '/some/other/path' + end + file + end + + should "output the runner using that path" do + assert_match two_hours + ' /some/other/path/script/runner -e production "blahblah"', @output + end + end + + context "A runner with no path set and RAILS_ROOT defined" do + setup do + Whenever.stubs(:path).returns('/my/path') - @output = load_whenever_output \ + @output = Whenever.cron \ <<-file every 2.hours do runner "blahblah" @@ -35,28 +51,28 @@ class OutputRunnerTest < Test::Unit::TestCase end end - context "A runner with runner_path set AND RAILS_ROOT defined" do + context "A runner with path set AND RAILS_ROOT defined" do setup do - Whenever::Job::Runner.stubs(:rails_root).returns('/my/path') + Whenever.stubs(:path).returns('/my/rails/path') - @output = load_whenever_output \ + @output = Whenever.cron \ <<-file - set :runner_path, '/my/path' + set :path, '/my/path' every 2.hours do runner "blahblah" end file end - should "use the runner_path" do + should "use the path" do assert_match two_hours + ' /my/path/script/runner -e production "blahblah"', @output assert_no_match /\/rails\/path/, @output end end - context "A runner with no runner_path set and no RAILS_ROOT defined" do + context "A runner with no path set and no RAILS_ROOT defined" do setup do - Whenever::Job::Runner.stubs(:rails_root).returns(nil) + Whenever.stubs(:path).returns(nil) @input = <<-file every 2.hours do @@ -67,9 +83,43 @@ class OutputRunnerTest < Test::Unit::TestCase should "raise an exception" do assert_raises ArgumentError do - load_whenever_output(@input) + Whenever.cron(@input) end end end + context "A runner with an environment set" do + setup do + @output = Whenever.cron \ + <<-file + set :environment, :silly + set :path, '/my/path' + every 2.hours do + runner "blahblah" + end + file + end + + should "output the runner using that environment" do + assert_match two_hours + ' /my/path/script/runner -e silly "blahblah"', @output + end + end + + context "A runner that overrides the environment set" do + setup do + @output = Whenever.cron \ + <<-file + set :environment, :silly + set :path, '/my/path' + every 2.hours do + runner "blahblah", :environment => :serious + end + file + end + + should "output the runner using that environment" do + assert_match two_hours + ' /my/path/script/runner -e serious "blahblah"', @output + end + end + end \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index ead54f2..1f0db0e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -21,13 +21,10 @@ require 'whenever' module TestExtensions - def load_whenever_output(input) - Whenever.cron(input) - end - def two_hours "0 0,2,4,6,8,10,12,14,16,18,20,22 * * *" end + end class Test::Unit::TestCase