Skip to content
Advertisement

Systemd consumer service starts before broker (RabbitMQ )

I have a RabbitMQ server set up on my raspberry Pi and I want the same device to run a consumer to handle messages to one of my queues. I first tried executing it from the crontab but realized later that running it as a systemd service may be a better idea. This is my first time setting up a systemd service so I may be overseeing something trivial.

My queue_listener.service file looks like this.

[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

After creating the service file I started the service by running the following commands.

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

The service starts and I can see the consumer in the RabbitMQ Management interface.

But when I reboot my device, the service doesn’t start. Below are the contents of the syslog after reboot.

.
.
.
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.

My consumer service seems to start before the RabbitMQ broker is up. What am I doing wrong? Should I go about this some other way?

Advertisement

Answer

Replace Wants with Requires, Wants is meaning good to have while Requires is a must.

Also, make sure the rabbitmq-server.service is in rabbitmq-server.target. If not, replace rabbitmq-server.target with rabbitmq-server.service

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

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement