diff --git a/src/main/java/edu/harvard/iq/dataverse/globus/GlobusServiceBean.java b/src/main/java/edu/harvard/iq/dataverse/globus/GlobusServiceBean.java index 667b1750ef1..eb8153a3296 100644 --- a/src/main/java/edu/harvard/iq/dataverse/globus/GlobusServiceBean.java +++ b/src/main/java/edu/harvard/iq/dataverse/globus/GlobusServiceBean.java @@ -817,18 +817,36 @@ public void globusUpload(JsonObject jsonData, Dataset dataset, String httpReques logger.fine("json: " + JsonUtil.prettyPrint(jsonData)); - globusLogger.info("Globus upload initiated"); - String taskIdentifier = jsonData.getString("taskIdentifier"); + + globusLogger.info("Globus upload initiated, task "+taskIdentifier); + GlobusEndpoint endpoint = getGlobusEndpoint(dataset); // The first check on the status of the task: // It is important to be careful here, and not give up on the task // prematurely if anything goes wrong during this initial api call! - // So, perhaps a @todo - make a number of attempts until we get a - // valid response ? - GlobusTaskState taskState = getTask(endpoint.getClientToken(), taskIdentifier, globusLogger); + + GlobusTaskState taskState = null; + + int retriesLimit = 3; + int retries = 0; + + while (taskState == null && retries < retriesLimit) { + taskState = getTask(endpoint.getClientToken(), taskIdentifier, globusLogger); + retries++; + try { + Thread.sleep(3000); + } catch (InterruptedException ie) { + logger.warning("caught an Interrupted Exception while trying to sleep for 3 sec. in globusDownload()"); + } + } + + if (taskState != null) { + globusLogger.info("Task owner: "+taskState.getOwner_id()+", human-friendly owner name: "+taskState.getOwner_string()); + } + String ruleId = taskState != null ? getRuleId(endpoint, taskState.getOwner_id(), "rw") : null; @@ -1269,11 +1287,11 @@ public void globusDownload(JsonObject jsonObject, Dataset dataset, User authUser } else { globusLogger = logger; } - - globusLogger.info("Starting monitoring a globus download task"); - + String taskIdentifier = jsonObject.getString("taskIdentifier"); + globusLogger.info("Starting monitoring a globus download task "+taskIdentifier); + GlobusEndpoint endpoint = getGlobusEndpoint(dataset); logger.fine("Endpoint path: " + endpoint.getBasePath()); @@ -1307,6 +1325,10 @@ public void globusDownload(JsonObject jsonObject, Dataset dataset, User authUser } } + if (taskState != null) { + globusLogger.info("Task owner: "+taskState.getOwner_id()+", human-friendly owner name: "+taskState.getOwner_string()); + } + String ruleId = taskState != null ? getRuleId(endpoint, taskState.getOwner_id(), "r") : null; diff --git a/src/main/java/edu/harvard/iq/dataverse/globus/GlobusTaskState.java b/src/main/java/edu/harvard/iq/dataverse/globus/GlobusTaskState.java index 4bce2b823a6..399d2ec1a87 100644 --- a/src/main/java/edu/harvard/iq/dataverse/globus/GlobusTaskState.java +++ b/src/main/java/edu/harvard/iq/dataverse/globus/GlobusTaskState.java @@ -17,8 +17,7 @@ public class GlobusTaskState { private boolean skip_source_errors; private String nice_status; private String nice_status_short_description; - // @todo: add fields for other potentially useful things, like the - // human-friendly name associated with the Globus account, etc. + private String owner_string; public String getDestination_endpoint_display_name() { return destination_endpoint_display_name; @@ -95,5 +94,12 @@ public void setNice_status(String nice_status) { public String getNice_status_short_description() { return nice_status_short_description; } - + + public void setOwner_string(String owner_string) { + this.owner_string = owner_string; + } + + public String getOwner_string() { + return owner_string; + } } \ No newline at end of file diff --git a/src/main/java/edu/harvard/iq/dataverse/globus/TaskMonitoringServiceBean.java b/src/main/java/edu/harvard/iq/dataverse/globus/TaskMonitoringServiceBean.java index 6b6ef0f74e9..c84b74a2823 100644 --- a/src/main/java/edu/harvard/iq/dataverse/globus/TaskMonitoringServiceBean.java +++ b/src/main/java/edu/harvard/iq/dataverse/globus/TaskMonitoringServiceBean.java @@ -38,8 +38,6 @@ public class TaskMonitoringServiceBean { @Resource ManagedScheduledExecutorService scheduler; - @EJB - SystemConfig systemConfig; @EJB SettingsServiceBean settingsSvc; @EJB @@ -53,11 +51,13 @@ public void init() { logger.info("Starting Globus task monitoring service"); int pollingInterval = SystemConfig.getIntLimitFromStringOrDefault( settingsSvc.getValueForKey(SettingsServiceBean.Key.GlobusPollingInterval), 600); + + // Monitoring service scheduler for ongoing upload tasks: this.scheduler.scheduleWithFixedDelay(this::checkOngoingUploadTasks, 0, pollingInterval, TimeUnit.SECONDS); - // A separate monitoring service for ongoing download tasks: + // A separate monitoring service scheduler for ongoing download tasks: this.scheduler.scheduleWithFixedDelay(this::checkOngoingDownloadTasks, 0, pollingInterval, TimeUnit.SECONDS); @@ -79,11 +79,12 @@ public void checkOngoingUploadTasks() { List tasks = globusService.findAllOngoingTasks(GlobusTaskInProgress.TaskType.UPLOAD); tasks.forEach(t -> { - FileHandler taskLogHandler = getTaskLogHandler(t); - Logger taskLogger = getTaskLogger(t, taskLogHandler); - - GlobusTaskState retrieved = globusService.getTask(t.getGlobusToken(), t.getTaskId(), taskLogger); + GlobusTaskState retrieved = globusService.getTask(t.getGlobusToken(), t.getTaskId(), null); + if (GlobusUtil.isTaskCompleted(retrieved)) { + FileHandler taskLogHandler = getTaskLogHandler(t); + Logger taskLogger = getTaskLogger(t, taskLogHandler); + // Do our thing, finalize adding the files to the dataset globusService.processCompletedTask(t, retrieved, GlobusUtil.isTaskSucceeded(retrieved), GlobusUtil.getCompletedTaskStatus(retrieved), taskLogger); // Whether it finished successfully, or failed in the process, @@ -91,13 +92,12 @@ public void checkOngoingUploadTasks() { // delete it. //globusService.deleteExternalUploadRecords(t.getTaskId()); globusService.deleteTask(t); + + if (taskLogHandler != null) { + taskLogHandler.close(); + } } - - if (taskLogHandler != null) { - // @todo it should be prudent to cache these loggers and handlers - // between monitoring runs (should be fairly easy to do) - taskLogHandler.close(); - } + }); } @@ -111,26 +111,30 @@ public void checkOngoingDownloadTasks() { List tasks = globusService.findAllOngoingTasks(GlobusTaskInProgress.TaskType.DOWNLOAD); tasks.forEach(t -> { - FileHandler taskLogHandler = getTaskLogHandler(t); - Logger taskLogger = getTaskLogger(t, taskLogHandler); - - GlobusTaskState retrieved = globusService.getTask(t.getGlobusToken(), t.getTaskId(), taskLogger); + + GlobusTaskState retrieved = globusService.getTask(t.getGlobusToken(), t.getTaskId(), null); + if (GlobusUtil.isTaskCompleted(retrieved)) { + FileHandler taskLogHandler = getTaskLogHandler(t); + Logger taskLogger = getTaskLogger(t, taskLogHandler); + String taskStatus = retrieved == null ? "N/A" : retrieved.getStatus(); + taskLogger.info("Checking on task " + t.getTaskId() + ", status: " + taskStatus); + globusService.processCompletedTask(t, retrieved, GlobusUtil.isTaskSucceeded(retrieved), GlobusUtil.getCompletedTaskStatus(retrieved), taskLogger); // globusService.processCompletedTask(t, GlobusUtil.isTaskSucceeded(retrieved), GlobusUtil.getTaskStatus(retrieved), taskLogger); // Whether it finished successfully or failed, the task can now // be deleted. globusService.deleteTask(t); - } - - if (taskLogHandler != null) { - // @todo it should be prudent to cache these loggers and handlers - // between monitoring runs (should be fairly easy to do) - taskLogHandler.close(); + + if (taskLogHandler != null) { + taskLogHandler.close(); + } } }); } - + // @todo: combine the 2 methods below into one (?) + // @todo: move the method(s) below into the GlobusUtil, for the Globus Service to use as well + // @todo: switch to a different log formatter (from the default xml) (?) private FileHandler getTaskLogHandler(GlobusTaskInProgress task) { if (task == null) { return null; @@ -146,7 +150,7 @@ private FileHandler getTaskLogHandler(GlobusTaskInProgress task) { + ".log"; FileHandler fileHandler; try { - fileHandler = new FileHandler(logFileName); + fileHandler = new FileHandler(logFileName, true); } catch (IOException | SecurityException ex) { // @todo log this error somehow? fileHandler = null;