如何使用init.d运行多个sidekiq worker

时间:2018-11-20 14:00:00

标签: ruby-on-rails bash sidekiq init.d

我正在使用Amazon Linux1。同一EC2服务器中有2个应用程序。 1是rails 3,另一个是rails5。我正在尝试使用init.d从两个应用程序中运行sidekiq,但其中只有一个可以运行。

下面是我的脚本在/etc/init.d/sidekiq_myapp中的示例。我只是更改了其他脚本的APP_DIR,并为sidekiq(-q参数)使用了不同的名称:

#!/bin/sh

### BEGIN INIT INFO
# Provides:          sidekiq
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts and stops sidekiq
# Description:       starts and stops sidekiq
### END INIT INFO

# You will need to modify these
APP="myapp"
AS_USER="root"
APP_DIR="/var/www/myapp"

APP_CONFIG="${APP_DIR}/config"
LOG_FILE="/opt/nginx/logs/error.log"
LOCK_FILE="$APP_DIR/tmp/${APP}-sidekiq-lock"
PID_FILE="$APP_DIR/tmp/pids/sidekiq.pid"
GEMFILE="$APP_DIR/Gemfile"
SIDEKIQ="sidekiq"
APP_ENV="production"
BUNDLE="bundle"

START_CMD="$BUNDLE exec $SIDEKIQ -q critical,5 -q default,2 -q low -d -L $LOG_FILE -e $APP_ENV -P $PID_FILE"
CMD="cd ${APP_DIR}; ${START_CMD} >> ${LOG_FILE} 2>&1 &"

RETVAL=0


start() {

  status
  if [ $? -eq 1 ]; then

    [ `id -u` == '0' ] || (echo "$SIDEKIQ runs as root only .."; exit 5)
    [ -d $APP_DIR ] || (echo "$APP_DIR not found!.. Exiting"; exit 6)
    cd $APP_DIR
    echo "Starting $SIDEKIQ message processor .. "

    su -c "$CMD" - $AS_USER

    RETVAL=$?
    #Sleeping for 8 seconds for process to be precisely visible in process table - See status ()
    sleep 8
    [ $RETVAL -eq 0 ] && touch $LOCK_FILE
    return $RETVAL
  else
    echo "$SIDEKIQ message processor is already running .. "
  fi


}

stop() {

  status
  if [ $? -eq 0 ]; then
    echo "Stopping sidekiq message processor .."
    SIG="INT"
    kill -$SIG `cat $PID_FILE`
    RETVAL=$?
    [ $RETVAL -eq 0 ] && rm -f $LOCK_FILE
    return $RETVAL
  else
    echo "Sidekiq message processor is stopped already .."
  fi

}

status() {

  ps -ef | grep 'sidekiq [0-9]*.[0-9]*.[0-9]*' | grep -v grep
  return $?
}


case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status

        if [ $? -eq 0 ]; then
             echo "$SIDEKIQ message processor is running .."
             RETVAL=0
         else
             echo "$SIDEKIQ message processor is stopped .."
             RETVAL=1
         fi
        ;;
    *)
        echo "Usage: $0 {start|stop|status}"
        exit 0
        ;;
esac
exit $RETVAL

我可以验证导轨3上的那个正在运行。轨道5的那个没有。我仍然必须在rails 5上手动运行bundle exec sidekiq才能使其正常工作。

# ps -ef | grep sidekiq
2672     1  1 13:37 ?        00:00:09 sidekiq 3.3.0 squadzip [0 of 25 busy]                                                                                                                                                    
7048  6954  0 13:51 pts/1    00:00:00 grep --color=auto sidekiq

我想坚持使用systemV。有什么想法吗?如果我将手动运行bundle exec sidekiq,则两者都将运行,这意味着我的rails / sidekiq设置很好。我的问题是如何同时启动它们。

0 个答案:

没有答案