From 953987c6b2fe5b2acd1c752d11bc5d05abb12d61 Mon Sep 17 00:00:00 2001 From: Javan Makhmali Date: Tue, 19 Oct 2010 13:53:41 -0400 Subject: [PATCH] moved output redirection to the Job class and subing it in from the job templates --- lib/whenever/cron.rb | 11 +-- lib/whenever/job.rb | 4 +- lib/whenever/job_types/default.rb | 6 +- lib/whenever/job_types/rails3.rb | 2 +- lib/whenever/output_redirection.rb | 106 ++++++++++++++--------------- test/unit/job_test.rb | 8 --- 6 files changed, 61 insertions(+), 76 deletions(-) diff --git a/lib/whenever/cron.rb b/lib/whenever/cron.rb index b673d95..8e16bc6 100644 --- a/lib/whenever/cron.rb +++ b/lib/whenever/cron.rb @@ -4,11 +4,10 @@ module Whenever attr_accessor :time, :task - def initialize(time = nil, task = nil, at = nil, output_redirection = nil) + def initialize(time = nil, task = nil, at = nil) @time = time @task = task @at = at.is_a?(String) ? (Chronic.parse(at) || 0) : (at || 0) - @output_redirection = output_redirection end def self.enumerate(item) @@ -24,13 +23,13 @@ module Whenever def self.output(times, job) enumerate(times).each do |time| enumerate(job.at).each do |at| - yield new(time, job.output, at, job.output_redirection).output + yield new(time, job.output, at).output end end end def output - [time_in_cron_syntax, task, output_redirection].compact.join(' ').strip + [time_in_cron_syntax, task].compact.join(' ').strip end def time_in_cron_syntax @@ -40,10 +39,6 @@ module Whenever else parse_time end end - - def output_redirection - Whenever::Output::Cron::OutputRedirection.new(@output_redirection).to_s unless @output_redirection == :not_set - end protected diff --git a/lib/whenever/job.rb b/lib/whenever/job.rb index b15897c..bf34632 100644 --- a/lib/whenever/job.rb +++ b/lib/whenever/job.rb @@ -1,13 +1,13 @@ module Whenever class Job - attr_reader :at, :output_redirection + attr_reader :at def initialize(options = {}) @options = options @at = options[:at] - @output_redirection = options.has_key?(:output) ? options[:output] : :not_set + @options[:output] = Whenever::Output::Redirection.new(options[:output]).to_s if options.has_key?(:output) @options[:environment] ||= :production @options[:path] ||= Whenever.path end diff --git a/lib/whenever/job_types/default.rb b/lib/whenever/job_types/default.rb index e9c7ec1..fadfac9 100644 --- a/lib/whenever/job_types/default.rb +++ b/lib/whenever/job_types/default.rb @@ -1,3 +1,3 @@ -job_type :command, ":task" -job_type :runner, "cd :path && script/runner -e :environment ':task'" -job_type :rake, "cd :path && RAILS_ENV=:environment rake :task --silent" +job_type :command, ":task :output" +job_type :runner, "cd :path && script/runner -e :environment ':task' :output" +job_type :rake, "cd :path && RAILS_ENV=:environment rake :task --silent :output" diff --git a/lib/whenever/job_types/rails3.rb b/lib/whenever/job_types/rails3.rb index 336f64e..144cb09 100644 --- a/lib/whenever/job_types/rails3.rb +++ b/lib/whenever/job_types/rails3.rb @@ -7,5 +7,5 @@ if File.exists?(File.join(Whenever.path, 'script', 'rails')) alias_method(:rails2_runner, :runner) if defined?(:runner) end - job_type :runner, "cd :path && script/rails runner -e :environment ':task'" + job_type :runner, "cd :path && script/rails runner -e :environment ':task' :output" end \ No newline at end of file diff --git a/lib/whenever/output_redirection.rb b/lib/whenever/output_redirection.rb index ee15030..44fa1d2 100644 --- a/lib/whenever/output_redirection.rb +++ b/lib/whenever/output_redirection.rb @@ -1,60 +1,58 @@ module Whenever module Output - class Cron - class OutputRedirection - - def initialize(output) - @output = output - end - - def to_s - return '' unless defined?(@output) - case @output - when String then redirect_from_string - when Hash then redirect_from_hash - when NilClass then ">> /dev/null 2>&1" - else '' - end - end - - protected - - def stdout - return unless @output.has_key?(:standard) - @output[:standard].nil? ? '/dev/null' : @output[:standard] - end - - def stderr - return unless @output.has_key?(:error) - @output[:error].nil? ? '/dev/null' : @output[:error] - end - - def redirect_from_hash - case - when stdout == '/dev/null' && stderr == '/dev/null' - "> /dev/null 2>&1" - when stdout && stderr == '/dev/null' - ">> #{stdout} 2> /dev/null" - when stdout && stderr - ">> #{stdout} 2>> #{stderr}" - when stderr == '/dev/null' - "2> /dev/null" - when stderr - "2>> #{stderr}" - when stdout == '/dev/null' - "> /dev/null" - when stdout - ">> #{stdout}" - else - '' - end - end - - def redirect_from_string - ">> #{@output} 2>&1" - end - + class Redirection + + def initialize(output) + @output = output end + + def to_s + return '' unless defined?(@output) + case @output + when String then redirect_from_string + when Hash then redirect_from_hash + when NilClass then ">> /dev/null 2>&1" + else '' + end + end + + protected + + def stdout + return unless @output.has_key?(:standard) + @output[:standard].nil? ? '/dev/null' : @output[:standard] + end + + def stderr + return unless @output.has_key?(:error) + @output[:error].nil? ? '/dev/null' : @output[:error] + end + + def redirect_from_hash + case + when stdout == '/dev/null' && stderr == '/dev/null' + "> /dev/null 2>&1" + when stdout && stderr == '/dev/null' + ">> #{stdout} 2> /dev/null" + when stdout && stderr + ">> #{stdout} 2>> #{stderr}" + when stderr == '/dev/null' + "2> /dev/null" + when stderr + "2>> #{stderr}" + when stdout == '/dev/null' + "> /dev/null" + when stdout + ">> #{stdout}" + else + '' + end + end + + def redirect_from_string + ">> #{@output} 2>&1" + end + end end end diff --git a/test/unit/job_test.rb b/test/unit/job_test.rb index ad448a0..e607782 100644 --- a/test/unit/job_test.rb +++ b/test/unit/job_test.rb @@ -7,14 +7,6 @@ class JobTest < Test::Unit::TestCase assert_equal 'foo', new_job(:at => 'foo').at end - should "return :output when #output_redirection is called" do - assert_equal 'foo', new_job(:output => 'foo').output_redirection - end - - should "return :not_set when #output_redirection is called and no :output is set" do - assert_equal :not_set, new_job.output_redirection - end - should "substitute the :task when #output is called" do job = new_job(:template => ":task", :task => 'abc123') assert_equal 'abc123', job.output