GitLabのsidekiqをプロセス監視して再起動する最低限の設定

※ この記事の内容は2013年8月頃?にメモしたものです

GitLabを動かしていると、たまにsidekiqだけ落ちていて post_receive hookとかがちゃんと動かなくなるときが結構あったんで、 監視することにした

muninにもプロセス監視用スクリプトはあるみたいだけど、 監視方法が ps -C しかないらしく sidekiq だとうまくいかない

なんとなくmonitが簡単そうだったので、 こちらを参考にプロセス監視して、落ちたら再起動するようにする

http://shanon-tech.blogspot.jp/2011/11/15monit.html

monitをインストール

yum install monit -y

設定

vim /etc/monit.conf

アラートメールの送信先とアラート送信に使うメールサーバを設定

メールサーバは複数用意した方が安全だけど今回はそんな重要でもないのでひとつ

# recipients for alerts
set alert moqada+monit.gitlab@example.com
# settings mailserver
set mailserver localhost
# settings management server
set httpd port 2812 and
     use address localhost  # only accept connection from localhost
     allow localhost        # allow localhost to connect to the server and

httpdの設定は必須。 これを設定しておかないと、 監視状態を変更したりするための monit コマンドが利用できなくなる

とりあえず、デフォルトの設定をほぼコピペして、 localhostからのアクセスのみ許可するようにする

sidekiqの監視設定

sidekiqの起動スクリプトを書く

cd .. みたいにディレクトリ移動して云々って書いたら動かなかったのでスクリプト化した uid/gid指定でも動かせるらしいけど、rootでinitに配置しておく

vim /etc/init.d/sidekiq

gitlabの起動スクリプトにも同じような記述があるけど sidekiqのみじゃなくてunicornとか色々まじってるので sidekiqんとこだけ取り出す

APP_ROOT="/home/git/gitlab"

start() {
        cd $APP_ROOT
        sudo -u git -H bash -l -c "bundle exec rake sidekiq:start RAILS_ENV=production"
}

stop() {
        cd $APP_ROOT
        sudo -u git -H bash -l -c "bundle exec rake sidekiq:stop RAILS_ENV=production"
}

case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        *)
                echo "Usage: sidekiq {start|stop}" >&2
                exit 1
                ;;
esac
exit 0

monitの設定ファイルを書く

vim /etc/monit.d/sidekiq
check process sidekiq with pidfile /home/git/gitlab/tmp/pids/sidekiq.pid  # このpidfileのプロセスを監視
        every 2 cycle  # 60 x 2 秒毎に監視
        start program = "/etc/init.d/sidekiq start" with timeout 90 seconds  # 起動コマンド (90秒でtimeout)
        stop program = "/etc/init.d/sidekiq stop" with timeout 90 seconds    # 停止コマンド (90秒でtimeout)
        if 5 restarts within 5 cycles then unmonitor  # 5回再起動に失敗したら監視をやめる

5.0から with timeout XX seconds オプションが追加されたらしい これを指定しておかないとデフォルト30秒でtimeoutになってしまう これ知らなくて、何度やっても、start/stopが失敗してしまってハマった...

http://wiki.niwos.com/linux/applications/monit

※ やっぱ、timeout設定関係なく、stop/start失敗する…原因いったい何…

unmonitorになったserviceの扱い

いったんunimonitorになったサービスはほっとくと二度と監視してくれないらしい これも知らんなくてハマった…

http://d.hatena.ne.jp/hogem/20090723/1248358467

とりあえずここにあるように定期的にcrontで monit monitor all を叩いておくことに

起動

/etc/init.d/monit start
chkconfig monit on