view wikked/tasks.py @ 240:68d6524a8333

Improve support for async wiki updates. The wiki can now be properly updated in the background using Celery after a page has been edited by a user. The Flask configuration is passed on to Celery, and the background updater function is only set when running the Flask application (not when running updates via `wk`, for instance).
author Ludovic Chabant <ludovic@chabant.com>
date Fri, 28 Mar 2014 22:02:59 -0700
parents 0e87dc411fe9
children fa2c3671bcd9
line wrap: on
line source

import logging
from celery import Celery
from wikked.wiki import Wiki, WikiParameters


logger = logging.getLogger(__name__)


logger.debug("Creating Celery application...")
celery_app = Celery('wikked', include=['wikked.tasks'])


class wiki_session(object):
    def __init__(self, wiki_root):
        self.wiki_root = wiki_root
        self.wiki = None

    def __enter__(self):
        params = WikiParameters(root=self.wiki_root)
        self.wiki = Wiki(params)
        self.wiki.start(False)
        return self.wiki

    def __exit__(self, type, value, traceback):
        if self.wiki.db.session:
            self.wiki.db.session.remove()
        return False


@celery_app.task
def update_wiki(wiki_root):
    with wiki_session(wiki_root) as wiki:
        wiki.updateAll()