Search code examples
ruby-on-railscapistranojrubysupervisordpuma

How to pass Java options to Puma daemon?


I have a Rails application running on jruby-9.0.4.0 and using Puma as the web server.

I am trying to increase the memory limit of puma; the current flags are -Xmx500m -Xss2048k. I added _JAVA_OPTIONS=-Xss4096k -Xmx2048m to my env and all the jruby processes use it like rake assets:precompile for example but the puma instance itself does not.

Part of capistrano trace

DEBUG [0aec947c] Command: cd 
/dummy/production/releases/20160707071111 && ( 
export RBENV_ROOT="/usr/local/rbenv" RBENV_VERSION="jruby-9.0.4.0" 
RAILS_ENV="production" ; /usr/local/rbenv/bin/rbenv exec bundle exec 
rake assets:precompile )

DEBUG [0aec947c]    Picked up _JAVA_OPTIONS: -Xss4096k -Xmx2048m

config/puma.rb

# Min and Max threads per worker
threads 8, 512

# Default to production
rails_env = ENV['RAILS_ENV'] || 'production'
environment rails_env

app_dir = "/dummy/#{rails_env}/current"

# Set up socket location
bind "tcp://localhost:3000"

# Logging
stdout_redirect "#{app_dir}/log/puma.stdout.log", "#{app_dir}/log/puma.stderr.log", true

# Set master PID and state locations
pidfile "#{app_dir}/pids/puma.pid"
state_path "#{app_dir}/pids/puma.state"
activate_control_app

ENV variables

JAVA_OPTS=-Xss4096k -Xmx2048m
_JAVA_OPTIONS=-Xss4096k -Xmx2048m
JAVA_HOME=/usr/lib/jvm/java-7-oracle-amd64

Output of cat /proc/<pid>/environ

RBENV_ROOT=/usr/local/rbenvprevious=NUPSTART_JOB=rcPATH=/dummy/production/shared/bundle/jruby/2.2.0/bin:/usr/local/rbenv/versions/jruby-9.0.4.0/bin:/usr/local/rbenv/libexec:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/binPWD=/dummy/production/releases/20160707133222RBENV_DIR=/dummy/production/releases/20160707133222SUPERVISOR_SERVER_URL=unix:///var/run/supervisor.sockUPSTART_EVENTS=runlevelRUNLEVEL=2NLSPATH=/usr/dt/lib/nls/msg/%L/%N.catSUPERVISOR_PROCESS_NAME=dummyprocessSUPERVISOR_ENABLED=1XFILESEARCHPATH=/usr/dt/app-defaults/%L/DtSHLVL=0UPSTART_INSTANCE=PREVLEVEL=NRBENV_VERSION=jruby-9.0.4.0RBENV_HOOK_PATH=:/usr/local/rbenv/rbenv.d:/usr/local/etc/rbenv.d:/etc/rbenv.d:/usr/lib/rbenv/hooksrunlevel=2SUPERVISOR_GROUP_NAME=dummyprocessTERM=linuxRUBYOPT=-rbundler/setupRUBYLIB=/usr/local/rbenv/versions/jruby-9.0.4.0/lib/ruby/gems/shared/gems/bundler-1.11.2/libroot

Machine is an 8-core with 24GB of RAM.

How can I let the puma instance also pick up the Java options?


Solution

  • As mentionned in this blog post, you can put the JVM options in JRUBY_OPTS environment variable.

    In your case, you could set the variable before starting Puma daemon :

    export JRUBY_OPTS="-J-Xss4096k -J-Xmx2048m"
    

    You may also try configuring the JVM directly by setting JAVA_OPTS environment variable :

    export JAVA_OPTS="-Xss4096k -Xmx2048m"
    

    Also check how the puma daemon is started

    cat /proc/<pid>/environ
    

    Your environment is not propagated to the Puma daemon. You need to find out how Puma is started. It may be as an init.d service or an upstart service.

    Update:

    It seems you can set your environment with rbenv-vars.

    Create a .rbenv-vars file in your Rails project with the following

    JAVA_OPTS='-Xss4096k -Xmx2048m'
    

    Then your environment should be propagated to the puma daemon.