executorbase restructure + improvments

This commit is contained in:
2021-12-22 19:35:09 +01:00
parent 1153b75322
commit 88623f6327
66 changed files with 573 additions and 950 deletions

View File

@@ -0,0 +1,27 @@
package ch.unisg.executorrobot;
import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import ch.unisg.executorbase.services.ExecuteTaskServiceBase;
@Component
@Primary
public class ExecuteTaskService extends ExecuteTaskServiceBase {
@Autowired
RobotService robotService;
private Logger executorLogger = Logger.getLogger(ExecuteTaskService.class.getName());
@Override
public String execution(String input) {
executorLogger.info("Executor | Starting execution with inputData: " + input);
robotService.executeRobotTask();
return "";
}
}

View File

@@ -1,18 +1,26 @@
package ch.unisg.executorrobot;
import java.util.concurrent.TimeUnit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import ch.unisg.executorrobot.executor.domain.Executor;
import ch.unisg.executorbase.ExecutorBase;
import ch.unisg.executorbase.services.NotifyExecutorPoolService;
@SpringBootApplication
public class ExecutorrobotApplication {
@ComponentScan({"ch.unisg.executorbase", "ch.unisg.executorrobot"})
public class ExecutorrobotApplication extends ExecutorBase {
public static void main(String[] args) {
/**
* This is not a nice solution but I didn't get the @PreDestroy hook to work... This is the
* only solution which was working so I had to make the executorStopped function static
* for now.
*/
// Thread printingHook = new Thread(() -> NotifyExecutorPoolService.executorStopped("http://localhost:8084"));
Thread printingHook = new Thread(() -> NotifyExecutorPoolService.executorStopped("http://executor-robot:8084"));
Runtime.getRuntime().addShutdownHook(printingHook);
SpringApplication.run(ExecutorrobotApplication.class, args);
Executor.getExecutor();
}
}

View File

@@ -1,4 +1,5 @@
package ch.unisg.executorrobot.executor.adapter.out;
package ch.unisg.executorrobot;
import java.io.IOException;
import java.io.StringReader;
import java.net.URI;
@@ -9,11 +10,20 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import ch.unisg.ics.interactions.wot.td.ThingDescription;
import ch.unisg.ics.interactions.wot.td.affordances.ActionAffordance;
import ch.unisg.ics.interactions.wot.td.affordances.Form;
@@ -27,25 +37,15 @@ import ch.unisg.ics.interactions.wot.td.security.SecurityScheme;
import ch.unisg.ics.interactions.wot.td.vocabularies.TD;
import ch.unisg.ics.interactions.wot.td.vocabularies.WoTSec;
import org.springframework.boot.autoconfigure.sendgrid.SendGridAutoConfiguration;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import ch.unisg.executorrobot.executor.application.port.out.UserToRobotPort;
@Component
@Primary
public class UserToRobotAdapter implements UserToRobotPort {
public class RobotService {
@Override
public String userToRobot() {
@Value("${search.engine.uri}")
String endpoint;
String endpoint = "https://api.interactions.ics.unisg.ch/search/searchEngine";
private Logger logger = Logger.getLogger(RobotService.class.getName());
public String executeRobotTask() {
String input = "@prefix dct: <http://purl.org/dc/terms/> . select ?title where { ?title dct:title 'leubot1' }";
@@ -83,10 +83,10 @@ public class UserToRobotAdapter implements UserToRobotPort {
ThingDescription td = TDGraphReader.readFromURL(ThingDescription.TDFormat.RDF_TURTLE, leubot1Uri);
String apiUrl = getAPIKey(td);
System.out.println("FOUND API URL " + apiUrl);
logger.info("Executor | FOUND API URL " + apiUrl);
String apiKey = apiUrl.split("/")[apiUrl.split("/").length-1].split("]")[0];
System.out.println("FOUND KEY " + apiKey);
logger.info("Executor | FOUND KEY " + apiKey);
if(apiKey == null) {
// TODO implement logic if execution failed
@@ -160,7 +160,7 @@ public class UserToRobotAdapter implements UserToRobotPort {
try {
TDHttpResponse response = request.execute();
System.out.println("Received response with status code: " + response.getStatusCode());
logger.info("Executor | Received response with status code: " + response.getStatusCode());
String url = response.getHeaders().get("Location");
return url;
@@ -210,7 +210,7 @@ public class UserToRobotAdapter implements UserToRobotPort {
try {
TDHttpResponse response = request.execute();
System.out.println("Received response with status code: " + response.getStatusCode());
logger.info("Executor | Received response with status code: " + response.getStatusCode());
return true;
} catch (IOException e) {
e.printStackTrace();
@@ -230,12 +230,11 @@ public class UserToRobotAdapter implements UserToRobotPort {
try {
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode());
logger.info("Executor | Delete user from robot response code: " + response.statusCode());
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

View File

@@ -1,38 +0,0 @@
package ch.unisg.executorrobot.executor.adapter.in.web;
import java.util.concurrent.CompletableFuture;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import ch.unisg.executorbase.executor.application.port.in.TaskAvailableCommand;
import ch.unisg.executorbase.executor.application.port.in.TaskAvailableUseCase;
import ch.unisg.executorbase.executor.domain.ExecutorType;
@RestController
public class TaskAvailableController {
private final TaskAvailableUseCase taskAvailableUseCase;
public TaskAvailableController(TaskAvailableUseCase taskAvailableUseCase) {
this.taskAvailableUseCase = taskAvailableUseCase;
}
@GetMapping(path = "/newtask/{taskType}")
public ResponseEntity<String> retrieveTaskFromTaskList(@PathVariable("taskType") String taskType) {
if (ExecutorType.contains(taskType.toUpperCase())) {
TaskAvailableCommand command = new TaskAvailableCommand(
ExecutorType.valueOf(taskType.toUpperCase()));
CompletableFuture.runAsync(() -> taskAvailableUseCase.newTaskAvailable(command));
}
// Add the content type as a response header
HttpHeaders responseHeaders = new HttpHeaders();
return new ResponseEntity<>("OK", responseHeaders, HttpStatus.OK);
}
}

View File

@@ -1,7 +0,0 @@
package ch.unisg.executorrobot.executor.application.port.out;
import ch.unisg.executorbase.executor.domain.ExecutorType;
public interface UserToRobotPort {
String userToRobot();
}

View File

@@ -1,28 +0,0 @@
package ch.unisg.executorrobot.executor.application.service;
import org.springframework.stereotype.Component;
import ch.unisg.executorrobot.executor.domain.Executor;
import ch.unisg.executorbase.executor.application.port.in.TaskAvailableCommand;
import ch.unisg.executorbase.executor.application.port.in.TaskAvailableUseCase;
import ch.unisg.executorbase.executor.domain.ExecutorStatus;
import lombok.RequiredArgsConstructor;
import javax.transaction.Transactional;
@RequiredArgsConstructor
@Component
@Transactional
public class TaskAvailableService implements TaskAvailableUseCase {
@Override
public void newTaskAvailable(TaskAvailableCommand command) {
Executor executor = Executor.getExecutor();
if (executor.getExecutorType() == command.getTaskType() &&
executor.getStatus() == ExecutorStatus.IDLING) {
executor.getAssignment();
}
}
}

View File

@@ -1,28 +0,0 @@
package ch.unisg.executorrobot.executor.domain;
import ch.unisg.executorrobot.executor.adapter.out.UserToRobotAdapter;
import ch.unisg.executorrobot.executor.application.port.out.UserToRobotPort;
import ch.unisg.executorbase.executor.domain.ExecutorBase;
import ch.unisg.executorbase.executor.domain.ExecutorType;
public class Executor extends ExecutorBase {
private static final Executor executor = new Executor(ExecutorType.SMALLROBOT, "http://localhost:8084");
private final UserToRobotPort userToRobotPort = new UserToRobotAdapter();
public static Executor getExecutor() {
return executor;
}
private Executor(ExecutorType executorType, String uri) {
super(executorType, uri);
}
@Override
protected
String execution(String input) {
userToRobotPort.userToRobot();
return "";
}
}

View File

@@ -1 +1,19 @@
server.port=8084
executor.type=SMALLROBOT
executor.uri=http://localhost:8084
roster.uri=http://localhost:8082
executor.pool.uri=http://localhost:8083
search.engine.uri=https://api.interactions.ics.unisg.ch/search/searchEngine
spring.profiles.active=chaos-monkey
chaos.monkey.enabled=false
management.endpoint.chaosmonkey.enabled=true
management.endpoint.chaosmonkeyjmx.enabled=true
# include specific endpoints
management.endpoints.web.exposure.include=health,info,chaosmonkey
chaos.monkey.watcher.controller=true
chaos.monkey.watcher.restController=true
chaos.monkey.watcher.service=true
chaos.monkey.watcher.repository=true
chaos.monkey.watcher.component=true