I have several frequent Cron jobs that are run via Rake and the output of those jobs are e-mailed (via a MAILTO). Due to the fact that these tasks load the Rails environment (which includes Erubis) they always prints out "** Erubis 2.6.5" on startup. This means that an e-mail is always generated since Cron receives output. Is there any way to configure Erubis to cease printing this startup message to the console?
views:
144answers:
4In general, if you can avoid it, you don't want your cron jobs to load the Rails environment. If you don't care about the output of your cronjob, you can redirect the output to /dev/null. If you do care about the output, you can modify the gem's source code to stop printing out that message when required.
Try sticking this at the top of your environment.rb before erubis is required:
class IO
alias :old_puts puts
def puts(thing)
if not thing.to_s.match(/\A\*\* Erubis [\d.]+\Z/)
return old_puts(thing)
end
end
end
example output. Doesn't look like there's a neater way of doing it unfortunately. Obviously test your app thoroughly after you have added this.
You need to redefine rails_helper.rb in erubis - these are offending lines:
## finish
ActionController::Base.new.logger.info "** Erubis #{::Erubis::VERSION}"
$stdout.puts "** Erubis #{::Erubis::VERSION}" if rails22
I suggest copying content of that file into new one, remove logging lines and requre that better_rails_helper instead of erubis providef one. . `
Rather than modifying the gem itself, it's easier and safer to modify the rails_xss plugin. The part of the plugin that loads the file that daeltar is talking about is at "/plugins/rails_xss/lib/rails_xss/erubis.rb". At the very top of the file is a require:
require 'erubis/helpers/rails_helper'
Modify this require to simply redirect standard output to a dummy IO before the require, and restore standard output when you are done, like this:
stdout_original, $stdout = $stdout, StringIO.new
require 'erubis/helpers/rails_helper'
$stdout = stdout_original
It's ugly, but it solves the problem in a relatively non-intrusive way. I had a similar issue as the OP, needing to pipe the output of a "script/run" process to another process, and erubis was rudely breaking the convention about rails components/plugins being silent on the stdout front (exactly for this reason). The above solution is what I came up with and it works for me.