Systemd 消费者服务在代理 (RabbitMQ) 之前启动

Systemd consumer service starts before broker (RabbitMQ )

我在我的 raspberry Pi 上设置了一个 RabbitMQ 服务器,我希望同一设备 运行 消费者处理发送到我的一个队列的消息。我首先尝试从 crontab 执行它,但后来意识到 运行 将其作为系统服务可能是一个更好的主意。这是我第一次设置 systemd 服务,所以我可能会监督一些微不足道的事情。

我的 queue_listener.service 文件如下所示。

[Unit]
Description= queue_listener
Wants=rabbitmq-server.target
After=rabbitmq-server.target

[Service]
Type=simple
Restart=always
WorkingDirectory=<path>
User=<user>
ExecStart=python3 <path>/queue_listener.py

[Install]
WantedBy=multi-user.target

创建服务文件后,我通过运行以下命令启动了服务。

sudo systemctl daemon-reload
sudo systemctl enable queue_listener.service 
sudo systemctl start queue_listener.service 

服务启动,在RabbitMQ管理界面可以看到消费者

但是当我重新启动我的设备时,服务没有启动。以下是重启后系统日志的内容。

.
.
.
13:51:15 pi-server systemd[1]: Started queue_listener.
13:51:16 pi-server python3[1035]: Traceback (most recent call last):
13:51:16 pi-server python3[1035]:   File "/home/pi/Projects/Pi-Project/Pi-Server/queue_listener.py", line 45, in <module>
13:51:16 pi-server python3[1035]:     mq.consume("measurements",write_measurement_to_db)
13:51:16 pi-server python3[1035]:   File "/home/pi/Projects/Pi-Project/Pi-Server/MQ_handler.py", line 25, in consume
13:51:16 pi-server python3[1035]:     connection = pika.BlockingConnection(parameters)
13:51:16 pi-server python3[1035]:   File "/home/pi/.local/lib/python3.7/site-packages/pika/adapters/blocking_connection.py", line 360, in __init__
13:51:16 pi-server python3[1035]:     self._impl = self._create_connection(parameters, _impl_class)
13:51:16 pi-server python3[1035]:   File "/home/pi/.local/lib/python3.7/site-packages/pika/adapters/blocking_connection.py", line 451, in _create_connection
13:51:16 pi-server python3[1035]:     raise self._reap_last_connection_workflow_error(error)
13:51:16 pi-server python3[1035]: pika.exceptions.AMQPConnectionError
13:51:16 pi-server systemd[1]: queue_listener.service: Main process exited, code=exited, status=1/FAILURE
13:51:16 pi-server systemd[1]: queue_listener.service: Failed with result 'exit-code'.
13:51:16 pi-server systemd[1]: queue_listener.service: Service RestartSec=100ms expired, scheduling restart.
13:51:16 pi-server systemd[1]: queue_listener.service: Scheduled restart job, restart counter is at 10.
13:51:16 pi-server systemd[1]: Stopped queue_listener.
13:51:16 pi-server systemd[1]: queue_listener.service: Start request repeated too quickly.
13:51:16 pi-server systemd[1]: queue_listener.service: Failed with result 'exit-code'.
13:51:16 pi-server systemd[1]: Failed to start queue_listener.
.
.
.
13:51:37 pi-server rabbitmq-server[478]:   ##  ##      RabbitMQ 3.8.7
13:51:37 pi-server rabbitmq-server[478]:   ##  ##
13:51:37 pi-server rabbitmq-server[478]:   ##########  Copyright (c) 2007-2020 VMware, Inc. or its affiliates.
13:51:37 pi-server rabbitmq-server[478]:   ######  ##
13:51:37 pi-server rabbitmq-server[478]:   ##########  Licensed under the MPL 2.0. Website: https://rabbitmq.com
13:51:37 pi-server rabbitmq-server[478]:   Doc guides: https://rabbitmq.com/documentation.html
13:51:37 pi-server rabbitmq-server[478]:   Support:    https://rabbitmq.com/contact.html
13:51:37 pi-server rabbitmq-server[478]:   Tutorials:  https://rabbitmq.com/getstarted.html
13:51:37 pi-server rabbitmq-server[478]:   Monitoring: https://rabbitmq.com/monitoring.html
13:51:37 pi-server rabbitmq-server[478]:   Logs: /var/log/rabbitmq/rabbit@pi-server.log
13:51:37 pi-server rabbitmq-server[478]:         /var/log/rabbitmq/rabbit@pi-server_upgrade.log
13:51:37 pi-server rabbitmq-server[478]:   Config file(s): (none)
13:51:41 pi-server rabbitmq-server[478]:   Starting broker... completed with 3 plugins.
13:51:41 pi-server systemd[1]: Started RabbitMQ broker.

我的消费者服务似乎在 RabbitMQ 代理启动之前启动。 我究竟做错了什么?我应该用其他方式解决这个问题吗?

Wants替换为RequiresWants是好东西,Requires是必须的。

此外,请确保 rabbitmq-server.service 在 rabbitmq-server.target 中。如果不是,请将 rabbitmq-server.target 替换为 rabbitmq-server.service

参考:https://www.freedesktop.org/software/systemd/man/systemd.unit.html