Webserver Development

This Forum is for discussion about the development of the software to control the playing of the music in the Minster Nave and the bells in the Tower.

There will be two programs; both based on the original software but now running on two Pis.
TerryJC
Posts: 2616
Joined: 16/05/2017, 17:17

Re: Webserver Development

Post by TerryJC »

Thanks Hamish. I'll try this tomorrow.
Terry
TerryJC
Posts: 2616
Joined: 16/05/2017, 17:17

Re: Webserver Development

Post by TerryJC »

hamishmb wrote: 16/03/2021, 19:35Quick thought: does the program return once it is started?
How do I tell if it returns? All I know is that it definitely starts because there is socket traffic. By the time I'm able to see the process table in htop, there is no sign of it anymore. Nothing seems to show up in the console, although I might have missed it because it races past at bootup.
hamishmb wrote: 16/03/2021, 19:35If not, systemd may just time out the start operation. "journalcyl -xe" should help with diagnosing this.
(Should be "journalctl -xe" BTW :) ) This really doesn't tell me anything new:

Code: Select all

pi@minster-music:~ $ journalctl -xe                     
Mar 17 10:44:37 minster-music uwsgi[518]: Music Pi System Status Request Sent
Mar 17 10:48:21 minster-music systemd-timesyncd[307]: Synchronized to time server for the first time 134.0.16.1:123 (
Mar 17 10:48:30 minster-music uwsgi[518]: Music Pi System Status Received
Mar 17 10:48:30 minster-music uwsgi[518]: WSGI app 0 (mountpoint='') ready in 255 seconds on interpreter 0xa82380 pid
Mar 17 10:48:30 minster-music uwsgi[518]: *** uWSGI is running in multiple interpreter mode ***
Mar 17 10:48:30 minster-music uwsgi[518]: spawned uWSGI master process (pid: 518)
Mar 17 10:48:30 minster-music uwsgi[518]: spawned uWSGI worker 1 (pid: 630, cores: 2)
Mar 17 10:51:00 minster-music sshd[646]: Accepted password for pi from 192.168.1.21 port 52854 ssh2
Mar 17 10:51:00 minster-music sshd[646]: pam_unix(sshd:session): session opened for user pi by (uid=0)
Mar 17 10:51:00 minster-music systemd-logind[353]: New session 3 of user pi.
-- Subject: A new session 3 has been created for user pi
-- Defined-By: systemd
-- Support: https://www.debian.org/support
-- Documentation: https://www.freedesktop.org/wiki/Software/systemd/multiseat
-- 
-- A new session with the ID 3 has been created for the user pi.
-- 
-- The leading process of the session is 646.
Mar 17 10:51:00 minster-music systemd[1]: Started Session 3 of user pi.
-- Subject: A start job for unit session-3.scope has finished successfully
-- Defined-By: systemd
-- Support: https://www.debian.org/support
-- 
-- A start job for unit session-3.scope has finished successfully.
-- 
-- The job identifier is 325.
hamishmb wrote: 16/03/2021, 19:35You may just need to put an "&" at the end of the command so it returns and systemd registers that the job started.
The problem with that is that the Flask App is started by the uwsgi.ini file:

Code: Select all

pi@minster-music:~ $ cat /etc/uwsgi.ini 
[uwsgi]

chdir = /home/pi/html
module = statusmonitor:app

master = true
processes = 1
threads = 2

uid = www-data
gid = www-data

socket = /tmp/statusmonitor.sock
chmod-socket = 664
vacuum = true

die-on-term = true
I still think that this is a permissions problem because the App works if it is run in the Flask Development Server with all the code in /home/pi/. I'm wondering whether this App could be run as the pi User rather than the www-data User. The problem with that is that I think nginx is launched by the www-data User by default, so I might end up with loads of problems.

Alternately, I could move all of the software from /home/pi/ to /var/www/html/. That's where nginx puts its pages by default, so that might help. The Audio Guide and Quiz Webserver code is all located there and it works fine.

Any thoughts on this?
Terry
hamishmb
Posts: 1891
Joined: 16/05/2017, 16:41

Re: Webserver Development

Post by hamishmb »

Hi Terry,

I would suggest moving all the files to /var/www/html - files in /home'pi should be owned by the pi user anyway.

You can check whether the command returns by running it in the shell and seeing if you are immediately dropped back to the shell.
Hamish
TerryJC
Posts: 2616
Joined: 16/05/2017, 17:17

Re: Webserver Development

Post by TerryJC »

hamishmb wrote: 17/03/2021, 12:53 I would suggest moving all the files to /var/www/html - files in /home'pi should be owned by the pi user anyway.

You can check whether the command returns by running it in the shell and seeing if you are immediately dropped back to the shell.
Hamish,

If I run statusmonitor.py from a shell it works, but that isn't the way that uwsgi needs it.

I'm currently in conversation with someone on the Raspberry Pi Forums who says that the behaviour that I'm seeing is as it should be. The Flask App is only launched when a browser tries to access the page. This is done to save resources, but in this case the Flask App is terminating before minstermusic.py and statusmonitor.py have finished their conversation. I've responded to him and await his ideas.
Terry
hamishmb
Posts: 1891
Joined: 16/05/2017, 16:41

Re: Webserver Development

Post by hamishmb »

What about running whichever command starts uwsgi? The command should be visible if you find and read the uwsgi.service file at /etc/systemd/system/uwsgi.service.

At any rate, I imagine the people on the Raspberry Pi forums may be of more use than me on this particular topic.
Hamish
TerryJC
Posts: 2616
Joined: 16/05/2017, 17:17

Re: Webserver Development

Post by TerryJC »

hamishmb wrote: 17/03/2021, 13:12 What about running whichever command starts uwsgi? The command should be visible if you find and read the uwsgi.service file at /etc/systemd/system/uwsgi.service.
Here it is. uwsgi is launched and invokes the uwsgi.ini file.

Code: Select all

pi@minster-music:~/html $ cat /etc/systemd/system/uwsgi.service
[Unit]
Description=uWSGI Service
After=network.target

[Service]
User=www-data
Group=www-data
WorkingDirectory=/home/pi/html/
ExecStart=/usr/local/bin/uwsgi --ini /etc/uwsgi.ini

[Install]
WantedBy=multi-user.target
Terry
hamishmb
Posts: 1891
Joined: 16/05/2017, 16:41

Re: Webserver Development

Post by hamishmb »

So what happens if you run:

Code: Select all

sudo -u www-data /usr/local/bin/uwsgi --ini /etc/uwsgi.ini
In the shell? Are you immediately dropped back to the shell or do you see some useful output?
Hamish
TerryJC
Posts: 2616
Joined: 16/05/2017, 17:17

Re: Webserver Development

Post by TerryJC »

Here it is:

Code: Select all

pi@minster-music:~/html $ sudo -u www-data /usr/local/bin/uwsgi --ini /etc/uwsgi.ini
[uWSGI] getting INI configuration from /etc/uwsgi.ini
*** Starting uWSGI 2.0.19.1 (32bit) on [Wed Mar 17 12:42:02 2021] ***
compiled with version: 8.3.0 on 17 June 2020 09:19:11
os: Linux-5.10.11-v7+ #1399 SMP Thu Jan 28 12:06:05 GMT 2021
nodename: minster-music
machine: armv7l
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: /home/pi/html
detected binary path: /usr/local/bin/uwsgi
chdir() to /home/pi/html
your processes number limit is 6873
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /tmp/statusmonitor.sock fd 3
Python version: 3.7.3 (default, Jul 25 2020, 13:03:44)  [GCC 8.3.0]
Python main interpreter initialized at 0x33aa40
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 143968 bytes (140 KB) for 2 cores
*** Operational MODE: threaded ***
Minster Webserver Software Version0.2.1 (26/01/2021)
System startup sequence initiated.
System startup sequence initiated.
Waiting 20 seconds for the Music Player to start
Sockets._create_and_connect(): (Music Pi Client Socket): Error connecting:

Traceback (most recent call last):
  File "./Tools/sockettools.py", line 432, in _create_and_connect
    self._create_socket()
  File "./Tools/sockettools.py", line 550, in _create_socket
    self.server_socket.bind(('', self.port_number))
OSError: [Errno 98] Address already in use



Sockets._create_and_connect(): (Music Pi Client Socket): Unknown error, possibly address already in use?
Sockets._create_and_connect(): (Music Pi Client Socket): Retrying in 10 seconds...
Connection Timed Out (Music Pi Client Socket): [Errno 98] Address already in use. Retrying in 10 seconds...
Sockets._create_and_connect(): (Music Pi Client Socket): Error connecting:
That keeps repeating. The problem is that the minstermusic.py software is already running.

However, I wonder if the line 'your mercy for graceful operations on workers is 60 seconds' might be the problem.
Terry
hamishmb
Posts: 1891
Joined: 16/05/2017, 16:41

Re: Webserver Development

Post by hamishmb »

I wonder as well, it sounds like it might be killing your thread. Maybe there are some systemd options for the unit file that might help, but I'm not sure. The Pi forum or the LUG might know, though.
Hamish
TerryJC
Posts: 2616
Joined: 16/05/2017, 17:17

Re: Webserver Development

Post by TerryJC »

The guy on the Raspberry Forums said that uwsgi:
launches and kills dynamically a number of "worker" processes (these are your Flask Apps) depending on demand.

If I were you and your app doesn't exceed on number of simultaneous requests I would keep flask's own server without uwsgi or other "enhancing" middle-ware. The only reason not to is when you want to expose it to internet but even then you can have a proxy like nginx (handling TLS/SSL) to pipe certain url paths to a single localhost:port instance.
That's one thing that the purist in me was trying to avoid, but he has a point. I know it works in the Development Server (that's how Penri tested the Web interface) and it isn't relly exposed to the Internet, so pragmatically, it might be the way to go.

I have found the uwsgi docs at https://uwsgi-docs.readthedocs.io/en/latest/ and the guy has pointed me at this page of it; https://uwsgi-docs.readthedocs.io/en/la ... eaper.html. I'll do a bit of bedside reading before I give up.
Terry
Post Reply