title: EventMachine UNIX socket connections: how do I use them with Thin running as a service?
body: <p>How can I use <code>EventMachine.connect_unix_domain</code> while running Thin as a service (using the init script (excerpt) and configuration below). The code directly below is the problem (I get an <code>eventmachine not initialized: evma_connect_to_unix_server</code> error). The second code example works, but doesn't allow me to daemonize thin (I don't think). Does Thin not already have a running instance of EventMachine?</p>
  
  <p>UPDATE: Oddly enough: stopping the server (with <code>service thin stop</code>), seems to get into the config.ru file and run the app (so it works, until the <code>stop</code> command times out and kills the process). What happens when thin stops that could be causing this behavior?</p>
  
  <h1>Problematic Code</h1>
  
  <pre><code>class Server &lt; Sinatra::Base
  # Webserver code removed
  end
  
  module Handler
    def receive_data data
      $received_data_changed = 1
      $received_data = data
    end
  end
  
  $sock = EventMachine.connect_unix_domain("/tmp/mysock.sock", Handler)
  </code></pre>
  
  <h1>Working Code</h1>
  
  <pre><code>EventMachine.run do
    class Server &lt; Sinatra::Base
    # Webserver code removed
    end
  
    module Handler
      def receive_data data
        $received_data_changed = 1
        $received_data = data
      end
    end
    $sock = EventMachine.connect_unix_domain("/tmp/mysock.sock", Handler)
    Server.run!(:port =&gt; 4567)
  end
  </code></pre>
  
  <h1>Init Script excerpt</h1>
  
  <pre><code>DAEMON=/usr/local/bin/thin
  SCRIPT_NAME=/etc/init.d/thin
  CONFIG_PATH=/etc/thin
  
  # Exit if the package is not installed
  [ -x "$DAEMON" ] || exit 0
  
  case "$1" in
    start)
      $DAEMON start --all $CONFIG_PATH
      ;;
  </code></pre>
  
  <h1>Thin Config</h1>
  
  <pre><code>--- 
  chdir: /var/www
  environment: development
  timeout: 30
  log: log/thin.log
  pid: tmp/pids/thin.pid
  max_conns: 1024
  max_persistent_conns: 512
  require: []
  
  wait: 30
  servers: 1
  socket: /tmp/thin.server.sock
  daemonize: true
  </code></pre>