views:

144

answers:

4

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?

A: 

In 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.

RyanTM
A: 

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.

A: 

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. . `

daeltar
A: 

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.

Ben
If you still want to see the "Erubis" message but don't want it to pipe to other processes, you can redirect standard output to a copy of standard error instead of a dummy string. To do this, in the snippet above replace "StringIO.new" with "$stderr.dup"
Ben