stlehmann/Flask-MQTT

How to configure a service to run Flask-Mqtt?

klinwer opened this issue · 4 comments

Based on this test application I try to run a similar configured application with a service. When I start the service manually from cli, flask starts without any issues and works as designed. After os boot the service stuck. OS is Raspian bullseye on a Raspberry 4, all updated to the latest versions.


[Unit]
Description=uWSGI instance to serve WebLamp
After=network.target

[Service]
User=pi
Group=www-data
WorkingDirectory=/home/pi/Desktop/WebLamp
Environment="PATH=/home/pi/Desktop/WebLamp"
ExecStart=/usr/bin/python3 WebLamp.py

[Install]
WantedBy=multi-user.target

● WebLamp.service
Loaded: loaded (/etc/systemd/system/WebLamp.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Thu 2023-02-23 18:24:40 CET; 1min 20s ago
Process: 413 ExecStart=/usr/bin/python3 WebLamp.py (code=exited, status=1/FAILURE)
Main PID: 413 (code=exited, status=1/FAILURE)
CPU: 2.360s

Feb 23 18:24:40 rasp6 python3[413]: answer = resolve(host, qfamily, False, use_network=use_network)
Feb 23 18:24:40 rasp6 python3[413]: File "/home/pi/.local/lib/python3.9/site-packages/eventlet/support/greendns.py", >
Feb 23 18:24:40 rasp6 python3[413]: raise EAI_NODATA_ERROR
Feb 23 18:24:40 rasp6 python3[413]: socket.gaierror: [Errno -2] Name or service not known
Feb 23 18:24:40 rasp6 systemd[1]: WebLamp.service: Main process exited, code=exited, status=1/FAILURE
Feb 23 18:24:40 rasp6 systemd[1]: WebLamp.service: Failed with result 'exit-code'.
Feb 23 18:24:40 rasp6 systemd[1]: WebLamp.service: Consumed 2.360s CPU time.
Feb 23 18:26:01 rasp6 systemd[1]: /etc/systemd/system/WebLamp.service:1: Assignment outside of section. Ignoring.
Feb 23 18:26:01 rasp6 systemd[1]: /etc/systemd/system/WebLamp.service:2: Assignment outside of section. Ignoring.
Feb 23 18:26:01 rasp6 systemd[1]: /etc/systemd/system/WebLamp.service:3: Assignment outside of section. Ignoring.
lines 1-17/17 (END)

Hello. Can you maybe provide more information or screenshots of the problems. Maybe explain the workflow a bit. Apologies, I am a bit new to this. Maybe I can look into it :)

Thanks for your fast response. Let me give you more details. My application is structured like this small publish/subscribe MQTT client. I am using a mosquitto broker version 2.0.11 on a different Raspberry Pi.
I have no issues to start my application as user pi from cli with "/usr/bin/python3 /home/pi/Desktop/WebLamp.py" This shows the logfile also.

pi@rasp6:~ $ cat Desktop/WebLamp/WebLamp.log
2023.02.23 21:17:44 INFO engineio.server MainThread : Server initialized for eventlet.
2023.02.23 21:17:44 INFO root MainThread : activate_server: rasp6 activated True
2023.02.23 21:17:44 INFO root MainThread : get_weblamp_name: rasp6
2023.02.23 21:17:44 INFO root MainThread : init_WebLamp: done
2023.02.23 21:17:44 INFO root Thread-1 : handle_logging: 16 :Received CONNACK (0, 0)
2023.02.23 21:17:44 INFO root Thread-1 : handle_logging: 16 :Sending SUBSCRIBE (d0, m1) [(b'WebLamp/cmd', 1)]
2023.02.23 21:17:44 INFO root Thread-1 : handle_logging: 16 :Sending SUBSCRIBE (d0, m2) [(b'WebLamp/cntr', 1)]
2023.02.23 21:17:44 INFO root Thread-1 : handle_connect: subscribed to [('WebLamp/cmd', 1), ('WebLamp/cntr', 1)]
2023.02.23 21:17:44 INFO root Thread-1 : handle_logging: 16 :Received SUBACK
2023.02.23 21:17:44 INFO root Thread-1 : handle_logging: 16 :Received SUBACK
2023.02.23 21:17:46 INFO engineio.server GreenThread-1 : -CMjS7qpFVfXkg1eAAAA: Sending packet OPEN data {'sid': '-CMjS7qpFVfXkg1eAAAA', 'upgrades': ['websocket'], 'pingTimeout': 20000, 'pingInterval': 25000}
2023.02.23 21:17:46 INFO engineio.server GreenThread-1 : -CMjS7qpFVfXkg1eAAAA: Received packet MESSAGE data 0/test,
2023.02.23 21:17:46 INFO root GreenThread-1 : websocket_connect: connected
2023.02.23 21:17:46 INFO engineio.server GreenThread-1 : -CMjS7qpFVfXkg1eAAAA: Sending packet MESSAGE data 0/test,{"sid":"Kc4D_XoWILRo5F9LAAAB"}
2023.02.23 21:17:47 INFO engineio.server GreenThread-2 : -CMjS7qpFVfXkg1eAAAA: Received request to upgrade to websocket
2023.02.23 21:17:47 INFO engineio.server GreenThread-2 : -CMjS7qpFVfXkg1eAAAA: Upgrade to websocket successful
2023.02.23 21:17:49 INFO root Thread-1 : handle_logging: 16 :Received PUBLISH (d0, q1, r0, m1), 'WebLamp/cmd', ... (16 bytes)
2

As you can see flask-mqtt is connecting and subscribing the broker. The same is valid when I start the service from cli.

After a reboot the service stuck, no logfile is available and the service status shows this:

● WebLamp.service
Loaded: loaded (/etc/systemd/system/WebLamp.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Thu 2023-02-23 21:24:57 CET; 57s ago
Process: 413 ExecStart=/usr/bin/python3 WebLamp.py (code=exited, status=1/FAILURE)
Main PID: 413 (code=exited, status=1/FAILURE)
CPU: 2.336s

Feb 23 21:24:57 rasp6 python3[413]: answer = resolve(host, qfamily, False, use_network=use_network)
Feb 23 21:24:57 rasp6 python3[413]: File "/home/pi/.local/lib/python3.9/site-packages/eventlet/support/greendns.py", line 466, in resolve
Feb 23 21:24:57 rasp6 python3[413]: raise EAI_NODATA_ERROR
Feb 23 21:24:57 rasp6 python3[413]: socket.gaierror: [Errno -2] Name or service not known
Feb 23 21:24:57 rasp6 systemd[1]: WebLamp.service: Main process exited, code=exited, status=1/FAILURE
Feb 23 21:24:57 rasp6 systemd[1]: WebLamp.service: Failed with result 'exit-code'.
Feb 23 21:24:57 rasp6 systemd[1]: WebLamp.service: Consumed 2.336s CPU time.
Feb 23 21:25:55 rasp6 systemd[1]: /etc/systemd/system/WebLamp.service:1: Assignment outside of section. Ignoring.
Feb 23 21:25:55 rasp6 systemd[1]: /etc/systemd/system/WebLamp.service:2: Assignment outside of section. Ignoring.
Feb 23 21:25:55 rasp6 systemd[1]: /etc/systemd/system/WebLamp.service:3: Assignment outside of section. Ignoring.

Starting this service manually with "sudo service WebLamp start", everything is working fine again. I tested a lot of service configuartions without success, tested permissions, groups and targets. I also tried to catch the socket.gaierror with wireshark to see if it is a timing issue.

Update. I get the same issue when I try to start the example app from Flask-MQTT-master. The service file looks like:

[Unit]
Description=uWSGI instance to serve WebLamp
After=network.target

[Service]
User=pi
Group=www-data
WorkingDirectory=/home/pi/Desktop/Flask-MQTT-master
ExecStart=/bin/python app.py

[Install]
WantedBy=multi-user.target

app.service - uWSGI instance to serve WebLamp
Loaded: loaded (/etc/systemd/system/app.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Fri 2023-02-24 06:44:16 CET; 54s ago
Process: 533 ExecStart=/bin/python app.py (code=exited, status=1/FAILURE)
Main PID: 533 (code=exited, status=1/FAILURE)
CPU: 2.073s

Feb 24 06:44:16 rasp6 python[533]: File "/home/pi/.local/lib/python3.9/site-packages/eventlet/support/greendns.py", line 466, in resolve
Feb 24 06:44:16 rasp6 python[533]: raise EAI_NODATA_ERROR
Feb 24 06:44:16 rasp6 python[533]: File "/home/pi/.local/lib/python3.9/site-packages/eventlet/support/greendns.py", line 511, in _getaddrinfo_lookup
Feb 24 06:44:16 rasp6 python[533]: answer = resolve(host, qfamily, False, use_network=use_network)
Feb 24 06:44:16 rasp6 python[533]: File "/home/pi/.local/lib/python3.9/site-packages/eventlet/support/greendns.py", line 466, in resolve
Feb 24 06:44:16 rasp6 python[533]: raise EAI_NODATA_ERROR
Feb 24 06:44:16 rasp6 python[533]: socket.gaierror: [Errno -2] Name or service not known
Feb 24 06:44:16 rasp6 systemd[1]: app.service: Main process exited, code=exited, status=1/FAILURE
Feb 24 06:44:16 rasp6 systemd[1]: app.service: Failed with result 'exit-code'.
Feb 24 06:44:16 rasp6 systemd[1]: app.service: Consumed 2.073s CPU time.

After a "sudo service app start" I get:

app.service - uWSGI instance to serve WebLamp
Loaded: loaded (/etc/systemd/system/app.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2023-02-24 06:55:39 CET; 5s ago
Main PID: 1773 (python)
Tasks: 1 (limit: 4915)
CPU: 1.256s
CGroup: /system.slice/app.service
└─1773 /bin/python app.py

the app is working as expected.

Hi, just as an FYI, the problem is solved. It takes about 10 seconds to get a valid name resolution via DNS.

The updated service file looks like:

[Unit]
Description=uWSGI instance to serve WebLamp
After=network.target

[Service]
User=pi
Group=www-data
ExecStartPre=-/bin/sleep 15
WorkingDirectory=/home/pi/Desktop/WebLamp
Environment="PATH=/home/pi/Desktop/WebLamp"
ExecStart=/usr/bin/python3 WebLamp.py

[Install]
WantedBy=multi-user.target