Ruby 守护进程导致 ActiveRecord 记录器 IOError

发布于 2024-08-10 23:03:34 字数 583 浏览 7 评论 0原文

我目前正在 Ruby 中编写一个项目,该项目使用 ActiveRecord gem 进行数据库交互,并且我尝试使用 ActiveRecord::Base.logger 属性和以下代码

ActiveRecord::Base.logger = Logger.new(File.open('logs/database.log', 'a'))

这对于迁移等来说效果很好(出于某种原因,这似乎需要启用日志记录,因为它在禁用时会给出 NilClass 错误),但是当我尝试运行包含调用 ActiveRecord 对象的线程守护程序的项目时,脚本失败出现以下错误

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/logger.rb:504:in `write': closed stream (IOError)

任何有关如何解决此问题的想法将不胜感激。目前我已经开始查看其他代码,看看人们是否有其他方法以更线程安全的方式实现 ActiveRecord 日志记录

谢谢

帕特里克

I'm writing a project at the moment in Ruby which makes use of the ActiveRecord gem for database interaction and I'm trying to log all the database activity using the ActiveRecord::Base.logger attribute with the following code

ActiveRecord::Base.logger = Logger.new(File.open('logs/database.log', 'a'))

This works fine for migrations etc (which for some reason seem to require that logging be enabled as it gives a NilClass error when it's disabled) but when I try to run the project which includes a threaded daemon calling the ActiveRecord object the script fails with the following error

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/logger.rb:504:in `write': closed stream (IOError)

Any ideas on how to solve this problem would be greatly appreciated. For the moment I've started to look through other code to see if people have other ways of implementing ActiveRecord logging in a more thread-safe manner

Thanks

Patrick

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

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

发布评论

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

评论(3

自演自醉 2024-08-17 23:03:34

我遇到了同样的问题。您需要首先进行守护进程,然后加载 Rails 环境。

I ran into the same issue. You need to daemonize first, and then load the Rails environment.

溇涏 2024-08-17 23:03:34

Delayed_Job 使用了守护进程和 ActiveRecord,
在守护进程之前,获取文件已打开,然后在守护进程中重新打开

@files_to_reopen = []
ObjectSpace.each_object(File) do |file|
  @files_to_reopen << file unless file.closed?
end

Daemons.run_proc("xxxxx_name",:dir=>pid_file,:dir_mode=>:normal) do
  Dir.chdir(Rails.root)
  # Re-open file handles
  @files_to_reopen.each do |file|
    begin
      file.reopen file.path
      file.sync = true
    rescue ::Exception
    end
  end
end

the delayed_job have used daemons and activerecord,
before daemonize,get the files have opend,and then reopen in daemonize

@files_to_reopen = []
ObjectSpace.each_object(File) do |file|
  @files_to_reopen << file unless file.closed?
end

Daemons.run_proc("xxxxx_name",:dir=>pid_file,:dir_mode=>:normal) do
  Dir.chdir(Rails.root)
  # Re-open file handles
  @files_to_reopen.each do |file|
    begin
      file.reopen file.path
      file.sync = true
    rescue ::Exception
    end
  end
end
微凉徒眸意 2024-08-17 23:03:34

事实证明,要使迁移工作,ActiveRecord::Base.logger 变量不能为 nil,这解释了问题的前半部分。尽管当使用文件而不是 STDERR 时,我仍然无法修复 IOError。

It turns out that for migrations to work the ActiveRecord::Base.logger variable cannot be nil, which explains the first half of the problem. I am as yet unable to fix the IOError though when a file is used instead of STDERR.

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