Skip to content

Commit

Permalink
Added output redirection support
Browse files Browse the repository at this point in the history
* replaced cron_log option with output
  * setting output to nil explicitly redirects to /dev/null
  * updated Readme and wheneverize template with examples
  • Loading branch information
Peer Allan authored and javan committed Oct 17, 2009
1 parent 53b92aa commit be38dd1
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 25 deletions.
39 changes: 39 additions & 0 deletions README.rdoc
Expand Up @@ -66,7 +66,46 @@ This will create an initial "config/schedule.rb" file you.
end

More examples on the wiki: http://wiki.github.com/javan/whenever/instructions-and-examples

== Output redirection

In your schedule.rb file you can specify the redirection options for your commands at a global or command level by setting the 'output' variable.

# adds ">> /path/to/file.log 2>&1" to all commands
set :output => '/path/to/file.log'

Or you can STDOUT and STDERR separately,

# adds ">> cron.log 2> error.log" to all commands
set :output => {:error => 'error.log', :standard => 'cron.log'}

# adds ">> cron.log" to all commands
set :output => {:standard => 'cron.log'}

# adds "2> error.log" to all commands
set :output => {:error => 'error.log'}

Additionally you can set these values at the command level,

every 3.hours do
runner "MyModel.some_process", :output => 'cron.log'
rake "my:rake:task", :output => {:error => 'error.log', :standard => 'cron.log'}
command "/usr/bin/cmd"
end

In all cases you can if you explicitly set the value of any output to 'nil' it will add a redirect to /dev/null

# adds ">> /dev/null 2>&1" to all commands
set :output => nil
set :output => {:error => nil, :standard => nil}

# adds ">> /dev/null" to all commands
set :output => {:standard => nil}

# adds "2> /dev/null" to all commands
set :output => {:error => nil}


== Cron output

$ cd /my/rails/app
Expand Down
4 changes: 3 additions & 1 deletion bin/wheneverize
Expand Up @@ -36,7 +36,9 @@ content = <<-FILE
# Example:
#
# set :cron_log, "/path/to/my/cron_log.log"
# set :output, "/path/to/my/cron_log.log"
# set :output, {:error => '/path/to/error.log', :standard => '/path/to/cron.log'}
# set :output, {:error => '/path/to/error.log', :standard => nil}
#
# every 2.hours do
# command "/usr/bin/some_great_command"
Expand Down
5 changes: 3 additions & 2 deletions lib/job_list.rb
Expand Up @@ -38,7 +38,8 @@ def every(frequency, options = {})
end

def command(task, options = {})
options[:cron_log] ||= @cron_log unless options[:cron_log] === false
@output = :unset unless defined?(@output)
options[:output] ||= options.has_key?(:output) ? options[:output] : @output
options[:class] ||= Whenever::Job::Default
@jobs[@current_time_scope] ||= []
@jobs[@current_time_scope] << options[:class].new(@options.merge(:task => task).merge(options))
Expand Down Expand Up @@ -140,7 +141,7 @@ def cron_jobs
@jobs.each do |time, jobs|
jobs.each do |job|
Whenever::Output::Cron.output(time, job) do |cron|
cron << " >> #{job.cron_log} 2>&1" if job.cron_log
cron << job.redirect_output
cron << "\n\n"

if cron.starts_with?("@")
Expand Down
46 changes: 44 additions & 2 deletions lib/job_types/default.rb
Expand Up @@ -2,12 +2,12 @@ module Whenever
module Job
class Default

attr_accessor :task, :at, :cron_log
attr_accessor :task, :at, :redirect

def initialize(options = {})
@task = options[:task]
@at = options[:at]
@cron_log = options[:cron_log]
@redirect = options[:output]
@environment = options[:environment] || :production
@path = options[:path] || Whenever.path
end
Expand All @@ -16,8 +16,50 @@ def output
task
end

def redirect_output
case @redirect
when String
redirect_from_string
when Hash
redirect_from_hash
when NilClass
" >> /dev/null 2>&1"
else
''
end
end

protected

def stdout
return unless @redirect.has_key?(:standard)
@redirect[:standard].nil? ? '/dev/null' : @redirect[:standard]
end

def stderr
return unless @redirect.has_key?(:error)
@redirect[:error].nil? ? '/dev/null' : @redirect[:error]
end

def redirect_from_hash
case
when stdout == '/dev/null' && stderr == '/dev/null'
" >> /dev/null 2>&1"
when stdout && stderr
" >> #{stdout} 2> #{stderr}"
when stderr
" 2> #{stderr}"
when stdout
" >> #{stdout}"
else
''
end
end

def redirect_from_string
" >> #{@redirect} 2>&1"
end

def path_required
raise ArgumentError, "No path available; set :path, '/your/path' in your schedule file" if @path.blank?
end
Expand Down

0 comments on commit be38dd1

Please sign in to comment.