From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Eric Wong Newsgroups: gmane.comp.lang.ruby.mongrel.devel Subject: teaching Mongrel to inherit sockets (and replace mongrel_cluster) Date: Thu, 13 Aug 2009 18:18:06 -0700 Message-ID: <20090814011806.GA13323@dcvr.yhbt.net> Reply-To: mongrel-development-GrnCvJ7WPxnNLxjTenLetw@public.gmane.org NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1250212942 25520 80.91.229.12 (14 Aug 2009 01:22:22 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 14 Aug 2009 01:22:22 +0000 (UTC) To: mongrel-development-GrnCvJ7WPxnNLxjTenLetw@public.gmane.org Original-X-From: mongrel-development-bounces-GrnCvJ7WPxnNLxjTenLetw@public.gmane.org Fri Aug 14 03:22:15 2009 Return-path: Envelope-to: gclrmd-mongrel-development@m.gmane.org Content-Disposition: inline User-Agent: Mutt/1.5.18 (2008-05-17) X-BeenThere: mongrel-development-GrnCvJ7WPxnNLxjTenLetw@public.gmane.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: mongrel-development-bounces-GrnCvJ7WPxnNLxjTenLetw@public.gmane.org Errors-To: mongrel-development-bounces-GrnCvJ7WPxnNLxjTenLetw@public.gmane.org Xref: news.gmane.org gmane.comp.lang.ruby.mongrel.devel:152 Archived-At: Received: from rubyforge.org ([205.234.109.19]) by lo.gmane.org with esmtp (Exim 4.50) id 1MblUk-0004b3-4n for gclrmd-mongrel-development@m.gmane.org; Fri, 14 Aug 2009 03:22:14 +0200 Received: from rubyforge.org (rubyforge.org [127.0.0.1]) by rubyforge.org (Postfix) with ESMTP id 8AE4F18581F8; Thu, 13 Aug 2009 21:22:13 -0400 (EDT) Received: from dcvr.yhbt.net (dcvr.yhbt.net [64.71.152.64]) by rubyforge.org (Postfix) with ESMTP id 413023C8033 for ; Thu, 13 Aug 2009 21:18:09 -0400 (EDT) Received: from localhost (unknown [12.186.229.34]) (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPSA id E61781F7E2; Fri, 14 Aug 2009 01:18:07 +0000 (UTC) List-Post: This change to Mongrel should be completely harmless for non-UNIX systems, too (it won't break (or do) anything). This minimal change let Mongrels inherit listen sockets from a parent process, so that parent could be a cluster manager. This means no more port juggling with mongrel_cluster! The entire Mongrel pack will all share one port. Only the manager script (see below) itself relies on UNIX-isms (but I think mongrel_cluster did, too). diff --git a/lib/mongrel.rb b/lib/mongrel.rb index 0619abe..90e99e7 100644 --- a/lib/mongrel.rb +++ b/lib/mongrel.rb @@ -91,7 +91,11 @@ module Mongrel def initialize(host, port, num_processors=950, throttle=0, timeout=60) tries = 0 - @socket = TCPServer.new(host, port) + @socket = if ENV['LISTEN_FD'] + TCPServer.for_fd(ENV['LISTEN_FD'].to_i) + else + TCPServer.new(host, port) + end if defined?(Fcntl::FD_CLOEXEC) @socket.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) end --- Here's my initial cut of a mongrel_cluster replacement, it can definitely be cleaned up and improved upon with real option parsing and such... ---------------------------------- 8< ---------------------------------- #!/usr/bin/env ruby require 'socket' nr_workers = 4 pids = {} cmd = %w(mongrel_rails start) server = TCPServer.new('0.0.0.0', 3000) server.listen 1024 ENV['LISTEN_FD'] = server.fileno.to_s # pass any signals we receive onto each child process: %w(TERM INT USR1 USR2).each do |sig| trap(sig) do pids.keys.each { |pid| Process.kill(sig, pid) rescue nil } exit 0 if sig == 'TERM' || sig == 'INT' # TERM and INT mean death end end nr_workers.times { |i| pids[fork { exec *cmd }] = i } loop do pid, status = Process.waitpid2(-1) i = pids.delete(pid) or next STDERR.puts "reaped worker #{i}: #{status}, respawning..." pids[fork { exec *cmd }] = i end ---------------------------------- 8< ---------------------------------- -- Eric Wong