13

Does anyone know how to make a mod_wsgi automatically reload a Flask app when any of the modules changes? I've tried WSGIScriptReloading On, but no luck. The official documentation is kind of a bear ... I guess I'll give it a stab if no one knows. Thanks in advance!

Also, if it could not permanently crash on syntax errors (like the the Flask reloader), that'd be awesome.

gatoatigrado
  • 16,580
  • 18
  • 81
  • 143

6 Answers6

20

With mod_wsgi, WSGIScriptReloading looks for changes to the .wsgi config file, rather than the code.

My workflow is to upload my code changes then just

$ touch MyWebApp.wsgi

which causes the last modified file timestamp to change and mod_wsgi to reload the code.

You can do this 'remotely' by saving the .wsgi file on your local machine and then uploading it again, or I just do it via SSH.

Not a lot you can do about syntax errors, the code is either running or it isn't, but a fix plus a touch will get it running again.

One gotcha to look out for if you're working via FTP: make sure you upload the 'touched' .wsgi file last otherwise it'll try and start with the wrong code.

MalphasWats
  • 3,255
  • 6
  • 34
  • 40
1

I think it's a very realistic situation to want to reload source code automatically in production. Think of an environment where sources are deployed per version and a 'production' symlink points to one of those versions. Whenever you want release a newer version, you just point the symlink to another path. But apache and mod_wsgi still collect the files from the symlinked directory and therefor need to have a reloading mechanism in place based on timestamps, size or w/e. Sure, one application might not be a problem, but what about hosting 15-20 applications that are all undergoing active development? Not automatically reloading sources is a pure loss in such a situation compared to restarting apache every single time.

Back to the question: if the framework you're using (in this case flask) does not have a plugin or tool in place for automatic source code reloading, then the two options described by Graham and Malphas are your best options. Either trigger the wsgi process to restart or implement a monitoring system.

1

You're right about adding the WSGIScriptReloading directive. The Flask docs don't make it 100% clear, but Apache looks for changes to your .wsgi file. The recommended solution is to just execute a touch command on your your .wsgi file, as part of your release process.

0

For production I prefer apache mod_wsgi too, while for development I use the flask's built-in server. I separate prod and dev config files and I set the debug directive True in the dev config so that Flask can detect and reload code changes automatically.

Muatik
  • 4,011
  • 10
  • 39
  • 72
  • Consider looking at using mod_wsgi-express for development. Check out my blog for recent posts about it and look for upcoming ones which talk more about using it in a development environment. – Graham Dumpleton Apr 17 '15 at 10:46
-1

The correct answer to this question is that the WSGIScriptReloading On needs to be added into 000-default.conf present under /etc/apache2/sites-enabled folder.

See below example

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerName sentiments.live
        ServerAdmin admin@sentiments.live
        DocumentRoot /var/www/html
        WSGIDaemonProcess flaskapp threads=5
        WSGIScriptAlias / /var/www/html/sentiments/flaskapp.wsgi

        <Directory sentiments>
                WSGIScriptReloading On
                WSGIProcessGroup sentiments
                WSGIApplicationGroup %{GLOBAL}
                Order deny,allow
                Allow from all
        </Directory>
Sarang
  • 641
  • 1
  • 9
  • 12
  • You do not need ``WSGIScriptReloading On`` as that is the default setting. For further information see http://modwsgi.readthedocs.io/en/develop/user-guides/reloading-source-code.html – Graham Dumpleton Mar 07 '18 at 07:43
  • You also have a number of other things wrong. The argument to ``Directory`` should be a absolute path starting with ``/`` else what it matches is uncertain and depends on global Apache setup. – Graham Dumpleton Mar 07 '18 at 07:44
  • The argument to ``WSGIProcessGroup`` also doesn't match the name you used for ``WSGIDaemonProcess``, so would be failing or matching wrong daemon process group if there was one elsewhere of that name defined. – Graham Dumpleton Mar 07 '18 at 07:45
  • Finally, you should not put your source code under the directory specified by ``DocumentRoot`` as a stuff up in other configuration could then result in your source code being able to be downloaded. Put your source code in a different directory. – Graham Dumpleton Mar 07 '18 at 07:45
-2

What do you mean 'the official documentation is kind of a bear'? What is wrong with the included recipe:

That document also explains why WSGIScriptReloading doesn't do what you expect.

And no it is not possible to permanently crash on syntax errors. It is embedded in Apache and the whole point of Apache is to keep stuff running.

Sounds like you should not be using Apache/mod_wsgi for development. Everyones knows one should not use automatic source code reloading in production so can't imagine you would want to do that.

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
  • 1
    I tend to think 'the official documentation is kind of a bear' just is a disclaimer against whiner comments on SO for "RTFM" first. – Prof. Falken Dec 07 '12 at 10:23
  • Oh, he means that no one wants to spend 2-3 hours reading about automatic source code reloading anymore. – John Wheeler Sep 17 '13 at 20:50
  • 1
    More that no one can be bothered reading and understanding documentation full stop these days. They expect people on SO to do the research for them and solve all their problems. What they don't realise is that they are destroying the support network that is available to help out as the original package authors can't be bothered to help users any more because the users don't want to try and help themselves first. – Graham Dumpleton Sep 18 '13 at 00:14