首页 > 解决方案 > 使用 restart=on-failure 时,Laravel systemd 队列作业停止

问题描述

我们有一个用于队列的 Laravel SystemD 作业,例如发送通知电子邮件。

现在我们注意到,当电子邮件发送不起作用时,SystemD 作业会停止。

这就是我们的服务定义的样子:

/etc/systemd/system # cat example.com-queue.service

[Unit]
Description=Laravel queue worker

[Service]
User=www-data
Group=www-data
Restart=on-failure
ExecStart=/usr/bin/php /var/www/html/example.com/web/artisan queue:work --daemon

[Install]
WantedBy=multi-user.target

该网站https://www.freedesktop.org/software/systemd/man/systemd.service.html#Restart=指出,失败时不会“成功”重新启动。

如果我们仔细观察服务状态,我们会发现 Laravel 队列实际上以“0”退出代码退出:

# systemctl status example.com-queue.service
● example.com-queue.service - Laravel queue worker
     Loaded: loaded (/etc/systemd/system/example.com-queue.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Sat 2021-08-14 15:11:18 CEST; 2 days ago
    Process: 2417820 ExecStart=/usr/bin/php /var/www/html/example.com/web/artisan queue:work --daemon (code=exited, status=0/SUCCESS)
Aug 14 15:11:08 example php[2417820]: [2021-08-14 15:11:08][16768] Processing: App\Notifications\UploadSuccess
Aug 14 15:11:18 example php[2417820]: [2021-08-14 15:11:18][16768] Failed:     App\Notifications\UploadSuccess
Aug 14 15:11:18 example systemd[1]: example.com-queue.service: Succeeded.

有一个简单的解决方法:我们可以切换到“Restart=always”,但我想阐明这个事实并找出 Laravel 的原因——如果出现错误——返回零退出代码。此外,我们担心“Restart=always”可能会导致不必要的副作用。

标签: laravelsystemctl

解决方案


推荐阅读