日志在生产中无法正常工作并延迟工作 [英] log doesn't work in production with delayed job

查看:120
本文介绍了日志在生产中无法正常工作并延迟工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些奇怪的问题,即我的delay_jobs生产失败。最后,我将其范围缩小到记录器。如果我注释掉我的log函数调用,则一切正常。但是,如果我尝试登录,则会在delay_job处理程序中得到它:

  ---!ruby / struct:Delayed :: PerformableMethod 
对象:AR:User:1
方法::load_and_update_without_send_later
args:[]

|闭流
/opt/ruby/lib/ruby/1.8/logger.rb:504:在`write'
/opt/ruby/lib/ruby/1.8/logger.rb:504:in`在'synchronize'中写入'
/opt/ruby/lib/ruby/1.8/monitor.rb:242:in`'b'b $ b /opt/ruby/lib/ruby/1.8/logger.rb:496:in`在`add'中写'
/opt/ruby/lib/ruby/1.8/logger.rb:326:在`add'
/opt/ruby/lib/ruby/1.8/logger.rb:374:in`信息'
/home/rails/myapp.com/releases/20100203203031/app/models/gmail.rb:35:在`log'


我的记录器如下:

  @@ error_log_file = File.open( #{RAILS_ROOT} /log/error.log,'a')
@@ error_log_file.sync = true
def log(msg)
msg.each | line |
line =#{Time.now.strftime('%H:%M:%S')}#{line}
@@ error_log_file.info(line)#这是第35行,其中
放置失败
放线
结束
结束

如果我评论



这是一个延迟的工作问题,还是与我的日志目录有关?符号链接(由标准的capistrano部署设置)?



也没有将任何内容写入我的error.log文件,也没有任何内容写入delay_job.log。完全陷入困境...

解决方案

伍迪·彼得森(Woody Peterson)在这里发现了问题: http://groups.google.com/group/delayed_job/browse_thread/thread/f7d0534bb6c7c83f/37bpa b $ b

问题是:


DJ在生产中使用Rails的缓冲日志,并且由于某种原因未触发刷新缓冲(不知道是按缓冲区大小刷新还是在请求后显式刷新)。


临时解决方法(归功于Nathan Phelps):


在生产中,缓冲的日志设置为auto_flushing值1000,这意味着直到记录了1000条消息后才会调用flush。假设您使用的是Collectoridea的delay_job分支,则可以在记录器在第64行初始化后立即在command.rb中将auto_flushing设置为更合理的值来解决此问题。IE


Delayed: :Worker.logger = Rails.logger


Delayed :: Worker.logger.auto_flushing = 1#或其他


非常适合我!


I'm suffering some weird issue where my delayed_jobs are failing in production. Finally I narrowed it down to the logger. If I comment out my log function calls, everything works. However if I try to log, I get this in the delayed_job handler:

 --- !ruby/struct:Delayed::PerformableMethod 
object: AR:User:1
method: :load_and_update_without_send_later
args: []

 | closed stream
/opt/ruby/lib/ruby/1.8/logger.rb:504:in `write'
/opt/ruby/lib/ruby/1.8/logger.rb:504:in `write'
/opt/ruby/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/opt/ruby/lib/ruby/1.8/logger.rb:496:in `write'
/opt/ruby/lib/ruby/1.8/logger.rb:326:in `add'
/opt/ruby/lib/ruby/1.8/logger.rb:374:in `info'
/home/rails/myapp.com/releases/20100203203031/app/models/gmail.rb:35:in `log'

My logger looks like this:

@@error_log_file = File.open("#{RAILS_ROOT}/log/error.log", 'a')
@@error_log_file.sync = true
def log(msg)
  msg.each do |line|
    line = "#{Time.now.strftime('%H:%M:%S')}  #{line}"
    @@error_log_file.info(line) # this is line 35 where it's failing
    puts line
  end
end

If I comment out the line "@@error_log_file.sync = true", it also works.

Is this a delayed job problem, or could it be related to my log directory being a symbolic link (as setup by a standard capistrano deploy)?

Also nothing is being written to my error.log file and nothing is being written to delayed_job.log. Totally stumped...

解决方案

Woody Peterson found the problem here: http://groups.google.com/group/delayed_job/browse_thread/thread/f7d0534bb6c7c83f/37b4e8ed7bfaba42

The problem is:

DJ is using Rails' buffered log in production, and flushing the buffer is not being triggered for some reason (don't know if it's flushed by buffer size or explicitly flushed after a request).

The temporary fix (credit to Nathan Phelps) is:

When in production the buffered log is set to an auto_flushing value of 1000 which means that flush is not called until 1000 messages have been logged. Assuming you're using collectiveidea's fork of delayed_job, you can address this by setting auto_flushing to a more reasonable value in command.rb right after the logger is initialized on line 64. I.E.

Delayed::Worker.logger = Rails.logger

Delayed::Worker.logger.auto_flushing = 1 # or whatever

Works for me perfectly!

这篇关于日志在生产中无法正常工作并延迟工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆