johnny
johnny

Reputation: 19755

How can I make Flask and Apache (mod_wsgi) update my database queries on each visit to a page?

In my Flask application I have a def that queries a database. When I changed the file, the SQL, the results did not show up on the webpage. When I stopped and started Apache, service apache2 restart (on Debian 7), then the new query results showed up.

I am running my WSGI process in daemon mode using mod_wsgi, v. 3.3, Apache 2.2.

I am not using SQLAlchemy or any other ORM, straight up SQL with a pymssql connect statement.

I am using Blueprints.

If I touch the .wsgi file, Apache will load the results as expected.

I am not sure how Flask-Cache can help me (or any other Flask module).

WSGIDaemonProcess myapp python-path=/var/www/intranet/application/flask:/var/www/intranet/application/flask/lib/python2.7/site-packages
WSGIProcessGroup myapp
WSGIScriptAlias /myapp/var/www/intranet/intranet.wsgi

<Directory /var/www/intranet>
        WSGIApplicationGroup %{GLOBAL}
        Order allow,deny
        Allow from all
</Directory>

<Location />
        Options FollowSymLinks
        AllowOverride None
        order allow,deny
        allow from all
        AuthType Basic
        AuthName "Subversion Repository"
        Require valid-user
        AuthUserFile /etc/apache2/dav_svn.passwd
        <IfModule mod_php4.c>
                php_flag magic_quotes_gpc Off
                php_flag track_vars On
</IfModule>

I have read much of this, https://code.google.com/p/modwsgi/wiki/ReloadingSourceCode, but I do not know if this is something Flask may already have built in for production.

How can I make a code change take effect without restarting Apache?

Edit: My query is not in the .wsgi file.

Upvotes: 0

Views: 320

Answers (1)

johnny
johnny

Reputation: 19755

What I ended up doing was use a post-receive hook in my --bare directory.

I started from here:

http://krisjordan.com/essays/setting-up-push-to-deploy-with-git

and added a touch to the end of it. Here is what I did:

#!/usr/bin/ruby
#Changed shebang a little from the website version for mine, Debian 7.
# post-receive

#johnny
require 'fileutils'
#


    # 1. Read STDIN (Format: "from_commit to_commit branch_name")
    from, to, branch = ARGF.read.split " "

    # 2. Only deploy if master branch was pushed
    if (branch =~ /master$/) == nil
        puts "Received branch #{branch}, not deploying."
        exit
    end

    # 3. Copy files to deploy directory
    deploy_to_dir = File.expand_path('../deploy')
    `GIT_WORK_TREE="#{deploy_to_dir}" git checkout -f master`
    puts "DEPLOY: master(#{to}) copied to '#{deploy_to_dir}'"

    # 4.TODO: Deployment Tasks
    # i.e.: Run Puppet Apply, Restart Daemons, etc

    #johnny
    FileUtils.touch('/path/to/my/file.wsgi')

I commit:

git commit -a -m'my commit message'

then,

git push production master

After much reading most people do not seem to like the auto update. Where I work, they need to see things immediately. Most things are database reads or static templates, so I don't mind using the "auto" touch for this particular application.

Upvotes: 0

Related Questions