Skip to content

Commit

Permalink
[celery#305] Added abstract models.
Browse files Browse the repository at this point in the history
`models` module replaced by a `models` package containing an `abstract`
module (for abstract models) and a `generic` module (for the default
models, previously in the `models` module)

---

Resolves celery#305

Fixes celery#314
  • Loading branch information
jose-padin authored and diegocastrum committed Aug 17, 2022
1 parent 33bd77e commit 74c1cc6
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 52 deletions.
3 changes: 3 additions & 0 deletions django_celery_results/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .generic import ChordCounter, GroupResult, TaskResult

__ALL__ = [ChordCounter, GroupResult, TaskResult]
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
"""Database models."""

import json
"""Abstract models."""

from celery import states
from celery.result import GroupResult as CeleryGroupResult
from celery.result import result_from_tuple
from django.conf import settings
from django.db import models
from django.utils.translation import gettext_lazy as _
Expand All @@ -15,8 +11,8 @@
TASK_STATE_CHOICES = sorted(zip(ALL_STATES, ALL_STATES))


class TaskResult(models.Model):
"""Task result/status."""
class AbstractTaskResult(models.Model):
"""Abstract Task result/status."""

task_id = models.CharField(
max_length=getattr(
Expand Down Expand Up @@ -94,8 +90,8 @@ class TaskResult(models.Model):
class Meta:
"""Table information."""

abstract = True
ordering = ['-date_done']

verbose_name = _('task result')
verbose_name_plural = _('task results')

Expand Down Expand Up @@ -131,49 +127,8 @@ def __str__(self):
return '<Task: {0.task_id} ({0.status})>'.format(self)


class ChordCounter(models.Model):
"""Chord synchronisation."""

group_id = models.CharField(
max_length=getattr(
settings,
"DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH",
255),
unique=True,
verbose_name=_("Group ID"),
help_text=_("Celery ID for the Chord header group"),
)
sub_tasks = models.TextField(
help_text=_(
"JSON serialized list of task result tuples. "
"use .group_result() to decode"
)
)
count = models.PositiveIntegerField(
help_text=_(
"Starts at len(chord header) and decrements after each task is "
"finished"
)
)

def group_result(self, app=None):
"""Return the :class:`celery.result.GroupResult` of self.
Arguments:
app (celery.app.base.Celery): app instance to create the
:class:`celery.result.GroupResult` with.
"""
return CeleryGroupResult(
self.group_id,
[result_from_tuple(r, app=app)
for r in json.loads(self.sub_tasks)],
app=app
)


class GroupResult(models.Model):
"""Task Group result/status."""
class AbstractGroupResult(models.Model):
"""Abstract Task Group result/status."""

group_id = models.CharField(
max_length=getattr(
Expand Down Expand Up @@ -226,8 +181,8 @@ def __str__(self):
class Meta:
"""Table information."""

abstract = True
ordering = ['-date_done']

verbose_name = _('group result')
verbose_name_plural = _('group results')

Expand Down
73 changes: 73 additions & 0 deletions django_celery_results/models/generic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""Database models."""

import json

from celery.result import GroupResult as CeleryGroupResult
from celery.result import result_from_tuple
from django.conf import settings
from django.db import models
from django.utils.translation import gettext_lazy as _

from django_celery_results.models.abstract import (
AbstractGroupResult,
AbstractTaskResult
)


class TaskResult(AbstractTaskResult):
"""Task result/status."""

class Meta(AbstractTaskResult.Meta):
"""Table information."""

abstract = False


class ChordCounter(models.Model):
"""Chord synchronisation."""

group_id = models.CharField(
max_length=getattr(
settings,
"DJANGO_CELERY_RESULTS_TASK_ID_MAX_LENGTH",
255),
unique=True,
verbose_name=_("Group ID"),
help_text=_("Celery ID for the Chord header group"),
)
sub_tasks = models.TextField(
help_text=_(
"JSON serialized list of task result tuples. "
"use .group_result() to decode"
)
)
count = models.PositiveIntegerField(
help_text=_(
"Starts at len(chord header) and decrements after each task is "
"finished"
)
)

def group_result(self, app=None):
"""Return the GroupResult of self.
Arguments:
---------
app (Celery): app instance to create the GroupResult with.
"""
return CeleryGroupResult(
self.group_id,
[result_from_tuple(r, app=app)
for r in json.loads(self.sub_tasks)],
app=app
)


class GroupResult(AbstractGroupResult):
"""Task Group result/status."""

class Meta(AbstractGroupResult.Meta):
"""Table information."""

abstract = False

0 comments on commit 74c1cc6

Please sign in to comment.