4
4
import math
5
5
import os
6
6
import tarfile
7
- import traceback
8
7
import uuid
9
8
from dataclasses import dataclass , field
10
9
from enum import Enum
@@ -153,16 +152,20 @@ def log(self, level: Level, message: str) -> None:
153
152
154
153
def final (self , message , upload_state = None ):
155
154
if upload_state :
155
+ # User has provided an over-ride for the upload state.
156
156
self .upload_state = upload_state
157
+ elif self .upload_state == UploadState .PROCESSING :
158
+ self .upload_state = UploadState .SUCCESS
159
+ logger .info (message )
157
160
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 )
164
163
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.
165
167
self .stack .append (UploadReportEntry (message = message ))
168
+ self .stack .append (UploadReportEntry (message = self .upload_state .name ))
166
169
self ._update_task (self .json ())
167
170
168
171
def json (self ):
@@ -1175,10 +1178,6 @@ def process_bundle(self):
1175
1178
# this is the last file to load. if any of the files missing, don't continue
1176
1179
if not meta or not config or not xtalforms_yaml :
1177
1180
msg = "Missing files in uploaded data, aborting"
1178
- self .report .final (
1179
- msg ,
1180
- Level .FATAL ,
1181
- )
1182
1181
raise FileNotFoundError (msg )
1183
1182
1184
1183
try :
@@ -1596,22 +1595,20 @@ def load_target(
1596
1595
raise IntegrityError (
1597
1596
f"Uploading { target_loader .data_bundle } failed"
1598
1597
)
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
-
1607
1598
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
1612
1609
1613
1610
else :
1614
- # move to final location
1611
+ # Move the uploaded file to its final location
1615
1612
target_loader .abs_final_path .mkdir (parents = True )
1616
1613
target_loader .raw_data .rename (target_loader .abs_final_path )
1617
1614
Path (target_loader .bundle_path ).rename (
@@ -1621,7 +1618,8 @@ def load_target(
1621
1618
set_directory_permissions (target_loader .abs_final_path , 0o755 )
1622
1619
1623
1620
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 ,
1625
1623
)
1626
1624
target_loader .experiment_upload .message = target_loader .report .json ()
1627
1625
0 commit comments