我有一个runit服务,用于使用rails运行unicorn应用程序。
它的restart命令使用一个信号(USR2)来处理零停机重启。基本上,它要等到新流程准备好后,旧流程才会消亡。
这会导致很长(40秒)的重新启动时间,其中service myservice restart直到结束才返回。
虽然我可以给runit更长的超时时间(我已经这样做了),但我想让这个重启成为一个火,并忘记某种操作,这样它会立即返回(或在USR2信号被触发后,但不会等待它完成)。
整个逻辑取自多篇关于零停机rails部署和unicorn重启的博客文章:
https://gist.github.com/czarneckid/4639793
https://gist.github.com/JeanMertz/8996796
https://nulogy.com/who-we-are/company-blog/articles/zero-downtime-deployments-with-chef-nginx-and-unicorn/
这是runit脚本(由chef生成):

#!/bin/bash
#
# This file is managed by Chef, using the <%= node.name %> cookbook.
# Editing this file by hand is highly discouraged!
#

exec 2>&1

#
# Since unicorn creates a new pid on restart/reload, it needs a little extra
# love to manage with runit. Instead of managing unicorn directly, we simply
# trap signal calls to the service and redirect them to unicorn directly.
#


RUNIT_PID=$$
APPLICATION_NAME=<%= @options[:application_name] %>
APPLICATION_PATH=<%= File.join(@options[:path], 'current') %>
BUNDLE_CMD="<%= @options[:bundle_command] ? "#{@options[:bundle_command]} exec" : '' %>"
UNICORN_CMD=<%= @options[:unicorn_command] ? @options[:unicorn_command] : 'unicorn' %>
UNICORN_CONF=<%= @options[:unicorn_config_path] ? @options[:unicorn_config_path] : File.join(@options[:path], 'current', 'config', 'unicorn.rb') %>
RAILS_ENV=<%= @options[:rails_env] %>
CUR_PID_FILE=<%= @options['pid'] ? @options['pid'] : File.join(@options[:path], 'current', 'shared', 'pids', "#{@options[:application_name]}.pid") %>
ENV_PATH=<%= @options[:env_dir] %>
OLD_PID_FILE=$CUR_PID_FILE.oldbin

echo "Runit service restarted (PID: $RUNIT_PID)"

function is_unicorn_alive {
  set +e
  if [ -n $1 ] && kill -0 $1 >/dev/null 2>&1; then
    echo "yes"
  fi
  set -e
}

if [ -e $OLD_PID_FILE ]; then
  OLD_PID=$(cat $OLD_PID_FILE)
  echo "Old master detected (PID: $OLD_PID), waiting for it to quit"
  while [ -n "$(is_unicorn_alive $OLD_PID)" ]; do
    sleep 5
  done
fi

if [ -e $CUR_PID_FILE ]; then
  CUR_PID=$(cat $CUR_PID_FILE)
  if [ -n "$(is_unicorn_alive $CUR_PID)" ]; then
    echo "Detected running Unicorn instance (PID: $CUR_PID)"
    RUNNING=true
  fi
fi

function start {
  unset ACTION
  if [ $RUNNING ]; then
    restart
  else
    echo 'Starting new unicorn instance'
    cd $APPLICATION_PATH

    exec chpst -e $ENV_PATH $BUNDLE_CMD $UNICORN_CMD -c $UNICORN_CONF -E $RAILS_ENV
    sleep 3
    CUR_PID=$(cat $CUR_PID_FILE)
  fi
}

function stop {
  unset ACTION
  echo 'Initializing graceful shutdown'
  kill -QUIT $CUR_PID

  while [ -n "$(is_unicorn_alive $CUR_PID)" ]; do
    echo '.'
    sleep 2
  done

  echo 'Unicorn stopped, exiting Runit process'
  kill -9 $RUNIT_PID
}

function restart {
  unset ACTION
  echo "Restart request captured, swapping old master (PID: $CUR_PID) for new master with USR2"
  kill -USR2 $CUR_PID

  sleep 2
  echo 'Restarting Runit service to capture new master PID'
  exit
}

function alarm {
  unset ACTION
  echo 'Unicorn process interrupted'
}

trap 'ACTION=stop' STOP TERM KILL
trap 'ACTION=restart' QUIT USR2 INT
trap 'ACTION=alarm' ALRM

[ $RUNNING ] || ACTION=start

if [ $ACTION ]; then
  echo "Performing \"$ACTION\" action and going into sleep mode until new signal captured"
elif [ $RUNNING ]; then
  echo "Going into sleep mode until new signal captured"
fi

if [ $ACTION ] || [ $RUNNING ]; then
  while true; do
    [ "$ACTION" == 'start' ] && start
    [ "$ACTION" == 'stop' ] && stop
    [ "$ACTION" == 'restart' ] && restart
    [ "$ACTION" == 'alarm' ] && alarm
    sleep 2
  done
fi

最佳答案

这是一种使用Runit、将重新加载逻辑移到control/h脚本并使用sv hup的超级奇怪的方法(或者因为它似乎不比发送USR2sv 2更重要)。不应涉及主运行脚本。

关于linux - Runit的服务重启如何立即返回?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37763909/

10-09 03:58