- Return an HTTP-500-error when the server cannot find the resourceFile requested by the "getTestUrls"-endpoint.

- Close the "inputScanner" after each use when retrieving the test-tasks.
- Show info-logs when sending an assignment to a worker.
- Code cleanup.
This commit is contained in:
Lampros Smyrnaios 2021-06-10 14:21:39 +03:00
parent 6729f51b03
commit 308cab5ecd
3 changed files with 25 additions and 19 deletions

View File

@ -28,16 +28,13 @@ dependencies {
implementation("org.springframework.security:spring-security-core:${springSecurityVersion}") implementation("org.springframework.security:spring-security-core:${springSecurityVersion}")
implementation("org.springframework.security:spring-security-web:${springSecurityVersion}") implementation("org.springframework.security:spring-security-web:${springSecurityVersion}")
implementation("org.springframework.security:spring-security-config:${springSecurityVersion}") implementation("org.springframework.security:spring-security-config:${springSecurityVersion}")
implementation("io.jsonwebtoken:jjwt:0.9.1") //implementation("io.jsonwebtoken:jjwt:0.9.1") // Use this in case we use auth-tokens later on.
// https://mvnrepository.com/artifact/com.google.guava/guava // https://mvnrepository.com/artifact/com.google.guava/guava
implementation group: 'com.google.guava', name: 'guava', version: '30.1.1-jre' implementation group: 'com.google.guava', name: 'guava', version: '30.1.1-jre'
implementation "org.projectlombok:lombok:1.18.20" implementation "org.projectlombok:lombok:1.18.20"
implementation 'com.google.code.gson:gson:2.8.7'
implementation group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final' implementation group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final'
implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
implementation group: 'commons-io', name: 'commons-io', version: '2.9.0'
testImplementation group: 'org.springframework.security', name: 'spring-security-test', version: springSecurityVersion testImplementation group: 'org.springframework.security', name: 'spring-security-test', version: springSecurityVersion
testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.boot:spring-boot-starter-test'

View File

@ -41,6 +41,8 @@ public class UrlController {
// TODO - Write the Assignment details to the database and then send it to the worker. // TODO - Write the Assignment details to the database and then send it to the worker.
logger.info("Sending assignment_" + assignment.getAssignmentId() + " to worker with ID: " + workerId);
return ResponseEntity.status(200).header("Content-Type", "application/json").body(new AssignmentResponse(assignment)); return ResponseEntity.status(200).header("Content-Type", "application/json").body(new AssignmentResponse(assignment));
} }
@ -50,16 +52,20 @@ public class UrlController {
@GetMapping("test") @GetMapping("test")
public ResponseEntity<?> getTestUrls(@RequestParam String workerId, @RequestParam int tasksLimit) { public ResponseEntity<?> getTestUrls(@RequestParam String workerId, @RequestParam int tasksLimit) {
List<Task> tasks = new ArrayList<>(); try {
new FileUtils(); // Find the input file. new FileUtils(); // Find the input file.
} catch (Exception e) {
logger.error(e.getMessage());
return ResponseEntity.status(500).body("The resource file for the requested tasks was not found.");
}
List<Task> tasks = new ArrayList<>();
HashMultimap<String, String> loadedIdUrlPairs; HashMultimap<String, String> loadedIdUrlPairs;
boolean isFirstRun = true; boolean isFirstRun = true;
boolean tasksLimitReached = false; boolean tasksLimitReached = false;
// Start loading urls. // Start loading urls.
while ( true ) while ( true ) {
{
loadedIdUrlPairs = FileUtils.getNextIdUrlPairBatchFromJson(); // Take urls from jsonFile. loadedIdUrlPairs = FileUtils.getNextIdUrlPairBatchFromJson(); // Take urls from jsonFile.
if ( FileUtils.isFinishedLoading(loadedIdUrlPairs.isEmpty(), isFirstRun) ) // Throws RuntimeException which is automatically passed on. if ( FileUtils.isFinishedLoading(loadedIdUrlPairs.isEmpty(), isFirstRun) ) // Throws RuntimeException which is automatically passed on.
@ -81,14 +87,18 @@ public class UrlController {
}// end pairs-for-loop }// end pairs-for-loop
if ( tasksLimitReached ) { if ( tasksLimitReached ) {
logger.debug("Done loading tasksLimit (" + tasksLimit + ") urls from the inputFile."); logger.debug("Done loading urls from the inputFile as the tasksLimit (" + tasksLimit + ") was reached.");
break; break;
} }
}// end loading-while-loop }// end loading-while-loop
if ( FileUtils.inputScanner != null )
FileUtils.inputScanner.close();
Assignment assignment = new Assignment((++assignmentId), tasks, workerId, new Date()); Assignment assignment = new Assignment((++assignmentId), tasks, workerId, new Date());
logger.info("Sending assignment_" + assignment.getAssignmentId() + " with " + tasks.size() + " tasks (" + FileUtils.duplicateIdUrlEntries + " more tasks were discarded as duplicates), to worker with ID: " + workerId);
return ResponseEntity.status(200).header("Content-Type", "application/json").body(new AssignmentResponse(assignment)); return ResponseEntity.status(200).header("Content-Type", "application/json").body(new AssignmentResponse(assignment));
} }

View File

@ -18,25 +18,24 @@ public class FileUtils {
private static final Logger logger = LoggerFactory.getLogger(FileUtils.class); private static final Logger logger = LoggerFactory.getLogger(FileUtils.class);
private static Scanner inputScanner = null; public static Scanner inputScanner = null;
private static int fileIndex = 0; private static int fileIndex = 0;
private static int unretrievableInputLines = 0; private static int unretrievableInputLines = 0;
private static int duplicateIdUrlEntries = 0; public static int duplicateIdUrlEntries = 0;
public static int jsonBatchSize = 3000; public static int jsonBatchSize = 3000;
private static final String utf8Charset = "UTF-8"; private static final String utf8Charset = "UTF-8";
public static String inputFileFullPath; public static String inputFileFullPath;
private static String userDir = System.getProperty("user.dir") + File.separator; private static String workingDir = System.getProperty("user.dir") + File.separator;
public FileUtils()
public FileUtils() throws RuntimeException
{ {
inputFileFullPath = userDir + "src" + File.separator + "main" + File.separator + "resources"; inputFileFullPath = workingDir + "src" + File.separator + "main" + File.separator + "resources";
String resourceFileName = "testInputFiles" + File.separator + "orderedList1000.json"; String resourceFileName = "testInputFiles" + File.separator + "orderedList1000.json";
inputFileFullPath += File.separator + resourceFileName; inputFileFullPath += File.separator + resourceFileName;
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(resourceFileName); InputStream inputStream = getClass().getClassLoader().getResourceAsStream(resourceFileName);
if ( inputStream == null ) { if ( inputStream == null )
logger.error("No resourceFile was found with name \"" + resourceFileName + "\"."); throw new RuntimeException("No resourceFile was found with name \"" + resourceFileName + "\".");
return;
}
logger.debug("Going to retrieve the data from the inputResourceFile: " + resourceFileName); logger.debug("Going to retrieve the data from the inputResourceFile: " + resourceFileName);