# Copyright © The Debusine Developers
# See the AUTHORS file at the top-level directory of this distribution
#
# This file is part of Debusine. It is subject to the license terms
# in the LICENSE file found in the top-level directory of this
# distribution. No part of Debusine, including this file, may be copied,
# modified, propagated, or distributed except according to the terms
# contained in the LICENSE file.

"""Base support for tasks that run on signing workers."""

from abc import ABCMeta
from typing import override

from django.db import close_old_connections, transaction

from debusine.artifacts.models import TaskTypes, WorkRequestResults
from debusine.tasks import BaseExternalTask
from debusine.tasks.models import BaseDynamicTaskData, BaseTaskData


class BaseSigningTask[
    TD: BaseTaskData,
    DTD: BaseDynamicTaskData = BaseDynamicTaskData,
](BaseExternalTask[TD, DTD], metaclass=ABCMeta):
    """Base class for tasks that run on signing workers."""

    TASK_TYPE = TaskTypes.SIGNING

    @override
    def execute(self) -> WorkRequestResults:
        """
        Execute task in a transaction.

        Close database connections before and after running the task to
        avoid problems with long-running worker threads.
        """
        close_old_connections()
        try:
            with transaction.atomic():
                return super().execute()
        finally:
            close_old_connections()
