rake后台任务无法运行

发布于 2024-09-17 07:35:53 字数 544 浏览 4 评论 0原文

我想像这样在rails中使用rake后台任务

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

这在开发环境中没问题,但在生产模式下不起作用。

我使用记录器检查命令字符串是否生成正常,但在生产环境中似乎一切都很好:

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 &

任何人都知道为什么这不能在生产模式下工作?

谢谢

I want to 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℡Ms空城旧梦 2024-09-24 07:35:53

这是我的测试设置来复制您的问题。

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

它会运行。它只是不会进入日志。您可以在以生产模式运行的 rails console 中看到它在此处运行。

>> 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 在生产模式下禁用日志记录。所以创建一个新的。

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

作为您可能不想要的免费建议的旁注,这是运行后台作业的糟糕方法。看看delayed_job gem。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文