Skip to content

service

django_spire.celery.services.service

CeleryTaskService

Bases: BaseDjangoModelService['CeleryTask']

Source code in django_spire/contrib/constructor/constructor.py
def __init__(self, obj: Any = None):
    self._obj_type_name: str = str(next(iter(self.__class__.__annotations__.values()))).split(
        '.'
    )[-1]

    if obj is None:
        return

    self._obj_mro_type_names = [cls.__name__ for cls in obj.__class__.__mro__]

    if self._obj_type_name not in self._obj_mro_type_names:
        message = f'{self.__class__.__name__} was instantiated with obj type "{obj.__class__.__name__}" and failed as it was expecting "{self._obj_type_name}".'
        raise ConstructorError(message)

    self._obj_type: type[TypeAny] = obj.__class__

    if self._obj_type is None or self._obj_type is ...:
        message = f'{self.__class__.__name__} top class attribute must have an annotated type.'
        raise ConstructorError(message)

    self.obj: TypeAny = obj

    if ABC not in self.__class__.__bases__:
        if not self._obj_is_valid:
            message = f'{self._obj_type_name} failed to validate on {self.__class__.__name__}'
            raise ConstructorError(message)

    self.__post_init__()

obj instance-attribute

queue = CeleryTaskQueueService() class-attribute instance-attribute

update_result

Source code in django_spire/celery/services/service.py
def update_result(self, async_result: AsyncResult) -> None:
    if async_result.state == states.SUCCESS:  # This is to prevent race based mutations
        try:
            self.obj.result = async_result.get()

            date_done = async_result.date_done

            if is_naive(date_done):
                date_done_aware = make_aware(date_done, UTC)
            else:
                date_done_aware = date_done

            self.obj.completed_datetime = date_done_aware

            if self.obj.started_datetime is None:
                self.obj.started_datetime = self.obj.queued_datetime

            self.obj.state = states.SUCCESS

        except (OperationalError, DatabaseError):
            if self.obj._result_capture_attempts > 3:
                self.obj.state = states.FAILURE
            else:
                self.obj._result_capture_attempts = F('_result_capture_attempts') + 1

update_from_async_result_and_save_if_change

Source code in django_spire/celery/services/service.py
def update_from_async_result_and_save_if_change(self) -> None:
    has_changed = False

    async_result = self.obj.async_result

    new_meta = async_result.info  # This is to prevent race based mutations

    if self.obj._task_meta != new_meta:
        if async_result.ready():
            completed_meta = self.obj.meta
            completed_meta.set_completed()
            self.obj.meta = completed_meta.model_dump()
        else:
            self.obj._task_meta = new_meta

        if self.obj.started_datetime is None and self.obj.meta.started_datetime:
            self.obj.started_datetime = self.obj.started_datetime

        has_changed = True

    new_state = async_result.state  # This is to prevent race based mutations

    if self.obj.state != states.SUCCESS and new_state == states.SUCCESS:
        self.update_result(async_result)
        has_changed = True

    elif self.obj.state != new_state:
        self.obj.state = new_state
        has_changed = True

    if has_changed:
        self.obj.save()