moved output redirection to the Job class and subing it in from the job templates
This commit is contained in:
parent
4e0c357f4d
commit
953987c6b2
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
@ -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"
|
||||
|
@ -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
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user