add taskresults app to handle celery task results
This commit is contained in:
parent
9b4bef0050
commit
a336af46c2
11 changed files with 149 additions and 2 deletions
5
gnuviechadmin/taskresults/__init__.py
Normal file
5
gnuviechadmin/taskresults/__init__.py
Normal 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.
|
||||
|
||||
"""
|
12
gnuviechadmin/taskresults/admin.py
Normal file
12
gnuviechadmin/taskresults/admin.py
Normal 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)
|
0
gnuviechadmin/taskresults/management/__init__.py
Normal file
0
gnuviechadmin/taskresults/management/__init__.py
Normal file
|
@ -0,0 +1,4 @@
|
|||
"""
|
||||
This module defines management commands for the taskresults app.
|
||||
|
||||
"""
|
|
@ -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()
|
29
gnuviechadmin/taskresults/migrations/0001_initial.py
Normal file
29
gnuviechadmin/taskresults/migrations/0001_initial.py
Normal 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,),
|
||||
),
|
||||
]
|
0
gnuviechadmin/taskresults/migrations/__init__.py
Normal file
0
gnuviechadmin/taskresults/migrations/__init__.py
Normal file
47
gnuviechadmin/taskresults/models.py
Normal file
47
gnuviechadmin/taskresults/models.py
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue