add taskresults app to handle celery task results

This commit is contained in:
Jan Dittberner 2014-12-29 15:55:57 +01:00
parent 9b4bef0050
commit a336af46c2
11 changed files with 149 additions and 2 deletions

View File

@ -1,6 +1,8 @@
Changelog
=========
* :feature:`-` add new app :py:mod:`taskresults` that takes care of handling
asynchronous `Celery`_ results
* :feature:`-` add new task :py:func:`osusers.tasks.delete_ldap_group` (needs
gvaldap >= 0.2.0 on the LDAP side)
* :feature:`-` add a `customer` field to :py:class:`osusers.models.User`
@ -30,10 +32,11 @@ Changelog
* :feature:`-` full test suite for osusers
* :feature:`-` full test suite for managemails app
* :feature:`-` full test suite for domains app
* :feature:`-` `Celery <http://www.celeryproject.com/>`_ integration for ldap
synchronization
* :feature:`-` `Celery`_ integration for ldap synchronization
* :release:`0.1 <2014-05-25>`
* :feature:`-` initial model code for os users
* :feature:`-` initial model code for mail address and mailbox management
* :feature:`-` initial model code for domains
.. _Celery: http://www.celeryproject.org/

View File

@ -149,3 +149,29 @@ provides some functionality that is common to all gnuviechadmin subprojects.
.. autotask:: osusers.tasks.remove_ldap_user_from_group
.. autotask:: osusers.tasks.setup_file_mail_userdir
.. autotask:: osusers.tasks.setup_file_sftp_userdir
:py:mod:`taskresults` app
=========================
.. automodule:: taskresults
:py:mod:`admin <taskresults.admin>`
-----------------------------------
.. automodule:: taskresults.admin
:py:mod:`management.commands <taskresults.management.commands>`
---------------------------------------------------------------
.. automodule:: taskresults.management.commands
:py:mod:`fetch_taskresults <taskresult.management.commands.fetch_taskresults>`
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. automodule:: taskresults.management.commands.fetch_taskresults
:py:mod:`models <taskresults.models>`
-------------------------------------
.. automodule:: taskresults.models

View File

@ -224,6 +224,7 @@ DJANGO_APPS = (
# Apps specific for this project go here.
LOCAL_APPS = (
'taskresults',
'domains',
'osusers',
'managemails',

View File

@ -0,0 +1,5 @@
"""
This is the taskresults app that is used for storing the results from
asynchronous `Celery <http://www.celeryproject.org>`_ tasks.
"""

View File

@ -0,0 +1,12 @@
"""
This module defines the admin interface for the taskresults app.
"""
from __future__ import absolute_import
from django.contrib import admin
from .models import TaskResult
admin.site.register(TaskResult)

View File

@ -0,0 +1,4 @@
"""
This module defines management commands for the taskresults app.
"""

View File

@ -0,0 +1,20 @@
"""
This model contains the implementation of a management command to fetch the
results of all `Celery <http://www.celeryproject.org/>`_ tasks that are not
marked as finished yet.
"""
from __future__ import unicode_literals
from django.core.management.base import BaseCommand
from taskresults.models import TaskResult
class Command(BaseCommand):
help = "fetch task results"
def handle(self, *args, **options):
for taskresult in TaskResult.objects.filter(finished=False):
taskresult.fetch_result()
taskresult.save()

View File

@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
]
operations = [
migrations.CreateModel(
name='TaskResult',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('task_id', models.CharField(max_length=36, verbose_name='Task id')),
('task_name', models.CharField(max_length=64, verbose_name='Task name')),
('result', models.TextField(verbose_name='Task result')),
('finished', models.BooleanField(default=False)),
('state', models.CharField(max_length=16, verbose_name='Task state')),
],
options={
'verbose_name': 'Task result',
'verbose_name_plural': 'Task results',
},
bases=(models.Model,),
),
]

View File

@ -0,0 +1,47 @@
"""
This model defines the database models to handle Celery AsyncResults.
"""
from __future__ import unicode_literals
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext as _
from gnuviechadmin.celery import app
class TaskResultManager(models.Manager):
def create_task_result(self, asyncresult, name):
taskresult = self.create(task_id=asyncresult.id, task_name=name)
return taskresult
@python_2_unicode_compatible
class TaskResult(models.Model):
task_id = models.CharField(_('Task id'), max_length=36)
task_name = models.CharField(_('Task name'), max_length=64)
result = models.TextField(_('Task result'))
finished = models.BooleanField(default=False)
state = models.CharField(_('Task state'), max_length=16)
objects = TaskResultManager()
class Meta:
verbose_name = _('Task result')
verbose_name_plural = _('Task results')
def __str__(self):
return "{task_name} ({task_id}): {finished}".format(
task_name=self.task_name,
task_id=self.task_id,
finished=_('yes') if self.finished else _('no')
)
def fetch_result(self):
if not self.finished:
ar = app.AsyncResult(self.task_id)
res = ar.get(no_ack=True, timeout=1)
self.result = str(res)
self.state = ar.state
self.finished = True