Snacktime, as you observed to me via IM, the problem appears to be related
to database connections disappearing across the daemonize fork. I was able
to run EventMachine in a daemonized process on Linux with no problem. Please
let us know here when you solve it. Thanks.
On 8/22/06, snacktime <snacktime at gmail.com>
wrote:>
> Any reason why the following code wouldn''t work with EM? I
included
> the Daemonize module at the end to show what it does, it''s fairly
> short. When I run this EM won''t accept connections. With
daemonize()
> commented out it works fine. Must be something about how it forks
> that EM doesn''t like.
>
> class MyServer
> include Daemonize
>
> def initialize
> daemonize()
> EventMachine::run {
> EventMachine::start_server "127.0.0.1", 8081, Netstring
> }
> end
> end
> server = MyServer.new
>
>
>
----------------------------------------------------------------------------------------
> module Daemonize
> VERSION = "0.1.2"
> # maximum process limit for the system has been reached
> def safefork
> tryagain = true
>
> while tryagain
> if pid = fork
> return pid
> end
> rescue Errno::EWOULDBLOCK
> end
> end
> end
>
> # This method causes the current running process to become a daemon
> # If closefd is true, all existing file descriptors are closed
> def daemonize(oldmode=0, closefd=false)
> srand # Split rand streams between spawning and daemonized process
> safefork and exit # Fork and exit from the parent
>
> # Detach from the controlling terminal
> unless sess_id = Process.setsid
> raise ''Cannot detach from controlled terminal''
> end
>
> # Prevent the possibility of acquiring a controlling terminal
> if oldmode.zero?
> trap ''SIGHUP'', ''IGNORE''
> exit if pid = safefork
> end
>
> Dir.chdir "/" # Release old working directory
> File.umask 0000 # Insure sensible umask
>
> if closefd
> # Make sure all file descriptors are closed
> ObjectSpace.each_object(IO) do |io|
> unless [STDIN, STDOUT, STDERR].include?(io)
> io.close rescue nil
> end
> end
> end
>
> STDIN.reopen "/dev/null" # Free file descriptors and
> STDOUT.reopen "/dev/null", "a" # point them
somewhere sensible
> STDERR.reopen STDOUT # STDOUT/STDERR should go to a logfile
> return oldmode ? sess_id : 0 # Return value is mostly irrelevant
> end
> end
> _______________________________________________
> Eventmachine-talk mailing list
> Eventmachine-talk at rubyforge.org
> http://rubyforge.org/mailman/listinfo/eventmachine-talk
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://rubyforge.org/pipermail/eventmachine-talk/attachments/20060822/ad164ecc/attachment.html