code improvements & delete task endpoint

This commit is contained in:
2021-11-02 20:58:59 +01:00
parent 6752454838
commit 06172b34cd
29 changed files with 256 additions and 24 deletions

View File

@@ -19,7 +19,11 @@ public class TaskAvailableController {
this.taskAvailableUseCase = taskAvailableUseCase;
}
@GetMapping(path = "/newtask/{taskType}")
/**
* Controller for notification about new events.
* @return 200 OK
**/
@GetMapping(path = "/newtask/{taskType}", consumes = { "application/json" })
public ResponseEntity<String> retrieveTaskFromTaskList(@PathVariable("taskType") String taskType) {
if (ExecutorType.contains(taskType.toUpperCase())) {

View File

@@ -21,6 +21,10 @@ public class ExecutionFinishedEventAdapter implements ExecutionFinishedEventPort
Logger logger = Logger.getLogger(ExecutionFinishedEventAdapter.class.getName());
/**
* Publishes the execution finished event
* @return void
**/
@Override
public void publishExecutionFinishedEvent(ExecutionFinishedEvent event) {
@@ -46,7 +50,7 @@ public class ExecutionFinishedEventAdapter implements ExecutionFinishedEventPort
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
System.out.println("Finish execution event sent with result:" + event.getResult());
logger.log(Level.INFO, "Finish execution event sent with result: {}", event.getResult());
}

View File

@@ -28,6 +28,11 @@ public class GetAssignmentAdapter implements GetAssignmentPort {
Logger logger = Logger.getLogger(GetAssignmentAdapter.class.getName());
/**
* Requests a new task assignment
* @return the assigned task
* @see Task
**/
@Override
public Task getAssignment(ExecutorType executorType, ExecutorURI executorURI) {
@@ -44,7 +49,9 @@ public class GetAssignmentAdapter implements GetAssignmentPort {
.build();
try {
logger.info("Sending getAssignment Request");
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
logger.log(Level.INFO, "getAssignment request result:\n {}", response.body());
if (response.body().equals("")) {
return null;
}

View File

@@ -27,6 +27,10 @@ public class NotifyExecutorPoolAdapter implements NotifyExecutorPoolPort {
Logger logger = Logger.getLogger(NotifyExecutorPoolAdapter.class.getName());
/**
* Notifies the executor-pool about the startup of this executor
* @return if the notification was successful
**/
@Override
public boolean notifyExecutorPool(ExecutorURI executorURI, ExecutorType executorType) {

View File

@@ -15,6 +15,6 @@ public class TaskAvailableService implements TaskAvailableUseCase {
@Override
public void newTaskAvailable(TaskAvailableCommand command) {
// Placeholder so spring can create a bean
// Placeholder so spring can create a bean, implementation of this function is inside the executors
}
}

View File

@@ -1,5 +1,7 @@
package ch.unisg.executorbase.executor.domain;
import java.util.logging.Logger;
import ch.unisg.common.exception.InvalidExecutorURIException;
import ch.unisg.common.valueobject.ExecutorURI;
import ch.unisg.executorbase.executor.adapter.out.web.ExecutionFinishedEventAdapter;
@@ -23,25 +25,28 @@ public abstract class ExecutorBase {
private ExecutorStatus status;
// TODO Violation of the Dependency Inversion Principle?, but we havn't really got a better solutions to send a http request / access a service from a domain model
// TODO I guess we can implement the execution as a service but there still is the problem with the startup request.
// TODO I guess we can somehow autowire this but I don't know why it's not working :D
private final NotifyExecutorPoolPort notifyExecutorPoolPort = new NotifyExecutorPoolAdapter();
private final NotifyExecutorPoolService notifyExecutorPoolService = new NotifyExecutorPoolService(notifyExecutorPoolPort);
private final GetAssignmentPort getAssignmentPort = new GetAssignmentAdapter();
private final ExecutionFinishedEventPort executionFinishedEventPort = new ExecutionFinishedEventAdapter();
public ExecutorBase(ExecutorType executorType) {
System.out.println("Starting Executor");
Logger logger = Logger.getLogger(ExecutorBase.class.getName());
protected ExecutorBase(ExecutorType executorType) {
logger.info("Starting Executor");
this.status = ExecutorStatus.STARTING_UP;
this.executorType = executorType;
// TODO set this automaticly
try {
this.executorURI = new ExecutorURI("localhost:8084");
} catch (InvalidExecutorURIException e) {
// Shutdown system if ip or port is not valid
// Shutdown system if the executorURI is not valid
System.exit(1);
}
this.executorType = executorType;
this.status = ExecutorStatus.STARTING_UP;
// Notify executor-pool about existence. If executor-pools response is successfull start with getting an assignment, else shut down executor.
if(!notifyExecutorPoolService.notifyExecutorPool(this.executorURI, this.executorType)) {
System.exit(0);
} else {
@@ -50,6 +55,10 @@ public abstract class ExecutorBase {
}
}
/**
* Requests a new task from the task queue
* @return void
**/
public void getAssignment() {
Task newTask = getAssignmentPort.getAssignment(this.getExecutorType(), this.getExecutorURI());
if (newTask != null) {
@@ -59,19 +68,28 @@ public abstract class ExecutorBase {
}
}
/**
* Start the execution of a task
* @return void
**/
private void executeTask(Task task) {
System.out.println("Starting execution");
logger.info("Starting execution");
this.status = ExecutorStatus.EXECUTING;
task.setResult(execution());
// TODO implement logic if execution was not successful
executionFinishedEventPort.publishExecutionFinishedEvent(
new ExecutionFinishedEvent(task.getTaskID(), task.getResult(), "SUCCESS"));
System.out.println("Finish execution");
logger.info("Finish execution");
getAssignment();
}
/**
* Implementation of the actual execution method of an executor
* @return the execution result
**/
protected abstract String execution();
}

View File

@@ -1,7 +1,7 @@
package ch.unisg.executorbase.executor.domain;
public enum ExecutorStatus {
STARTING_UP,
EXECUTING,
IDLING,
STARTING_UP, // Executor is starting
EXECUTING, // Executor is currently executing a task
IDLING, // Executor has no tasks left and is waiting for new instructions
}

View File

@@ -3,6 +3,10 @@ package ch.unisg.executorbase.executor.domain;
public enum ExecutorType {
ADDITION, ROBOT;
/**
* Checks if the give executor type exists.
* @return Wheter the given executor type exists
**/
public static boolean contains(String test) {
for (ExecutorType x : ExecutorType.values()) {