配置 Sidekiq 使用 Upstart 启动和停止,并且使用 Monit 监视

upstart 配置

/etc/init/sidekiq.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# /etc/init/sidekiq.conf - Sidekiq config

# This example config should work with Ubuntu 12.04+.  It
# allows you to manage multiple Sidekiq instances with
# Upstart, Ubuntu's native service management tool.
#
# See workers.conf for how to manage all Sidekiq instances at once.
#
# Save this config as /etc/init/sidekiq.conf then mange sidekiq with:
#   sudo start sidekiq
#   sudo stop sidekiq
#   sudo status sidekiq
#
# or use the service command:
#   sudo service sidekiq {start,stop,restart,status}
#

description "Sidekiq Background Worker"

# This starts upon bootup and stops on shutdown
start on runlevel [2345]
stop on runlevel [06]

# change to match your deployment user
setuid deploy
setgid deploy
env HOME=/home/deploy

respawn
respawn limit 3 30

# TERM is sent by sidekiqctl when stopping sidekiq. Without declaring these as
# normal exit codes, it just respawns.
normal exit 0 TERM

#instance $index

script
# this script runs in /bin/sh by default
# respawn as bash so we can source in rbenv
exec /bin/bash <<'EOT'
  # Pick your poison :) Or none if you're using a system wide installed Ruby.
  # rbenv
  # source /home/apps/.bash_profile
  # OR
  # source /home/apps/.profile
  # OR system:
  # source /etc/profile.d/rbenv.sh
  #
  # rvm
  # source /home/apps/.rvm/scripts/rvm
  # Logs out to /var/log/upstart/sidekiq.log by default

  # 服务器上的数据库用户名和密码在这个文件里面
  source /etc/environment

  # 服务器用的是 rbenv
  export HOME=/home/deploy
  export PATH="$HOME/.rbenv/bin:$PATH"
  eval "$(rbenv init -)"
  export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"

  cd /var/www/xxx_app/current
  exec bundle exec sidekiq -e production -P /var/www/xxx_app/current/tmp/pids/sidekiq.pid -L /var/www/xxx_app/current/log/sidekiq.log
EOT
end script

monit 配置

monit 使用 upstart 的配置将会非常简单

/etc/monit/conf.d/sidekiq.monitrc

1
2
3
4
5
check process sidekiq_application_production with pidfile /var/www/xxx_app/shared/tmp/pids/sidekiq.pid
  start program = "/bin/bash -c 'sudo start sidekiq'"
  stop program = "/bin/bash -c 'sudo stop sidekiq'" with timeout 90 seconds
  if totalmem is greater than 500 MB for 2 cycles then restart # eating up memory?
  group sidekiq

评论