Last Updated: February 25, 2016
·
2.623K
· estum

Puma cluster and Monit: respawn workers on exceeding memory limit

In puma config:

workers 8
pidfile "/app_pids_path/puma.pid" # Cluster PID

on_worker_boot do |wid|
  File.open("/app_pids_path/puma/worker_#{wid}.pid", "w"){|f| f.puts Process.pid }
end

Puma allows to spawn missing workers by sending SIGCHLD signal to the cluster process.
Also, useful signals: TTIN — spawn one more worker, TTOU — kill the last worker.

Little shell script respawn_worker (don't forget to make it executable):
Usage: respawn_worker WORKER_SERIAL_NUMBER

#!/usr/bin/env bash

pid_file="/app_pids_path/puma.pid" # Cluster PID
pid_dir=`dirname $pid_file`

[[ -e $pid_file ]] || exit 1
[[ -z "$1" ]] && exit 1

kill `cat ${pid_dir}/puma/worker_$1.pid`
kill -s SIGCHLD `cat $pid_file`

And, finally monit config script:

check process my_app_puma_worker_0
    with pidfile /app_pids_path/puma/worker_0.pid
     if totalmem is greater than 300 MB for 2 cycles then exec "/script_dir/respawn_worker 0"

check process my_app_puma_worker_1
    with pidfile /app_pids_path/puma/worker_1.pid
    if totalmem is greater than 320 MB for 2 cycles then exec "/script_dir/respawn_worker 1"

check process my_app_puma_worker_2
    with pidfile /app_pids_path/puma/worker_2.pid
    if totalmem is greater than 340 MB for 2 cycles then exec "/script_dir/respawn_worker 2"

# ... and for each worker

1 Response
Add your response

You are shared the awesome stuff. I've just used it for my production server.

Thank you!

over 1 year ago ·