title: God resque start gives "The server is not available"
body: <p>I'm having trouble figuring out how to get God to restart resque.</p>
  
  <p>I've got a Rails 3.2.2 stack on a Ubuntu 10.04.3 LTS Linode slice. Its running system Ruby 1.9.3-p194 (no RVM). </p>
  
  <p>There's a God init.d service at <code>/etc/init.d/god-service</code> that contains:</p>
  
  <pre><code>CONF_DIR=/etc/god
  GOD_BIN=/var/www/myapp.com/shared/bundle/ruby/1.9.1/bin/god
  RUBY_BIN=/usr/local/bin/ruby
  RETVAL=0
  
  # Go no further if config directory is missing.
  [ -d "$CONF_DIR" ] || exit 0
  
  case "$1" in
      start)
          # Create pid directory
          $RUBY_BIN $GOD_BIN -c $CONF_DIR/master.conf
          RETVAL=$?
    ;;
      stop)
          $RUBY_BIN $GOD_BIN terminate
          RETVAL=$?
    ;;
      restart)
          $RUBY_BIN $GOD_BIN terminate
          $RUBY_BIN $GOD_BIN -c $CONF_DIR/master.conf
          RETVAL=$?
    ;;
      status)
          $RUBY_BIN $GOD_BIN status
          RETVAL=$?
    ;;
      *)
          echo "Usage: god {start|stop|restart|status}"
          exit 1
    ;;
  esac
  
  exit $RETVAL
  </code></pre>
  
  <p><code>master.conf</code> in the above contains:</p>
  
  <pre><code>load "/var/www/myapp.com/current/config/resque.god"
  </code></pre>
  
  <p><code>resque.god</code> in the above contains:</p>
  
  <pre><code>APP_ROOT  = "/var/www/myapp.com/current"
  God.log_file  = "/var/www/myapp.com/shared/log/god.log"
  
  
  God.watch do |w|
    w.name = 'resque'
    w.interval = 30.seconds
    w.dir = File.expand_path(File.join(File.dirname(__FILE__),'..'))
    w.start = "RAILS_ENV=production bundle exec rake resque:work QUEUE=*"
    w.uid = "deploy"
    w.gid = "deploy"
  
    w.start_grace = 10.seconds
    w.log = File.expand_path(File.join(File.dirname(__FILE__), '..','log','resque-worker.log'))
  
    # restart if memory gets too high
    w.transition(:up, :restart) do |on|
      on.condition(:memory_usage) do |c|
        c.above = 200.megabytes
        c.times = 2
      end
    end
  
    # determine the state on startup
    w.transition(:init, { true =&gt; :up, false =&gt; :start }) do |on|
      on.condition(:process_running) do |c|
        c.running = true
      end
    end
  
    # determine when process has finished starting
    w.transition([:start, :restart], :up) do |on|
      on.condition(:process_running) do |c|
        c.running = true
        c.interval = 5.seconds
      end
  
      # failsafe
      on.condition(:tries) do |c|
        c.times = 5
        c.transition = :start
        c.interval = 5.seconds
      end
    end
  
    # start if process is not running
    w.transition(:up, :start) do |on|
      on.condition(:process_running) do |c|
        c.running = false
      end
    end
  end
  </code></pre>
  
  <p>In <code>deploy.rb</code> I have a reload task:</p>
  
  <pre><code>task :reload_god_config do
    run "god stop resque"
    run "god load #{File.join(deploy_to, 'current', 'config', 'resque.god')}"
    run "god start resque"
  end
  </code></pre>
  
  <p>The problem is whether I deploy, or run <code>god (stop|start|restart|status) resque</code> manually, I get the error message:</p>
  
  <pre><code>The server is not available (or you do not have permissions to access it)
  </code></pre>
  
  <p>I tried installing <code>god</code> to system gems and pointing to it in <code>god-service</code>:</p>
  
  <pre><code>GOD_BIN=/usr/local/bin/god
  </code></pre>
  
  <p>but <code>god start rescue</code> gives the same error.</p>
  
  <p>However, I can start the service by doing:</p>
  
  <pre><code>sudo /etc/init.d/god-service start
  </code></pre>
  
  <p>So its probably a permissions issue, I think, probably related to the fact that the init.d service is owned by <code>root</code> and god is run from the bundle by the <code>deploy</code> user. </p>
  
  <p>What's the best way around this issue?</p>