1

I have written a web application to control an LED strip using a Python application that runs on a uWSGI server. The front end and back end components seem to work correctly but there are issues with the program that occur when I run it with uWSGI and not when using the Flask dev webserver and I am unsure how to go about diagnosing them. The two parts of my code that wsgi seems to have a problem with are the python logging module (hence my difficulty diagnosing the problem), and the operation of the LED's themselves. I have listed my configuration files below and am happy to provide any other information that might be helpful.

The code isn't in a state where it can be installed easily on another system easily although all of the files involved in the development have been included in this GitHub repository. This includes the Python back end code, the HTML + CSS, and the remaining configuration files. The intended platform is Raspbian Linux running on a Raspberry Pi model 3A+.

/etc/lights/lights.ini (uWSGI)

[uwsgi]
module = lights:app

chdir = /var/lights/
logto = /var/log/lights/lights.log
master = true
processes = 5
enable-threads = true
threads = 10

socket = lights.sock
chmod-socket = 666
vacuum = true

die-on-term = true

plugin = /usr/lib/uwsgi/plugins/python3_plugin.so

/etc/nginx/sites-enabled/lights (nginx)

server {
    listen 80;
    server_name 192.168.1.79;
    
    access_log /var/log/nginx/lights_access.log;
    error_log /var/log/nginx/lights_error.log;

    root /var/lights/;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:///var/lights/lights.sock;
    }
}
jess
  • 222
  • 2
  • 18
  • Are you seeing error codes on the uWsgi server? If not [This answer is one I have used in the past](https://stackoverflow.com/questions/10364854/flask-debug-true-does-not-work-when-going-through-uwsgi) to debug live server applications. Essentially it lets you treat the server like the flask app and debug in "production," but you need to make sure to remove it once you are done debugging. – Edeki Okoh Jan 13 '20 at 20:09
  • I'm not seeing any error codes on the uWSGI server and nothing pops up in the log but I will try using that option anyway and see if I can find anything helpful, thank you – jess Jan 13 '20 at 20:13
  • anything on `/var/log/lights/lights.log`? have you tried running uWSGI without service, e.g. `systemctl stop lights.service` then `/usr/bin/uwsgi --ini /etc/lights/lights.ini`? running in CLI without `systemd.service` and `daemonize` config option should run uWSGI output to stdout. About your problems though, I suspect permission problems, but more information needed. – mforsetti Jul 15 '20 at 18:01
  • @mforsetti I'd have to reassemble the project to check as I have it packed away at the moment (I'll unpack it and check shortly), but as far as I can recall there was no output at all to `/var/log/lights/lights.log` and I don't seem to remember there being any effect pm the problem when I ran it from the CLI or with higher privs – jess Jul 15 '20 at 18:21

1 Answers1

1
def main():
    logging.basicConfig(level=logging.INFO, format=logFormat)
    application = app
    app.run()

if __name__ == "__main__":
    main()

The logging issue seems to be here. Logging is only set up if the program is executed as the main Python script, but not for Python imports. UWSGI calls this function using the Python C API, so the main method will never be executed.

This also explains why it works with the Flask development server, as you execute as a Python script instead of importing the module.

To fix it, you can just move logging setup to the main module level and you should be good.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Uku Loskit
  • 40,868
  • 9
  • 92
  • 93
  • This is perfect, thank you very much. I have identified the error in the log file as "ws2811_init failed with code -13 (Unable to initialize SPI)". I suspect this is a permissions issue as it looks like the same error as when I try and run the script directly with Python without elevated permissions. Does uwsgi drop privileges internally even when run as root? If so do you know how I might configure uwsgi to run my script with the correct privileges? – jess Jul 15 '20 at 23:13
  • 1
    `Does uwsgi drop privileges internally even when run as root?` that will depend on the config, but most likely, if you have installed uwsgi via your distribution. easiest way to verify is to run `ps aux | grep uwsgi`. most likely it's www-data. ps aux will probably reveal extra config paths as well if they are used. you can use the `uid` and `gid` settings to override this the privileges behavior. – Uku Loskit Jul 15 '20 at 23:33