开发者

rake background task can not run

I want t开发者_如何学JAVAo use rake background task in rails like this

system("cd #{Rails.root} && RAILS_ENV=#{Rails.env} rake abc:def --trace 2>&1 >> #{Rails.root}/log/rake.log &")

This is ok in development environment, but will not work in production mode.

I used logger to check whether the command string is generated ok or not, but it seems every things is fine in production evironment:

cd /home/username/rails_staging/Abc/releases/20100904034630 && RAILS_ENV=production rake abc:def --trace 2>&1 >> /home/username/rails_staging/Abc/releases/20100904034630/log/rake.log &

Any body has any ideas about why this can not work in production mode?

Thanks


This is my test setup to replicate your problem.

# lib/tasks/whatever.rake
namespace :abc do
  task :def do
    puts "DEF ran in #{Rails.env} mode in the directory #{Rails.root}."
  end
end

$ rake abc:def
DEF ran in development mode in the directory /tmp/boluapp

$ RAILS_ENV=production rake abc:def
DEF ran in production mode in the directory /tmp/bobluapp.

It will run. It's just not going to the log. You can see it run here in the rails console running in production mode.

>> system("cd #{Rails.root} && RAILS_ENV=#{Rails.env} rake abc:def --trace 2>&1 /tmp/rake.log")
** Invoke abc:def (first_time)
** Execute abc:def
DEF ran in production mode in the directory /tmp/boluapp.

Rake disables logging in production mode. So create a new one.

# lib/tasks/whatever.rb
namespace :abc do
  task :def do
    logger = Logger.new("#{Rails.root}/log/rake.log")
    logger.level = Logger::INFO
    logger.info "DEF ran in #{Rails.env} mode in the directory #{Rails.root}."
  end
end

$ RAILS_ENV=production rake abc:def
$ cat log/rake.log
# Logfile created on 2011-11-23 00:00:00 -0500 by logger.rb/31641
DEF ran in production mode in the directory /tmp/boluapp.

As a side note of free advice you probably don't want, this is a bad way to run background jobs. Look at the delayed_job gem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜