Skip to content

Commit 56160a2

Browse files
committed
feat: Output destination can be dynamically determined from the task
1 parent fd70047 commit 56160a2

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

lib/whenever/job.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def initialize(options = {})
1111
@mailto = options.fetch(:mailto, :default_mailto)
1212
@job_template = options.delete(:job_template) || ":job"
1313
@roles = Array(options.delete(:roles))
14-
@options[:output] = options.has_key?(:output) ? Whenever::Output::Redirection.new(options[:output]).to_s : ''
14+
@options[:output] = options.has_key?(:output) ? Whenever::Output::Redirection.new(options[:output], options[:task]).to_s : ''
1515
@options[:environment_variable] ||= "RAILS_ENV"
1616
@options[:environment] ||= :production
1717
@options[:path] = Shellwords.shellescape(@options[:path] || Whenever.path)

lib/whenever/output_redirection.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
module Whenever
22
module Output
33
class Redirection
4-
def initialize(output)
4+
def initialize(output, task)
55
@output = output
6+
@task = task
67
end
78

89
def to_s
@@ -11,7 +12,7 @@ def to_s
1112
when String then redirect_from_string
1213
when Hash then redirect_from_hash
1314
when NilClass then ">> /dev/null 2>&1"
14-
when Proc then @output.call
15+
when Proc then @output.lambda? ? @output.call : @output.call(@task)
1516
else ''
1617
end
1718
end

test/functional/output_redirection_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,20 @@ class OutputRedirectionTest < Whenever::TestCase
245245

246246
assert_match(/^.+ .+ .+ .+ blahblah 2>&1 | logger -t whenever_cron$/, output)
247247
end
248+
249+
test 'a command when the standard output is set to a proc' do
250+
output = Whenever.cron \
251+
<<-file
252+
# :output must be escaped so that eval can be executed
253+
set :job_template, nil
254+
set :output, proc { |task| "2>&1 | " + task.sub(/\\..+$/, '') + ".log" }
255+
every 2.hours do
256+
command "/path/to/file_a.rb"
257+
command "/path/to/file_b"
258+
end
259+
file
260+
261+
assert_match(%r{^.+ .+ .+ .+ /path/to/file_a.rb 2>&1 | /path/to/file_a.log$}, output)
262+
assert_match(%r{^.+ .+ .+ .+ /path/to/file_b 2>&1 | /path/to/file_b.log$}, output)
263+
end
248264
end

0 commit comments

Comments
 (0)