Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 159e70f

Browse files
author
Alan Christie
committedJan 25, 2024·
fix: Adjust task status behaviour
1 parent 85101cf commit 159e70f

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed
 

‎viewer/target_loader.py

+23-25
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import math
55
import os
66
import tarfile
7-
import traceback
87
import uuid
98
from dataclasses import dataclass, field
109
from enum import Enum
@@ -153,16 +152,20 @@ def log(self, level: Level, message: str) -> None:
153152

154153
def final(self, message, upload_state=None):
155154
if upload_state:
155+
# User has provided an over-ride for the upload state.
156156
self.upload_state = upload_state
157+
elif self.upload_state == UploadState.PROCESSING:
158+
self.upload_state = UploadState.SUCCESS
159+
logger.info(message)
157160
else:
158-
if self.upload_state == UploadState.PROCESSING:
159-
self.upload_state = UploadState.SUCCESS
160-
logger.info(message)
161-
else:
162-
self.upload_state = UploadState.FAILED
163-
logger.error(message)
161+
self.upload_state = UploadState.FAILED
162+
logger.error(message)
164163

164+
# This is (expected to be) the last message for the upload.
165+
# Add the user-supplied message and then add the string representation
166+
# of the upload state.
165167
self.stack.append(UploadReportEntry(message=message))
168+
self.stack.append(UploadReportEntry(message=self.upload_state.name))
166169
self._update_task(self.json())
167170

168171
def json(self):
@@ -1175,10 +1178,6 @@ def process_bundle(self):
11751178
# this is the last file to load. if any of the files missing, don't continue
11761179
if not meta or not config or not xtalforms_yaml:
11771180
msg = "Missing files in uploaded data, aborting"
1178-
self.report.final(
1179-
msg,
1180-
Level.FATAL,
1181-
)
11821181
raise FileNotFoundError(msg)
11831182

11841183
try:
@@ -1596,22 +1595,20 @@ def load_target(
15961595
raise IntegrityError(
15971596
f"Uploading {target_loader.data_bundle} failed"
15981597
)
1599-
except IntegrityError as exc:
1600-
logger.error(exc, exc_info=True)
1601-
target_loader.report.final(f"Uploading {target_loader.data_bundle} failed")
1602-
raise IntegrityError from exc
1603-
1604-
except (FileExistsError, FileNotFoundError, StopIteration) as exc:
1605-
raise Exception from exc
1606-
16071598
except Exception as exc:
1608-
# catching and logging any other error
1609-
logger.error(exc, exc_info=True)
1610-
target_loader.report.log(Level.FATAL, traceback.format_exc())
1611-
raise Exception from exc
1599+
# Handle _any_ underlying problem.
1600+
# These are errors processing the data, which we handle gracefully.
1601+
# The task should _always_ end successfully.
1602+
# Any problem with the underlying data is transmitted in the report.
1603+
logger.debug(exc, exc_info=True)
1604+
target_loader.report.final(
1605+
f"Uploading {target_loader.data_bundle} failed",
1606+
upload_state=UploadState.SUCCESS,
1607+
)
1608+
return
16121609

16131610
else:
1614-
# move to final location
1611+
# Move the uploaded file to its final location
16151612
target_loader.abs_final_path.mkdir(parents=True)
16161613
target_loader.raw_data.rename(target_loader.abs_final_path)
16171614
Path(target_loader.bundle_path).rename(
@@ -1621,7 +1618,8 @@ def load_target(
16211618
set_directory_permissions(target_loader.abs_final_path, 0o755)
16221619

16231620
target_loader.report.final(
1624-
f"{target_loader.data_bundle} uploaded successfully"
1621+
f"{target_loader.data_bundle} uploaded successfully",
1622+
upload_state=UploadState.SUCCESS,
16251623
)
16261624
target_loader.experiment_upload.message = target_loader.report.json()
16271625

‎viewer/views.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -1578,14 +1578,12 @@ def create(self, request, *args, **kwargs):
15781578
class TaskStatus(APIView):
15791579
def get(self, request, task_id, *args, **kwargs):
15801580
"""Given a task_id (a string) we try to return the status of the task,
1581-
trying to handle unknown tasks as best we can. Celery is happy to accept any
1582-
string as a Task ID. To know it's a real task takes a lot of work, or you can
1583-
simply interpret a 'PENDING' state as "unknown task".
1581+
trying to handle unknown tasks as best we can.
15841582
"""
15851583
# Unused arguments
15861584
del request, args, kwargs
15871585

1588-
logger.info("task_id=%s", task_id)
1586+
logger.debug("task_id=%s", task_id)
15891587

15901588
# task_id is a UUID, but Celery expects a string
15911589
task_id_str = str(task_id)
@@ -1609,11 +1607,13 @@ def get(self, request, task_id, *args, **kwargs):
16091607
elif isinstance(result.info, list):
16101608
messages = result.info
16111609

1610+
# The task is considered to have failed
1611+
# if the word 'FAILED' is in the last line of the message (regardless of case)
16121612
task_status = "UNKNOWN"
1613-
if result.ready():
1614-
task_status = "SUCCESS" if result.successful() else "FAILED"
1613+
if result.ready() and messages:
1614+
task_status = "FAILED" if 'failed' in messages[-1].lower() else "SUCCESS"
1615+
16151616
data = {
1616-
'id': result.id,
16171617
'started': result.state != 'PENDING',
16181618
'finished': result.ready(),
16191619
'status': task_status,

0 commit comments

Comments
 (0)
Please sign in to comment.