Rubin: Logger and Demons

I am using ruby ​​1.9.2p180 (2011-02-18 revision 30909)

For logging purposes, I use a logbook. My program has two blocks that are used as daemons.

But writing from these blocks leads to an error and nothing is written to the log file:

log shifting failed. closed stream log writing failed. closed stream 

Here's what happens in the code:

 log = Logger.new(logbase + 'logfile.log', 'monthly') log.level = Logger::INFO proc = Daemons.call(options) do # [...] log.info "Any Logmessage" # [...] end 

Any idea what is wrong there?

+7
source share
2 answers

The Daemons driver closes all file descriptors when it demonizes a process. Thus, any log files that were opened before the Daemons block will be closed inside the forked process.

And since you cannot write to closed file descriptors → errors.

You can learn more about what happens when you demonize a process by reading the chapter:

What do demons internally do with my demons?
http://daemons.rubyforge.org/Daemons.html

The solution is to open the log file inside the daemon block, and not outside it. That should fix it. But keep in mind that demonization changes the working directory to / , so keep this in mind when referencing log file paths.

+8
source

A solution that works successfully in the delayed_job shutter involves extracting all open files before fork and reopening them later.

Corrected Extract from delayed_job :

 @files_to_reopen = [] ObjectSpace.each_object(File) do |file| @files_to_reopen << file unless file.closed? end Daemons.run_proc('some_process') do @files_to_reopen.each do |file| file.reopen file.path, 'a+' file.sync = true end # Your code end 
+2
source

All Articles