From 5386035397a2ed5a53697731cef3d22e8db80be1 Mon Sep 17 00:00:00 2001 From: LSmyrnaios Date: Thu, 23 Sep 2021 16:23:49 +0300 Subject: [PATCH] - Add timeDurationLimits to wait for the requested Assignments to come from the Controller. - Make sure that the test-Results do not get posted to the Controller and written to the database. - Improve error-handling in "AssignmentHandler.requestAssignments()". --- .../urls_worker/util/AssignmentHandler.java | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/main/java/eu/openaire/urls_worker/util/AssignmentHandler.java b/src/main/java/eu/openaire/urls_worker/util/AssignmentHandler.java index 2dcd302..fe4bbb0 100644 --- a/src/main/java/eu/openaire/urls_worker/util/AssignmentHandler.java +++ b/src/main/java/eu/openaire/urls_worker/util/AssignmentHandler.java @@ -15,7 +15,10 @@ import org.slf4j.LoggerFactory; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestClientException; +import org.springframework.web.client.RestTemplate; +import java.time.Duration; import java.util.*; @@ -29,16 +32,21 @@ public class AssignmentHandler { private static final boolean askForTest = false; // Enable this only for testing. + private static final Duration requestConnectTimeoutDuration = Duration.ofSeconds(20); // 20 seconds. + private static final Duration requestReadTimeoutDuration = Duration.ofMinutes(30); // 30 minutes. + // The controller has to retrieve the data from the database, then prepare them in memory, insert them in the "assignment"-table and, finally, return them to the worker. + public static AssignmentRequest requestAssignments() { String requestUrl = UrlsWorkerApplication.controllerBaseUrl + "urls" + (askForTest ? "/test" : "") + "?workerId=" + UrlsWorkerApplication.workerId + "&workerAssignmentsLimit=" + WorkerConstants.ASSIGNMENTS_LIMIT; logger.info("Going to request assignments from the controller-server: " + requestUrl); + RestTemplate restTemplate = new RestTemplateBuilder().setConnectTimeout(requestConnectTimeoutDuration).setReadTimeout(requestReadTimeoutDuration).build(); String json = null; try { - json = new RestTemplateBuilder().build().getForObject(requestUrl, String.class); - } catch (Exception e) { + json = restTemplate.getForObject(requestUrl, String.class); + } catch (RestClientException e) { logger.error("Could not retrieve the assignments!\n" + e.getMessage()); return null; } @@ -46,15 +54,12 @@ public class AssignmentHandler { AssignmentRequest assignmentRequest = null; try { assignmentRequest = new ObjectMapper().readValue(json, AssignmentRequest.class); - } catch (JsonProcessingException e) { - e.printStackTrace(); - } - - if ( assignmentRequest == null ) { - String errorMessage = "Could not map the json to an \"assignmentRequest\"!"; - System.err.println(errorMessage); + } catch (JsonProcessingException jpe) { + String errorMessage = "Could not process/map the json to an \"assignmentRequest\"!\n" + jpe.getMessage(); logger.error(errorMessage); - System.exit(1); + System.err.println(errorMessage); + jpe.printStackTrace(); + return null; } //logger.debug(assignmentRequest.toString()); // DEBUG! @@ -111,12 +116,13 @@ public class AssignmentHandler { isAvailableForWork = true; // State this before posting, to catch the soonest next scheduled request. - /*logger.debug("UrlReports:"); // DEBUG! - for ( UrlReport urlReport : urlReports ) { - logger.debug(urlReport.toString()); - }*/ - - postWorkerReport(assignmentRequestCounter); + if ( askForTest ) { + logger.debug("UrlReports:"); // DEBUG! + for ( UrlReport urlReport : urlReports ) + logger.debug(urlReport.toString()); + } // Avoid posting the results in "askForTestUrls"-mode. We don't want for test-results to be written into the database by the controller. + else + postWorkerReport(assignmentRequestCounter); // Note: Cannot call this method here retrospectively, as if it runs 100s of times, the memory may break.. // The scheduler will handle calling it every half an hour, in case the Worker is available for work..