Puma¶
rc.conf¶
redmine_enable="YES"
redmine_app_root="/usr/local/www/redmine"
redmine_env="production"
redmine_max_threads=4
redmine_user="git"
Gemfile.local¶
# Gemfile.local
gem 'puma-daemon', require: false
gem 'puma'
config/puma.rb¶
# Puma can serve each request in a thread from an internal thread pool.
# The `threads` method setting takes two numbers: a minimum and maximum.
# Any libraries that use thread pools should be configured to match
# the maximum value specified for Puma. Default is set to 5 threads for minimum
# and maximum; this matches the default thread size of Active Record.
#
require 'puma/daemon'
app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}/tmp"
environment ENV.fetch("RAILS_ENV") {"production"} # development
threads_count = ENV.fetch("RAILS_MAX_THREADS") {4}
workers threads_count
threads threads_count, threads_count
preload_app!
puts "RAILS_ENV=#{ENV["RAILS_ENV"]}"
daemonize false
if ENV["RAILS_ENV"] == "production"
daemonize true
bind "unix://#{shared_dir}/sockets/puma.sock"
pidfile "#{shared_dir}/pids/puma.pid"
state_path "#{shared_dir}/puma.state"
#stdout_redirect "#{app_dir}/log/puma_stdout.log", "#{app_dir}/log/puma_stderr.log"
end
activate_control_app
on_worker_boot do |worker_index|
#File.open("tmp/sockets/puma_worker_#{worker_index}.pid", "w") { |f| f.puts Process.pid }
ActiveSupport.on_load(:active_record) do
ActiveRecord::Base.establish_connection
end
end
# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
#
#port ENV.fetch("PORT") { 3000 }
# Specifies the `environment` that Puma will run in.
#
# Specifies the number of `workers` to boot in clustered mode.
# Workers are forked webserver processes. If using threads and workers together
# the concurrency of the application would be max `threads` * `workers`.
# Workers do not work on JRuby or Windows (both of which do not support
# processes).
#
# workers ENV.fetch("WEB_CONCURRENCY") { 2 }
# Use the `preload_app!` method when specifying a `workers` number.
# This directive tells Puma to first boot the application and load code
# before forking the application. This takes advantage of Copy On Write
# process behavior so workers use less memory. If you use this option
# you need to make sure to reconnect any threads in the `on_worker_boot`
# block.
#
# preload_app!
# If you are preloading your application and using Active Record, it's
# recommended that you close any connections to the database before workers
# are forked to prevent connection leakage.
#
# before_fork do
# ActiveRecord::Base.connection_pool.disconnect! if defined?(ActiveRecord)
# end
# The code in the `on_worker_boot` will be called if you are using
# clustered mode by specifying a number of `workers`. After each worker
# process is booted, this block will be run. If you are using the `preload_app!`
# option, you will want to use this block to reconnect to any threads
# or connections that may have been created at application boot, as Ruby
# cannot share connections between processes.
#
# on_worker_boot do
# ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
# end
#
# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart
rc.sh:¶
#!/usr/bin/env bash
PATH=/usr/local/rvm/rubies/ruby-2.4.0/bin:/bin:/usr/bin:/usr/local/bin:~/bin
export PATH
#if [ -f /etc/profile.d/rvm.sh ];then
# source /etc/profile.d/rvm.sh
# rvm use ruby-2.4.0 > /dev/null 2>&1
# rvm use default ruby-2.4.0 > /dev/null 2>&1
# rvm use system ruby-2.4.0 > /dev/null 2>&1
#fi
export RAILS_ENV="production"
export RAILS_MAX_THREADS=4
APP_ROOT="/usr/local/www/redmine.git"
APP_USER="git"
DAEMON_OPTS="-C ${APP_ROOT}/config/puma.rb"
PID_PATH="${APP_ROOT}/tmp/pids"
WEB_SERVER_PID="${PID_PATH}/puma.pid"
NAME="redmine"
DESC="Redmine service"
check_pid(){
if [ -f ${WEB_SERVER_PID} ]; then
PID=$(cat ${WEB_SERVER_PID})
STATUS=$(ps aux | grep ${PID//@/} | grep -v grep | wc -l)
else
STATUS=0
PID=0
fi
}
execute() {
#sudo -u $APP_USER -H bash -c "$1"
su ${APP_USER} -c "${1}"
}
start() {
cd ${APP_ROOT}
check_pid
if [ "${PID//@/}" -ne 0 -a "${STATUS}" -ne 0 ]; then
echo "Error! ${DESC} ${NAME} is currently running!"
return 1
else
if [ $(whoami) = root ]; then
execute "bundle exec puma ${DAEMON_OPTS}"
echo "${DESC} started"
fi
fi
}
stop() {
cd ${APP_ROOT}
check_pid
if [ "${PID//@/}" -ne 0 -a "${STATUS}" -ne 0 ]; then
## Program is running, stop it.
kill -QUIT $(cat ${WEB_SERVER_PID})
test -f ${WEB_SERVER_PID} && rm "${WEB_SERVER_PID}" > /dev/null 2>&1
echo "${DESC} stopped"
return 0
else
## Program is not running, exit with error.
echo "Error! ${DESC} not started!"
exit 1
fi
}
restart() {
cd ${APP_ROOT}
check_pid
if [ "$PID" -ne 0 -a "${STATUS}" -ne 0 ]; then
echo "Restarting ${DESC}..."
kill -USR2 $(cat ${WEB_SERVER_PID})
echo "${DESC} restarted."
exit 0
else
echo "Error, ${NAME} not running!"
start
exit 1
fi
}
status() {
cd ${APP_ROOT}
check_pid
if [ "${PID}" -ne 0 -a "${STATUS}" -ne 0 ]; then
echo "${DESC} / Puma with PID ${PID//@/} is running."
exit 0
else
echo "${DESC} is not running."
exit 1
fi
}
## Check to see if we are running as root first.
## Found at http://www.cyberciti.biz/tips/shell-root-user-check-script.html
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root"
exit 1
fi
case "${1}" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload|force-reload)
echo -n "Reloading ${NAME} configuration: "
kill -HUP $(cat ${PID//@/})
echo "done."
;;
status)
status
;;
*)
echo "Usage: service redmine {start|stop|restart|reload|status}" >&2
exit 1
;;
esac
exit 0
/usr/local/etc/rc.d/redmine:¶
#!/usr/bin/env sh
# PROVIDE: redmine
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add the following line to /etc/rc.conf to enable redmine:
#
# redmine_enable="YES"
#
. /etc/rc.subr
name=redmine
rcvar=redmine_enable
load_rc_config $name
: ${redmine_enable:="NO"}
: ${redmine_env:="production"}
: ${redmine_user:="git"}
: ${redmine_max_threads:=4}
: ${redmine_app_root:="/usr/local/www/redmine"}
export REDMINE_USER="${redmine_user}"
export REDMINE_ENV="${redmine_env}"
export REDMINE_MAX_THREADS="${redmine_max_threads}"
command="${redmine_app_root}/bin/redmine.sh"
extra_commands="status reload"
start_precmd="redmine_prestart"
start_cmd="redmine_start"
stop_cmd="redmine_stop"
restart_cmd="redmine_restart"
reload_cmd="redmine_reload"
status_cmd="redmine_status"
redmine_prestart(){
echo "${name} prestart"
echo "env: ${redmine_env}"
echo "app root: ${redmine_app_root}"
echo "max threads: ${redmine_max_threads}"
echo "user: ${redmine_user}"
}
redmine_start(){
echo "Starting ${name}."
${command} start
_run_rc_postcmd
}
redmine_stop(){
echo "Stoping ${name}."
${command} stop
_run_rc_postcmd
}
redmine_restart(){
echo "Restarting ${name}."
${command} restart
_run_rc_postcmd
}
redmine_reload(){
echo "Reloading ${name}."
${command} reload
_run_rc_postcmd
}
redmine_status(){
echo "Status ${name}"
${command} status
_run_rc_postcmd
}
run_rc_command "$1"