Search code examples
phpbashrabbitmqamqpphp-deployer

How to automate the deployment of new versions of a PHP script running in the background without downtime?


I have a AMQP consumer (a RabbitMQ consumer) written in PHP always active running in the background. This script is run in multiple nodes and 12 times per node: 12 unix background processes running:

php -f consumer.php &.

If a new version of the code must be deployed, at the moment I always have to kill ALL these processes manually and launch them again one by one, in each node.

  1. Is there a way to automate the deployment of background scripts? I.e. put it in a deployment pipeline and then having them reloaded, similarly to using https://deployer.org.
  2. Is there a way to avoid downtime?
  3. Any way ReactPHP would help in this case?

Solution

  • Found the answer in the Laravel docs (the solution works for any always running background process, not just PHP and Laravel). Supervisor!

    Configuring Supervisor

    Supervisor configuration files are typically stored in the /etc/supervisor/conf.d directory. Within this directory, you may create any number of configuration files that instruct supervisor how your processes should be monitored. For example, let's create a laravel-worker.conf file that starts and monitors a queue:work process:

    Starting Supervisor

    Once the configuration file has been created, you may update the Supervisor configuration and start the processes using the following commands:

    sudo supervisorctl reread

    sudo supervisorctl update

    sudo supervisorctl start laravel-worker:*

    It will even help me starting as many processes I want with a single configuration file and single command. Again, from the Laravel docs:

    [program:laravel-worker]
    process_name=%(program_name)s_%(process_num)02d
    command=php /home/forge/app.com/artisan queue:work sqs --sleep=3 --tries=3
    autostart=true
    autorestart=true
    user=forge
    numprocs=8
    redirect_stderr=true
    stdout_logfile=/home/forge/app.com/worker.log
    

    By calling sudo supervisorctl start laravel-worker:*, 8 background processes will run, which also restart in case of error.

    If I just want to restart with a new released version, I call the restart command directly:

    supervisorctl restart laravel-worker:*

    I'll just integrate this as a Deployer task in my CI/CD pipeline.