d3bug3r
d3bug3r

Reputation: 2586

Laravel 5 running queue

I have a setup where the email sending service is queued to redis driver on my laravel application.

But I need to run the following code on my local host php artisan queue:work --daemon where the queue will be executed.

How can I run the daemon once I pushed my code into the server? I am currently using AWS Elastic Beanstalk

Thanks!!

Upvotes: 2

Views: 1726

Answers (1)

Maryadi Poipo
Maryadi Poipo

Reputation: 1478

Thanks @davidlee for posting comment of this question... :) Finally I found a solution for running queue in elasticbeanstalk amazon. I'm using supervisord. I put the file on my laravel root as supervise.sh. Content of supervise.sh is like this : enter image description here

 #!/bin/bash
    #
    #
    # Author: Günter Grodotzki ([email protected])
    # Version: 2015-04-25
    #
    # install supervisord
    #
    # See:
    # - https://github.com/Supervisor/initscripts
    # - http://supervisord.org/

    if [ "${SUPERVISE}" == "enable" ]; then

      export HOME="/root"
      export PATH="/sbin:/bin:/usr/sbin:/usr/bin:/opt/aws/bin"

      easy_install supervisor

      cat <<'EOB' > /etc/init.d/supervisord
    #!/bin/bash
    #
# supervisord   Startup script for the Supervisor process control system
#
# Author:       Mike McGrath <[email protected]> (based off yumupdatesd)
#               Jason Koppe <[email protected]> adjusted to read sysconfig,
#                   use supervisord tools to start/stop, conditionally wait
#                   for child processes to shutdown, and startup later
#               Erwan Queffelec <[email protected]>
#                   make script LSB-compliant
#
# chkconfig:    345 83 04
# description: Supervisor is a client/server system that allows \
#   its users to monitor and control a number of processes on \
#   UNIX-like operating systems.
# processname: supervisord
# config: /etc/supervisord.conf
# config: /etc/sysconfig/supervisord
# pidfile: /var/run/supervisord.pid
#
### BEGIN INIT INFO
# Provides: supervisord
# Required-Start: $all
# Required-Stop: $all
# Short-Description: start and stop Supervisor process control system
# Description: Supervisor is a client/server system that allows
#   its users to monitor and control a number of processes on
#   UNIX-like operating systems.
### END INIT INFO
# Source function library
. /etc/rc.d/init.d/functions
# Source system settings
if [ -f /etc/sysconfig/supervisord ]; then
    . /etc/sysconfig/supervisord
fi
# Path to the supervisorctl script, server binary,
# and short-form for messages.
supervisorctl=${SUPERVISORCTL-/usr/bin/supervisorctl}
supervisord=${SUPERVISORD-/usr/bin/supervisord}
prog=supervisord
pidfile=${PIDFILE-/var/run/supervisord.pid}
lockfile=${LOCKFILE-/var/lock/subsys/supervisord}
STOP_TIMEOUT=${STOP_TIMEOUT-60}
OPTIONS="${OPTIONS--c /etc/supervisord.conf}"
RETVAL=0
start() {
    echo -n $"Starting $prog: "
    daemon --pidfile=${pidfile} $supervisord $OPTIONS
    RETVAL=$?
    echo
    if [ $RETVAL -eq 0 ]; then
        touch ${lockfile}
        $supervisorctl $OPTIONS status
    fi
    return $RETVAL
}
stop() {
    echo -n $"Stopping $prog: "
    killproc -p ${pidfile} -d ${STOP_TIMEOUT} $supervisord
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -rf ${lockfile} ${pidfile}
}
reload() {
    echo -n $"Reloading $prog: "
    LSB=1 killproc -p $pidfile $supervisord -HUP
    RETVAL=$?
    echo
    if [ $RETVAL -eq 7 ]; then
        failure $"$prog reload"
    else
        $supervisorctl $OPTIONS status
    fi
}
restart() {
    stop
    start
}
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status -p ${pidfile} $supervisord
        RETVAL=$?
        [ $RETVAL -eq 0 ] && $supervisorctl $OPTIONS status
        ;;
    restart)
        restart
        ;;
    condrestart|try-restart)
        if status -p ${pidfile} $supervisord >&/dev/null; then
          stop
          start
        fi
        ;;
    force-reload|reload)
        reload
        ;;
    *)
        echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload}"
        RETVAL=2
esac
exit $RETVAL
EOB

  chmod +x /etc/init.d/supervisord

  cat <<'EOB' > /etc/sysconfig/supervisord
# Configuration file for the supervisord service
#
# Author: Jason Koppe <[email protected]>
#             orginal work
#         Erwan Queffelec <[email protected]>
#             adjusted to new LSB-compliant init script
# make sure elasticbeanstalk PARAMS are being passed through to supervisord
. /opt/elasticbeanstalk/support/envvars
# WARNING: change these wisely! for instance, adding -d, --nodaemon
# here will lead to a very undesirable (blocking) behavior
#OPTIONS="-c /etc/supervisord.conf"
PIDFILE=/var/run/supervisord/supervisord.pid
#LOCKFILE=/var/lock/subsys/supervisord.pid
# Path to the supervisord binary
SUPERVISORD=/usr/local/bin/supervisord
# Path to the supervisorctl binary
SUPERVISORCTL=/usr/local/bin/supervisorctl
# How long should we wait before forcefully killing the supervisord process ?
#STOP_TIMEOUT=60
# Remove this if you manage number of open files in some other fashion
#ulimit -n 96000
EOB

  mkdir -p /var/run/supervisord/
  chown webapp: /var/run/supervisord/

  cat <<'EOB' > /etc/supervisord.conf
[unix_http_server]
file=/tmp/supervisor.sock
chmod=0777
[supervisord]
logfile=/var/app/support/logs/supervisord.log
logfile_maxbytes=0
logfile_backups=0
loglevel=warn
pidfile=/var/run/supervisord/supervisord.pid
nodaemon=false
nocleanup=true
user=webapp
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock
[program:laravel_queue]
command=php artisan queue:listen
directory=/var/www/html
stdout_logfile=/var/www/html/storage/logs/laravel-queue.log
logfile_maxbytes=0
logfile_backups=0
redirect_stderr=true
autostart=true
autorestart=true
startretries=86400
EOB

  # this is now a little tricky, not officially documented, so might break but it is the cleanest solution
  # first before the "flip" is done (e.g. switch between ondeck vs current) lets stop supervisord
  echo -e '#!/usr/bin/env bash\nservice supervisord stop' > /opt/elasticbeanstalk/hooks/appdeploy/enact/00_stop_supervisord.sh
  chmod +x /opt/elasticbeanstalk/hooks/appdeploy/enact/00_stop_supervisord.sh
  # then right after the webserver is reloaded, we can start supervisord again
  echo -e '#!/usr/bin/env bash\nservice supervisord start' > /opt/elasticbeanstalk/hooks/appdeploy/enact/99_z_start_supervisord.sh
  chmod +x /opt/elasticbeanstalk/hooks/appdeploy/enact/99_z_start_supervisord.sh
fi

Honestly... I don't really understand the meaning of above code ahaha... I just copied it from someone's blog... :D And then we need to a new supervise.config inside .ebextensions folder like this: enter image description here

packages:
  yum:
    python27-setuptools: []

files:
      "/usr/bin/supervise.sh" :
        mode: "000755"
        owner: root
        group: root
        content: |
          #!/bin/bash
          #
          # Author: Günter Grodotzki ([email protected])
          # Version: 2015-04-25
          #
          # install supervisord
          #
          # See:
          # - https://github.com/Supervisor/initscripts
          # - http://supervisord.org/

          if [ "${SUPERVISE}" == "enable" ]; then

            export HOME="/root"
        export PATH="/sbin:/bin:/usr/sbin:/usr/bin:/opt/aws/bin"

        easy_install supervisor

        cat <<'EOB' > /etc/init.d/supervisord
      #!/bin/bash
      #
      # supervisord   Startup script for the Supervisor process control system
      #
      # Author:       Mike McGrath <[email protected]> (based off yumupdatesd)
      #               Jason Koppe <[email protected]> adjusted to read sysconfig,
      #                   use supervisord tools to start/stop, conditionally wait
      #                   for child processes to shutdown, and startup later
      #               Erwan Queffelec <[email protected]>
      #                   make script LSB-compliant
      #
      # chkconfig:    345 83 04
      # description: Supervisor is a client/server system that allows \
      #   its users to monitor and control a number of processes on \
      #   UNIX-like operating systems.
      # processname: supervisord
      # config: /etc/supervisord.conf
      # config: /etc/sysconfig/supervisord
      # pidfile: /var/run/supervisord.pid
      #
      ### BEGIN INIT INFO
      # Provides: supervisord
      # Required-Start: $all
      # Required-Stop: $all
      # Short-Description: start and stop Supervisor process control system
      # Description: Supervisor is a client/server system that allows
      #   its users to monitor and control a number of processes on
      #   UNIX-like operating systems.
      ### END INIT INFO
      # Source function library
      . /etc/rc.d/init.d/functions
      # Source system settings
      if [ -f /etc/sysconfig/supervisord ]; then
          . /etc/sysconfig/supervisord
      fi
      # Path to the supervisorctl script, server binary,
      # and short-form for messages.
      supervisorctl=${SUPERVISORCTL-/usr/bin/supervisorctl}
      supervisord=${SUPERVISORD-/usr/bin/supervisord}
      prog=supervisord
      pidfile=${PIDFILE-/var/run/supervisord.pid}
      lockfile=${LOCKFILE-/var/lock/subsys/supervisord}
      STOP_TIMEOUT=${STOP_TIMEOUT-60}
      OPTIONS="${OPTIONS--c /etc/supervisord.conf}"
      RETVAL=0
      start() {
          echo -n $"Starting $prog: "
          daemon --pidfile=${pidfile} $supervisord $OPTIONS
          RETVAL=$?
          echo
          if [ $RETVAL -eq 0 ]; then
              touch ${lockfile}
              $supervisorctl $OPTIONS status
          fi
          return $RETVAL
      }
      stop() {
          echo -n $"Stopping $prog: "
          killproc -p ${pidfile} -d ${STOP_TIMEOUT} $supervisord
          RETVAL=$?
          echo
          [ $RETVAL -eq 0 ] && rm -rf ${lockfile} ${pidfile}
      }
      reload() {
          echo -n $"Reloading $prog: "
          LSB=1 killproc -p $pidfile $supervisord -HUP
          RETVAL=$?
          echo
          if [ $RETVAL -eq 7 ]; then
              failure $"$prog reload"
          else
              $supervisorctl $OPTIONS status
          fi
      }
      restart() {
          stop
          start
      }
      case "$1" in
          start)
              start
              ;;
          stop)
              stop
              ;;
          status)
              status -p ${pidfile} $supervisord
              RETVAL=$?
              [ $RETVAL -eq 0 ] && $supervisorctl $OPTIONS status
              ;;
          restart)
              restart
              ;;
          condrestart|try-restart)
              if status -p ${pidfile} $supervisord >&/dev/null; then
                stop
                start
              fi
              ;;
          force-reload|reload)
              reload
              ;;
          *)
              echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload}"
              RETVAL=2
      esac
      exit $RETVAL
      EOB

        chmod +x /etc/init.d/supervisord

        cat <<'EOB' > /etc/sysconfig/supervisord
      # Configuration file for the supervisord service
      #
      # Author: Jason Koppe <[email protected]>
      #             orginal work
      #         Erwan Queffelec <[email protected]>
      #             adjusted to new LSB-compliant init script
      # make sure elasticbeanstalk PARAMS are being passed through to supervisord
      . /opt/elasticbeanstalk/support/envvars
      # WARNING: change these wisely! for instance, adding -d, --nodaemon
      # here will lead to a very undesirable (blocking) behavior
      #OPTIONS="-c /etc/supervisord.conf"
      PIDFILE=/var/run/supervisord/supervisord.pid
      #LOCKFILE=/var/lock/subsys/supervisord.pid
      # Path to the supervisord binary
      SUPERVISORD=/usr/local/bin/supervisord
      # Path to the supervisorctl binary
      SUPERVISORCTL=/usr/local/bin/supervisorctl
      # How long should we wait before forcefully killing the supervisord process ?
      #STOP_TIMEOUT=60
      # Remove this if you manage number of open files in some other fashion
      #ulimit -n 96000
      EOB

        mkdir -p /var/run/supervisord/
        chown webapp: /var/run/supervisord/

        cat <<'EOB' > /etc/supervisord.conf
      [unix_http_server]
      file=/tmp/supervisor.sock
      chmod=0777
      [supervisord]
      logfile=/var/app/support/logs/supervisord.log
      logfile_maxbytes=0
      logfile_backups=0
      loglevel=warn
      pidfile=/var/run/supervisord/supervisord.pid
      nodaemon=false
      nocleanup=true
      user=webapp
      [rpcinterface:supervisor]
      supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
      [supervisorctl]
      serverurl=unix:///tmp/supervisor.sock
      [program:laravel_queue_general]
      command=php /var/www/html/artisan queue:listen --timeout=600
      directory=/var/www/html
      stdout_logfile=/var/www/html/storage/logs/laravel-queue.log
      logfile_maxbytes=0
      logfile_backups=0
      redirect_stderr=true
      autostart=true
      autorestart=true
      startretries=86400
      process_name=%(program_name)s_%(process_num)02d
      numprocs=2
      [program:laravel_queue_data]
      command=php /var/www/html/artisan queue:listen --timeout=600 --queue=https://sqs.ap-southeast-1.amazonaws.com/333973004348/data-processing-queue
      directory=/var/www/html
      stdout_logfile=/var/www/html/storage/logs/laravel-queue.log
      logfile_maxbytes=0
      logfile_backups=0
      redirect_stderr=true
      autostart=true
      autorestart=true
      startretries=86400
      process_name=%(program_name)s_%(process_num)02d
      numprocs=30
      [program:laravel_queue_notif]
      command=php /var/www/html/artisan queue:listen --timeout=600 --queue=https://sqs.ap-southeast-1.amazonaws.com/333973004348/notifications-queue
      directory=/var/www/html
      stdout_logfile=/var/www/html/storage/logs/laravel-queue.log
      logfile_maxbytes=0
      logfile_backups=0
      redirect_stderr=true
      autostart=true
      autorestart=true
      startretries=86400
      process_name=%(program_name)s_%(process_num)02d
      numprocs=2
      EOB

        # this is now a little tricky, not officially documented, so might break but it is the cleanest solution
        # first before the "flip" is done (e.g. switch between ondeck vs current) lets stop supervisord
        echo -e '#!/usr/bin/env bash\nservice supervisord stop' > /opt/elasticbeanstalk/hooks/appdeploy/enact/00_stop_supervisord.sh
        chmod +x /opt/elasticbeanstalk/hooks/appdeploy/enact/00_stop_supervisord.sh
        # then right after the webserver is reloaded, we can start supervisord again
        echo -e '#!/usr/bin/env bash\nservice supervisord start' > /opt/elasticbeanstalk/hooks/appdeploy/enact/99_z_start_supervisord.sh
        chmod +x /opt/elasticbeanstalk/hooks/appdeploy/enact/99_z_start_supervisord.sh
      fi

Sometime's the queue failed to start. So we need to run it manually by login to ec2 instance of amazon using putty or mobaextrem (this is my favorite ssh terminal). And then after login, we just need to execute this command :

sudo -i
cd /usr/bin
./supervise.sh

/opt/elasticbeanstalk/hooks/appdeploy/enact/99_z_start_supervisord.sh
./99_z_start_supervisord.sh

yaaapsss... that's all... :)

Note : for checking whether the queue is running or not, we can use these :

ps ax|grep supervise
ps aux|grep sord

Upvotes: 1

Related Questions