Improve error-handling when renaming workerReport-files.

This commit is contained in:
Lampros Smyrnaios 2023-09-08 17:41:10 +03:00
parent 1c8f3765ca
commit 6944678391
1 changed files with 17 additions and 15 deletions

View File

@ -516,24 +516,26 @@ public class UrlsServiceImpl implements UrlsService {
// Rename the worker-report to indicate success or failure. // Rename the worker-report to indicate success or failure.
String workerReportBaseName = this.workerReportsDirPath + File.separator + workerId + File.separator + workerId + "_assignments_" + assignmentRequestCounter + "_report"; String workerReportBaseName = this.workerReportsDirPath + File.separator + workerId + File.separator + workerId + "_assignments_" + assignmentRequestCounter + "_report";
File workerReport = new File(workerReportBaseName + ".json"); File workerReport = new File(workerReportBaseName + ".json");
File renamedWorkerReport = null;
boolean wasWorkerReportRenamed = true;
try {
// Check if the workerReport does not exist under this name, as it may have been renamed previously to "failed" and now is the 2nd try. // Check if the workerReport does not exist under this name, as it may have been renamed previously to "failed" and now is the 2nd try.
if ( !workerReport.isFile() ) { if ( !workerReport.isFile() ) {
// Then this is the 2nd try and the report has already been renamed to "failed" the 1st time. // Then this is the 2nd try and the report has already been renamed to "failed" the 1st time.
workerReport = new File(workerReportBaseName + "_failed.json"); workerReport = new File(workerReportBaseName + "_failed.json");
if ( !workerReport.isFile() ) { // In this case, an error exists, since this file was deleted before its time. if ( !workerReport.isFile() ) { // In this case, an error exists, since this file was deleted before its time.
logger.error("The workerReport file \"" + workerReport.getAbsolutePath() + "\" does not exist!"); logger.error("The workerReport file \"" + workerReport.getAbsolutePath() + "\" does not exist!");
// TODO - How to handle it? This report may be either successful or failed but the file was deleted. wasWorkerReportRenamed = false; // Do not proceed on renaming the non-existing file.
// TODO - Do we need additional handling? This report may be either successful or failed but the file was deleted.
} }
} }
if ( wasWorkerReportRenamed ) { // Only if the file exists.
File renamedWorkerReport = new File(workerReportBaseName + ((errorMsg == null) ? "_successful.json" : "_failed.json")); renamedWorkerReport = new File(workerReportBaseName + ((errorMsg == null) ? "_successful.json" : "_failed.json"));
boolean wasWorkerReportRenamed = true;
try {
if ( !workerReport.renameTo(renamedWorkerReport) ) { if ( !workerReport.renameTo(renamedWorkerReport) ) {
logger.warn("There was a problem when renaming the workerReport: " + workerReport.getName()); logger.warn("There was a problem when renaming the workerReport: " + workerReport.getName());
wasWorkerReportRenamed = false; wasWorkerReportRenamed = false;
} }
}
} catch (Exception e) { } catch (Exception e) {
logger.error("There was a problem when renaming the workerReport: " + workerReport.getName(), e); logger.error("There was a problem when renaming the workerReport: " + workerReport.getName(), e);
wasWorkerReportRenamed = false; wasWorkerReportRenamed = false;