Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/goats_tom/api_views/dragons_reduce.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Module for DRAGONSReduce view set."""

__all__ = ["DRAGONSReduceViewSet"]
from django.db import transaction
from django.db.models import QuerySet
from dramatiq_abort import abort
from rest_framework import mixins, permissions
Expand Down Expand Up @@ -71,9 +72,13 @@ def perform_create(self, serializer: DRAGONSReduceSerializer) -> None:
reduce = serializer.save()
reduce.mark_queued()
DRAGONSProgress.create_and_send(reduce)
task_id = run_dragons_reduce.send(reduce.id, file_ids)
reduce.task_id = task_id.message_id
reduce.save()

def _enqueue() -> None:
task = run_dragons_reduce.send(reduce.id, file_ids)
reduce.task_id = task.message_id
reduce.save()

transaction.on_commit(_enqueue)

def perform_update(self, serializer: DRAGONSReduceUpdateSerializer) -> None:
"""Cancels a task.
Expand Down
14 changes: 12 additions & 2 deletions src/goats_tom/tasks/run_dragons_reduce.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@
from gempy.utils import logutils
from recipe_system.reduction.coreReduce import Reduce

from goats_tom.context.user_context import get_current_user_id
from goats_tom.logging_extensions.handlers import DRAGONSHandler
from goats_tom.models import DRAGONSFile, DRAGONSReduce
from goats_tom.realtime import DRAGONSProgress, NotificationInstance

matplotlib.use("Agg", force=True)

logger = logging.getLogger(__name__)


def _safe_literal(value: str, label: str, expected_type: type | None = None) -> Any:
"""
Expand Down Expand Up @@ -79,13 +78,24 @@ def run_dragons_reduce(reduce_id: int, file_ids: list[int]) -> None:
Raised if the DRAGONSReduce instance does not exist.
"""
try:
logger = logging.getLogger(__name__)
user_id = get_current_user_id()
logger.debug(
"Starting DRAGONS reduction with DRAGONSReduce id=%s and uid=%s",
reduce_id,
user_id,
)

reduce: DRAGONSReduce | None = None
module_name: str | None = None
# Get the reduction to run in the background.
# Generate a unique module name to avoid conflicts in sys.modules.
unique_id = uuid.uuid4()
module_name = f"dynamic_recipes_{unique_id}"

# Get the recipe instance.
reduce = DRAGONSReduce.objects.get(id=reduce_id)
logger.debug("Loaded DRAGONSReduce id=%s status=%s", reduce.id, reduce.status)

run = reduce.recipe.dragons_run
recipe = reduce.recipe
Expand Down
10 changes: 9 additions & 1 deletion tests/goats_tom/api_views/test_dragons_reduce.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,18 @@ def authenticate(self, request):
"""Helper method to authenticate requests."""
force_authenticate(request, user=self.user)

@patch("goats_tom.api_views.dragons_reduce.transaction.on_commit")
@patch("goats_tom.api_views.dragons_reduce.run_dragons_reduce.send")
def test_create_reduction(self, mock_run_dragons_reduce):
def test_create_reduction(self, mock_run_dragons_reduce, mock_on_commit):
"""Test creating a new DRAGONS reduction."""
mock_run_dragons_reduce.return_value.message_id = "12345"

# Make on_commit run the callback immediately.
def _run_now(func, using=None):
func()

mock_on_commit.side_effect = _run_now

recipe = DRAGONSRecipeFactory()
data = {
"recipe_id": recipe.id,
Expand Down