2

My goal:

-serve multiple python flask applications under the same development server

-no server side caching

-run gunicorn applications as background tasks

-nginx as reverse proxy that will resolve application instances by hostname, running all on port 80

I've realised I can run my Flask/Gunicorn application in a background using systemd - the problem is, it serves cached version of my application and I'd like to serve a new version of my build, everytime I commit new work into it while running

systemd/system/website.com.service

[Unit]
Description=Gunicorn instance to serve website.com
After=network.target

[Service]
User=melcma
Group=www-data
PIDFile=/var/tmp/gunicorn.pid
WorkingDirectory=/var/www/website.com
Environment="PATH=/var/www/website.com/env/bin"
ExecStart=/var/www/website.com/env/bin/gunicorn --workers 1 --bind unix:website.com.sock -m 007 wsgi:app
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

From what I learned, this line: "ExecReload=/bin/kill -s HUP $MAINPID" is supposed to reload my server, but how do I trigger it under files change? I've tried to add "--reload" to ExecStart command but no effect.

And bonus question:

Is using systemd for this purpose a good way or it would rather butcher the server? (running task in background that launches gunicorn instance and refreshes itself under files changes)

Melcma
  • 141
  • 1
  • 4
  • I believe it is not good idea to reload it automatically. Its much better to call ``systemctl reload website.com`` explicitly. While guicorn supports ``--reload`` key (http://docs.gunicorn.org/en/19.0/settings.html#reload) it is not recommended from production. – user996142 Jul 04 '17 at 19:55
  • pardon me, I mean for development (local) server, that's why I need instant reload on files change. – Melcma Jul 04 '17 at 20:00
  • So why do you need systemd to run local server? I mean can't you run gunicorn directly? And even with systemd you could use ``--reload`` key – user996142 Jul 04 '17 at 20:14
  • Entire server would be a clone of production server, so it should serve multiple applications as they might communicate together. Running them with --reload would be a solution, but I don't really want to run multiple terminals every time. Any better solution? – Melcma Jul 04 '17 at 20:21
  • I've tried to run it like that in systemd, but that takes no effect: ExecStart=/var/www/website.com/env/bin/gunicorn --reload --workers 1 --bind unix:website.com.sock -m 007 wsgi:app – Melcma Jul 04 '17 at 20:25
  • what is you gunicorn version? does reload work when you run gunicorn directly? Have you tried to remove pyc files? – user996142 Jul 04 '17 at 20:28
  • It's 19.7.1. I've tried to run it with gunicorn -b 127.0.0.0:5000 wsgi:app - it run's the server but doesnt reload itself under file changes. Also, what pyc files? – Melcma Jul 05 '17 at 20:38
  • ``gunicorn -b 127.0.0.0:5000 --reload wsgi:app``? ``pyc`` are bytecode-compiled files produced by cpython: https://stackoverflow.com/questions/2998215/if-python-is-interpreted-what-are-pyc-files – user996142 Jul 05 '17 at 20:43
  • I've removed __ pycache __ folder from root of my project. I run "gunicorn -b 0.0.0.0:5000 --reload wsgi:app". Just to confirm - I have my html files inside /template folder which is inside root of my project. – Melcma Jul 05 '17 at 23:13
  • Hello @Melcma, just wanted to tell you that I had the same issue -- albeit on Django; and I filed a bug for it: https://github.com/benoitc/gunicorn/issues/1562 – ahmed Aug 02 '17 at 23:49

0 Answers0