title: Error deploying rails app on puma in Heroku
body: <p>I am trying to deploy a rails app running on puma and jruby. the Procfile is as follows</p>
  
  <pre><code>web: bundle exec puma -C config/puma.rb -p $PORT -e $RACK_ENV
  </code></pre>
  
  <p>configuration for Puma placed in </p>
  
  <blockquote>
    <p>config/puma.rb</p>
  </blockquote>
  
  <pre><code>if ENV['RACK_ENV'] != 'production' || ENV['RAILS_ENV'] != 'production'
    workers Integer(ENV['PUMA_WORKERS'] || 4)
  end
  
  threads Integer(ENV['MIN_THREADS']  || 1), Integer(ENV['MAX_THREADS'] || 4)
  
  rackup      DefaultRackup
  port ENV['PORT'] || 3000
  environment ENV['RACK_ENV'] || 'development'
  preload_app!
  
  on_worker_boot do
    # worker specific setup
    ActiveSupport.on_load(:active_record) do
      config = ActiveRecord::Base.configurations[Rails.env] ||
                  Rails.application.config.database_configuration[Rails.env]
      config['pool'] = ENV['MAX_THREADS'] || 16
      ActiveRecord::Base.establish_connection(config)
    end
  end
  </code></pre>
  
  <p>the reason I have a conditional initialisation for workers is because Heroku complains (or may be Puma) worker mode is not available for JRuby and Windows; anyway, </p>
  
  <p>The strange behaviour I notice on heroku is that (unlike) my local environment a clustered Puma instance starts without any problems, and picks up two different ports to bind i.e. 3000 and 5000.</p>
  
  <p>However, on production i.e. Heroku this causes a crash</p>
  
  <pre><code>2014-06-23T08:50:54.545724+00:00 heroku[web.1]: State changed from crashed to starting
  2014-06-23T08:51:02.510184+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Djava.rmi.server.useCodebaseOnly=true
  2014-06-23T08:51:01.333763+00:00 heroku[web.1]: Starting process with command `bundle exec puma -C config/puma.rb -p 49576 -e production`
  2014-06-23T08:51:21.658663+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Djava.rmi.server.useCodebaseOnly=true
  2014-06-23T08:51:44.964380+00:00 app[web.1]: * Version 2.8.2 (jruby 1.9.3), codename: Sir Edmund Percival Hillary
  2014-06-23T08:51:44.962724+00:00 app[web.1]: The signal USR1 is in use by the JVM and will not work correctly on this platform
  2014-06-23T08:51:44.964563+00:00 app[web.1]: * Min threads: 1, max threads: 4
  2014-06-23T08:51:44.964738+00:00 app[web.1]: * Environment: production
  2014-06-23T08:51:44.963925+00:00 app[web.1]: Puma starting in single mode...
  2014-06-23T08:51:57.852471+00:00 app[web.1]: Errno::EADDRINUSE: Address already in use - bind - Address already in use
  2014-06-23T08:51:57.830580+00:00 app[web.1]: * Listening on tcp://0.0.0.0:49576
  2014-06-23T08:51:57.852474+00:00 app[web.1]:         initialize at org/jruby/ext/socket/RubyTCPServer.java:118
  2014-06-23T08:51:57.852478+00:00 app[web.1]:   add_tcp_listener at /app/vendor/bundle/jruby/1.9/gems/puma-2.8.2-java/lib/puma/binder.rb:195
  2014-06-23T08:51:57.852476+00:00 app[web.1]:                new at org/jruby/RubyIO.java:852
  2014-06-23T08:51:57.850256+00:00 app[web.1]: * Listening on tcp://0.0.0.0:49576
  2014-06-23T08:51:57.852479+00:00 app[web.1]:              parse at /app/vendor/bundle/jruby/1.9/gems/puma-2.8.2-java/lib/puma/binder.rb:96
  2014-06-23T08:51:57.852481+00:00 app[web.1]:               each at org/jruby/RubyArray.java:1613
  2014-06-23T08:51:57.852482+00:00 app[web.1]:              parse at /app/vendor/bundle/jruby/1.9/gems/puma-2.8.2-java/lib/puma/binder.rb:82
  2014-06-23T08:51:57.852484+00:00 app[web.1]:      load_and_bind at /app/vendor/bundle/jruby/1.9/gems/puma-2.8.2-java/lib/puma/runner.rb:119
  2014-06-23T08:51:57.852486+00:00 app[web.1]:                run at /app/vendor/bundle/jruby/1.9/gems/puma-2.8.2-java/lib/puma/single.rb:73
  2014-06-23T08:51:57.852487+00:00 app[web.1]:                run at /app/vendor/bundle/jruby/1.9/gems/puma-2.8.2-java/lib/puma/cli.rb:499
  2014-06-23T08:51:57.852489+00:00 app[web.1]:             (root) at /app/vendor/bundle/jruby/1.9/gems/puma-2.8.2-java/bin/puma:10
  2014-06-23T08:51:57.852491+00:00 app[web.1]:               load at org/jruby/RubyKernel.java:1099
  2014-06-23T08:51:57.852492+00:00 app[web.1]:             (root) at /app/vendor/bundle/jruby/1.9/bin/puma:23
  2014-06-23T08:51:59.347012+00:00 heroku[web.1]: State changed from starting to crashed
  2014-06-23T08:51:59.333790+00:00 heroku[web.1]: Process exited with status 1
  </code></pre>
  
  <p>This strange startup behaviour is not seen when I run it on my laptop. </p>
  
  <p>The only way I have got puma working on jruby in Heroku is through this setting</p>
  
  <pre><code>if ENV['RACK_ENV'] != 'production' || ENV['RAILS_ENV'] != 'production'
    workers Integer(ENV['PUMA_WORKERS'] || 4)
    port ENV['PORT'] || 3000
    environment ENV['RACK_ENV'] || 'development'
  end
  
  threads Integer(ENV['MIN_THREADS']  || 1), Integer(ENV['MAX_THREADS'] || 4)
  
  rackup      DefaultRackup
  preload_app!
  
  on_worker_boot do
    # worker specific setup
    ActiveSupport.on_load(:active_record) do
      config = ActiveRecord::Base.configurations[Rails.env] ||
                  Rails.application.config.database_configuration[Rails.env]
      config['pool'] = ENV['MAX_THREADS'] || 16
      ActiveRecord::Base.establish_connection(config)
    end
  end
  </code></pre>
  
  <p>i.e. if the port and environment directives are out side the if block Heroku somehow tried to run two instances on the same port, which will obviously cause a crash. </p>
  
  <p>Am I missing any configuration here or basically running in clustered mode (I even tried running on Ruby 2.0.0 instead of JRuby) is just not possible in Heroku</p>
  
  <p><strong>* Update *</strong></p>
  
  <p><strong>Procfile</strong></p>
  
  <pre><code>web: bundle exec puma -C config/puma.rb
  </code></pre>
  
  <p><strong>config/puma.rb</strong></p>
  
  <pre><code>if ENV['RACK_ENV'] != 'production' || ENV['RAILS_ENV'] != 'production'
    workers Integer(ENV['PUMA_WORKERS'] || 4)
  end
  
  threads Integer(ENV['MIN_THREADS']  || 1), Integer(ENV['MAX_THREADS'] || 16)
  
  rackup      DefaultRackup
  port ENV['PORT'] || 3000
  environment ENV['RACK_ENV'] || 'development'
  preload_app!
  
  on_worker_boot do
    # worker specific setup
    ActiveSupport.on_load(:active_record) do
      config = ActiveRecord::Base.configurations[Rails.env] ||
                  Rails.application.config.database_configuration[Rails.env]
      config['pool'] = ENV['MAX_THREADS'] || 16
      ActiveRecord::Base.establish_connection(config)
    end
  end
  </code></pre>
  
  <p>The reason for conditional block (quoted from initial problem description</p>
  
  <blockquote>
    <p>the reason I have a conditional initialisation for workers is because
    Heroku complains (or may be Puma) worker mode is not available for
    JRuby and Windows;</p>
  </blockquote>
  
  <p>And error logs (even if I remove JRUBY, the conditional worker block) and try to run on clustered mode</p>
  
  <pre><code>606 2014-06-23T07:13:42.058507+00:00 heroku[api]: Release v35 created by foo@bar.com
   607 2014-06-23T07:13:47.674708+00:00 app[web.1]: [2] Puma starting in cluster mode...
   608 2014-06-23T07:13:47.674777+00:00 app[web.1]: [2] * Version 2.8.2 (ruby 2.1.1-p76), codename: Sir Edmund Percival Hillary
   609 2014-06-23T07:13:47.674816+00:00 app[web.1]: [2] * Min threads: 1, max threads: 16
   610 2014-06-23T07:13:47.674939+00:00 app[web.1]: [2] * Preloading application
   611 2014-06-23T07:13:47.674858+00:00 app[web.1]: [2] * Environment: production
   612 2014-06-23T07:13:47.674900+00:00 app[web.1]: [2] * Process workers: 4
   613 2014-06-23T07:13:49.078330+00:00 app[web.1]: [2] * Listening on tcp://0.0.0.0:45245
   614 2014-06-23T07:13:49.078766+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/binder.rb:195:in `initialize': Address already in use - bind(2) for "0.0.0.0     " port 45245 (Errno::EADDRINUSE)
   615 2014-06-23T07:13:49.078775+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/binder.rb:195:in `add_tcp_listener'
   616 2014-06-23T07:13:49.078771+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/binder.rb:195:in `new'
   617 2014-06-23T07:13:49.078780+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/binder.rb:82:in `each'
   618 2014-06-23T07:13:49.078785+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/runner.rb:119:in `load_and_bind'
   619 2014-06-23T07:13:49.078778+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/binder.rb:96:in `block in parse'
   620 2014-06-23T07:13:49.078784+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/binder.rb:82:in `parse'
   621 2014-06-23T07:13:49.078793+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/cli.rb:499:in `run'
   622 2014-06-23T07:13:49.078789+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/cluster.rb:271:in `run'
   623 2014-06-23T07:13:49.078794+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/bin/puma:10:in `&lt;top (required)&gt;'
   624 2014-06-23T07:13:49.078798+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/bin/puma:23:in `load'
   625 2014-06-23T07:13:49.078801+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/bin/puma:23:in `&lt;main&gt;'
   626 2014-06-23T07:13:49.078565+00:00 app[web.1]: [2] * Listening on tcp://0.0.0.0:45245
   627 2014-06-23T07:13:50.359244+00:00 heroku[web.1]: Process exited with status 1
   628 2014-06-23T07:13:45.838792+00:00 heroku[web.1]: Starting process with command `bundle exec puma -C config/puma.rb -p 45245 -e production`
   629 2014-06-23T07:13:50.385427+00:00 heroku[web.1]: State changed from starting to crashed
   630 2014-06-23T07:13:50.385913+00:00 heroku[web.1]: State changed from crashed to starting
   631 2014-06-23T07:13:57.445391+00:00 app[web.1]: [2] Puma starting in cluster mode...
   632 2014-06-23T07:13:57.445417+00:00 app[web.1]: [2] * Environment: production
   633 2014-06-23T07:13:57.445413+00:00 app[web.1]: [2] * Version 2.8.2 (ruby 2.1.1-p76), codename: Sir Edmund Percival Hillary
   634 2014-06-23T07:13:57.445415+00:00 app[web.1]: [2] * Min threads: 1, max threads: 16
   635 2014-06-23T07:13:57.445419+00:00 app[web.1]: [2] * Process workers: 4
   636 2014-06-23T07:13:57.445423+00:00 app[web.1]: [2] * Preloading application
   637 2014-06-23T07:13:54.371058+00:00 heroku[web.1]: Starting process with command `bundle exec puma -C config/puma.rb -p 33243 -e production`
   638 2014-06-23T07:13:59.883490+00:00 app[web.1]: /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/binder.rb:195:in `initialize': Address already in use - bind(2) for "0.0.0.0     " port 33243 (Errno::EADDRINUSE)
   639 2014-06-23T07:13:59.882892+00:00 app[web.1]: [2] * Listening on tcp://0.0.0.0:33243
   640 2014-06-23T07:13:59.883501+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/binder.rb:96:in `block in parse'
   641 2014-06-23T07:13:59.883494+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/binder.rb:195:in `new'
   642 2014-06-23T07:13:59.883499+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/binder.rb:195:in `add_tcp_listener'
   643 2014-06-23T07:13:59.883506+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/binder.rb:82:in `parse'
   644 2014-06-23T07:13:59.883510+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/runner.rb:119:in `load_and_bind'
   645 2014-06-23T07:13:59.883503+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/binder.rb:82:in `each'
   646 2014-06-23T07:13:59.883512+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/cluster.rb:271:in `run'
   647 2014-06-23T07:13:59.883519+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/bin/puma:23:in `load'
   648 2014-06-23T07:13:59.883517+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/bin/puma:10:in `&lt;top (required)&gt;'
   649 2014-06-23T07:13:59.883514+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/gems/puma-2.8.2/lib/puma/cli.rb:499:in `run'
   650 2014-06-23T07:13:59.883525+00:00 app[web.1]:  from /app/vendor/bundle/ruby/2.1.0/bin/puma:23:in `&lt;main&gt;'
   651 2014-06-23T07:13:59.883273+00:00 app[web.1]: [2] * Listening on tcp://0.0.0.0:33243
   652 2014-06-23T07:14:01.451472+00:00 heroku[api]: Set DEV-2-PASS config vars by foo@bar.com
   653 2014-06-23T07:14:01.451540+00:00 heroku[api]: Release v36 created by foo@bar.com
   654 2014-06-23T07:14:01.624035+00:00 heroku[web.1]: State changed from starting to crashed
   655 2014-06-23T07:14:01.963688+00:00 heroku[web.1]: State changed from crashed to starting
   656 2014-06-23T07:14:01.615855+00:00 heroku[web.1]: Process exited with status 1
  </code></pre>