Ruby daemons calling ActiveRecord IOError

I am writing a project currently in Ruby that uses the ActiveRecord XML interface to interact with the database, and I am trying to register all database activity using an attribute ActiveRecord::Base.loggerwith the following code

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

This works great for porting, etc. (for some reason, it seems that it requires logging to be turned on, because it generated a NilClass error when it was turned off), but when I try to start a project that includes a streaming daemon that calls an ActiveRecord object, w370> not executed 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. At the moment, I began to look at other code to find out if other ways to implement ActiveRecord registration in a more thread-safe mode.

thanks

Patrick

+5
source share
3 answers

I ran into the same problem. You must first demonize and then load the Rails environment.

+4
source

delayed_job used by daemons and activerecord, before daemonize, get files have opend, and then re-open 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
+4
source

, ActiveRecord::Base.logger nil, . IOError, STDERR.

+1

All Articles