- 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()".
This commit is contained in:
parent
e091a029a8
commit
5386035397
|
@ -15,7 +15,10 @@ import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.client.RestClientException;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
|
||||||
|
@ -29,16 +32,21 @@ public class AssignmentHandler {
|
||||||
|
|
||||||
private static final boolean askForTest = false; // Enable this only for testing.
|
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()
|
public static AssignmentRequest requestAssignments()
|
||||||
{
|
{
|
||||||
String requestUrl = UrlsWorkerApplication.controllerBaseUrl + "urls" + (askForTest ? "/test" : "") + "?workerId=" + UrlsWorkerApplication.workerId + "&workerAssignmentsLimit=" + WorkerConstants.ASSIGNMENTS_LIMIT;
|
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);
|
logger.info("Going to request assignments from the controller-server: " + requestUrl);
|
||||||
|
|
||||||
|
RestTemplate restTemplate = new RestTemplateBuilder().setConnectTimeout(requestConnectTimeoutDuration).setReadTimeout(requestReadTimeoutDuration).build();
|
||||||
String json = null;
|
String json = null;
|
||||||
try {
|
try {
|
||||||
json = new RestTemplateBuilder().build().getForObject(requestUrl, String.class);
|
json = restTemplate.getForObject(requestUrl, String.class);
|
||||||
} catch (Exception e) {
|
} catch (RestClientException e) {
|
||||||
logger.error("Could not retrieve the assignments!\n" + e.getMessage());
|
logger.error("Could not retrieve the assignments!\n" + e.getMessage());
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -46,15 +54,12 @@ public class AssignmentHandler {
|
||||||
AssignmentRequest assignmentRequest = null;
|
AssignmentRequest assignmentRequest = null;
|
||||||
try {
|
try {
|
||||||
assignmentRequest = new ObjectMapper().readValue(json, AssignmentRequest.class);
|
assignmentRequest = new ObjectMapper().readValue(json, AssignmentRequest.class);
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException jpe) {
|
||||||
e.printStackTrace();
|
String errorMessage = "Could not process/map the json to an \"assignmentRequest\"!\n" + jpe.getMessage();
|
||||||
}
|
|
||||||
|
|
||||||
if ( assignmentRequest == null ) {
|
|
||||||
String errorMessage = "Could not map the json to an \"assignmentRequest\"!";
|
|
||||||
System.err.println(errorMessage);
|
|
||||||
logger.error(errorMessage);
|
logger.error(errorMessage);
|
||||||
System.exit(1);
|
System.err.println(errorMessage);
|
||||||
|
jpe.printStackTrace();
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
//logger.debug(assignmentRequest.toString()); // DEBUG!
|
//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.
|
isAvailableForWork = true; // State this before posting, to catch the soonest next scheduled request.
|
||||||
|
|
||||||
/*logger.debug("UrlReports:"); // DEBUG!
|
if ( askForTest ) {
|
||||||
for ( UrlReport urlReport : urlReports ) {
|
logger.debug("UrlReports:"); // DEBUG!
|
||||||
logger.debug(urlReport.toString());
|
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.
|
||||||
postWorkerReport(assignmentRequestCounter);
|
else
|
||||||
|
postWorkerReport(assignmentRequestCounter);
|
||||||
|
|
||||||
// Note: Cannot call this method here retrospectively, as if it runs 100s of times, the memory may break..
|
// 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..
|
// The scheduler will handle calling it every half an hour, in case the Worker is available for work..
|
||||||
|
|
Loading…
Reference in New Issue