如何在不停机的情况下在后台自动部署 PHP 脚本 运行 的新版本?

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

我有一个用 PHP 编写的 AMQP 消费者(一个 RabbitMQ 消费者)在后台 运行 始终处于活动状态 。此脚本在多个节点中 运行 每个节点 12 次:12 个 unix 后台进程 运行ning:

php -f consumer.php &.

如果必须部署新版本的代码,目前我总是必须手动终止所有这些进程,然后在每个节点中一个接一个地重新启动它们。

  1. 有没有办法自动部署后台脚本? IE。将其放入部署管道中,然后重新加载它们,类似于使用 https://deployer.org.
  2. 有没有办法避免停机?
  3. 在这种情况下,ReactPHP 有什么帮助吗?

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:*

它甚至可以帮助我使用单个配置文件和单个命令启动任意多个进程。同样,来自 Laravel 文档:

[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

通过调用 sudo supervisorctl start laravel-worker:*,8 个后台进程将 运行,如果出错也会重新启动。

如果我只想用新发布的版本重启,我直接调用restart命令:

supervisorctl restart laravel-worker:*

我会将其作为 Deployer 任务集成到我的 CI/CD 管道中。