Multiplay Labs

tech hits and tips from Multiplay

Archive for July 22nd, 2009

Rails Spawn in production mode = missing log entries

with 2 comments

When running under passenger in production mode ( which uses buffered logging ) when an error occurs in a task / process which is created using Spawn the log messages are lost. This is due to the call to exit! in spawns fork_it method.

The following patch fixes this and a few more minor issues

--- vendor/plugins/spawn/lib/spawn.rb.orig      2009-07-22 20:27:51.000000000 +0100
+++ vendor/plugins/spawn/lib/spawn.rb   2009-07-22 20:37:02.000000000 +0100
@@ -17,7 +17,7 @@
     if !env || env == RAILS_ENV
       @@method = method
     end
-    @@logger.debug "spawn> method = #{@@method}" if defined? RAILS_DEFAULT_LOGGER
+    @@logger.debug "spawn> method = #{@@method}"
   end
 
   # set the resources to disconnect from in the child process (when forking)
@@ -103,7 +103,7 @@
         # run the block of code that takes so long
         yield
 
-      rescue => ex
+      rescue Exception => ex
         @@logger.error "spawn> Exception in child[#{Process.pid}] - #{ex.class}: #{ex.message}"
       ensure
         begin
@@ -116,6 +116,8 @@
           end
         ensure
           @@logger.info "spawn> child[#{Process.pid}] took #{Time.now - start} sec"
+          # Because we use exit! we must try to ensure the log is flushed else output including errors could be lost
+          @@logger.flush if @@logger.respond_to?(:flush)
           # this form of exit doesn't call at_exit handlers
           exit!(0)
         end

Written by Dilbert

July 22nd, 2009 at 8:19 pm

Posted in Hackery,Rails