Multiplay Labs

tech hits and tips from Multiplay

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
-    @@logger.debug "spawn> method = #{@@method}" if defined? RAILS_DEFAULT_LOGGER
+    @@logger.debug "spawn> method = #{@@method}"
   # 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
-      rescue => ex
+      rescue Exception => ex
         @@logger.error "spawn> Exception in child[#{}] - #{ex.class}: #{ex.message}"
@@ -116,6 +116,8 @@
  "spawn> child[#{}] took #{ - 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

Written by Dilbert

July 22nd, 2009 at 8:19 pm

Posted in Hackery,Rails

2 Responses to 'Rails Spawn in production mode = missing log entries'

Subscribe to comments with RSS or TrackBack to 'Rails Spawn in production mode = missing log entries'.

  1. I just fixed this on github… thanks for the find!


    20 Nov 09 at 06:28

  2. Thanks for the heads up Squeat appreciated 🙂

    Steven Hartland

    20 Nov 09 at 11:02

Leave a Reply

You must be logged in to post a comment.