Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output destination can be dynamically determined from the task #837

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/whenever/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize(options = {})
@mailto = options.fetch(:mailto, :default_mailto)
@job_template = options.delete(:job_template) || ":job"
@roles = Array(options.delete(:roles))
@options[:output] = options.has_key?(:output) ? Whenever::Output::Redirection.new(options[:output]).to_s : ''
@options[:output] = options.has_key?(:output) ? Whenever::Output::Redirection.new(options[:output], options[:task]).to_s : ''
@options[:environment_variable] ||= "RAILS_ENV"
@options[:environment] ||= :production
@options[:path] = Shellwords.shellescape(@options[:path] || Whenever.path)
Expand Down
5 changes: 3 additions & 2 deletions lib/whenever/output_redirection.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module Whenever
module Output
class Redirection
def initialize(output)
def initialize(output, task)
@output = output
@task = task
end

def to_s
Expand All @@ -11,7 +12,7 @@ def to_s
when String then redirect_from_string
when Hash then redirect_from_hash
when NilClass then ">> /dev/null 2>&1"
when Proc then @output.call
when Proc then @output.lambda? ? @output.call : @output.call(@task)
else ''
end
end
Expand Down
16 changes: 16 additions & 0 deletions test/functional/output_redirection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,20 @@ class OutputRedirectionTest < Whenever::TestCase

assert_match(/^.+ .+ .+ .+ blahblah 2>&1 | logger -t whenever_cron$/, output)
end

test 'a command when the standard output is set to a proc' do
output = Whenever.cron \
<<-file
# :output must be escaped so that eval can be executed
set :job_template, nil
set :output, proc { |task| "2>&1 | " + task.sub(/\\..+$/, '') + ".log" }
every 2.hours do
command "/path/to/file_a.rb"
command "/path/to/file_b"
end
file

assert_match(%r{^.+ .+ .+ .+ /path/to/file_a.rb 2>&1 | /path/to/file_a.log$}, output)
assert_match(%r{^.+ .+ .+ .+ /path/to/file_b 2>&1 | /path/to/file_b.log$}, output)
end
end
9 changes: 8 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
require 'whenever'
require 'test_case'
require 'mocha/setup'

begin
require 'mocha/setup'
rescue LoadError => e
raise unless e.message =~ %r{mocha/setup}

require 'mocha/minitest'
end

module Whenever::TestHelpers
protected
Expand Down