executorbase restructure + improvments
This commit is contained in:
@@ -78,6 +78,12 @@
|
||||
<artifactId>js-scriptengine</artifactId>
|
||||
<version>21.3.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.annotation</groupId>
|
||||
<artifactId>javax.annotation-api</artifactId>
|
||||
<version>1.3.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@@ -0,0 +1,40 @@
|
||||
package ch.unisg.executorcomputation;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import ch.unisg.executorbase.services.ExecuteTaskServiceBase;
|
||||
|
||||
@Component
|
||||
@Primary
|
||||
public class ExecuteTaskService extends ExecuteTaskServiceBase {
|
||||
|
||||
Logger executorLogger = Logger.getLogger(ExecuteTaskService.class.getName());
|
||||
|
||||
@Override
|
||||
public String execution(String input) {
|
||||
executorLogger.info("Executor | Starting execution with inputData: " + input);
|
||||
|
||||
ScriptEngineManager mgr = new ScriptEngineManager();
|
||||
ScriptEngine engine = mgr.getEngineByName("JavaScript");
|
||||
|
||||
String result = "";
|
||||
try {
|
||||
result = engine.eval(input).toString();
|
||||
} catch (ScriptException e1) {
|
||||
// TODO some logic if execution fails
|
||||
executorLogger.severe(e1.getMessage());
|
||||
return result;
|
||||
}
|
||||
|
||||
executorLogger.info("Executor | Finish execution");
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
@@ -1,22 +1,30 @@
|
||||
package ch.unisg.executorcomputation;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
import ch.unisg.executorcomputation.executor.domain.Executor;
|
||||
import ch.unisg.executorbase.ExecutorBase;
|
||||
import ch.unisg.executorbase.services.NotifyExecutorPoolService;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ExecutorcomputationApplication {
|
||||
@ComponentScan({"ch.unisg.executorbase", "ch.unisg.executorcomputation"})
|
||||
public class ExecutorcomputationApplication extends ExecutorBase {
|
||||
|
||||
static Logger logger = Logger.getLogger(ExecutorcomputationApplication.class.getName());
|
||||
|
||||
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://executor-computation:8085"));
|
||||
Runtime.getRuntime().addShutdownHook(printingHook);
|
||||
|
||||
SpringApplication.run(ExecutorcomputationApplication.class, args);
|
||||
Executor.getExecutor();
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,35 +0,0 @@
|
||||
package ch.unisg.executorcomputation.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));
|
||||
}
|
||||
|
||||
return new ResponseEntity<>("OK", new HttpHeaders(), HttpStatus.OK);
|
||||
}
|
||||
}
|
@@ -1,28 +0,0 @@
|
||||
package ch.unisg.executorcomputation.executor.application.service;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import ch.unisg.executorcomputation.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();
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,57 +0,0 @@
|
||||
package ch.unisg.executorcomputation.executor.domain;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
|
||||
import ch.unisg.executorbase.executor.domain.ExecutorBase;
|
||||
import ch.unisg.executorbase.executor.domain.ExecutorType;
|
||||
|
||||
public class Executor extends ExecutorBase {
|
||||
|
||||
private static Logger executorLogger = Logger.getLogger(Executor.class.getName());
|
||||
|
||||
private static final Executor executor = new Executor(ExecutorType.COMPUTATION, "http://localhost:8085");
|
||||
|
||||
public static Executor getExecutor() {
|
||||
return executor;
|
||||
}
|
||||
|
||||
private Executor(ExecutorType executorType, String uri) {
|
||||
super(executorType, uri);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected
|
||||
String execution(String inputData) {
|
||||
|
||||
executorLogger.info("Executor | Starting execution with inputData: " + inputData);
|
||||
|
||||
ScriptEngineManager mgr = new ScriptEngineManager();
|
||||
ScriptEngine engine = mgr.getEngineByName("JavaScript");
|
||||
|
||||
String result = "";
|
||||
try {
|
||||
result = engine.eval(inputData).toString();
|
||||
} catch (ScriptException e1) {
|
||||
// TODO some logic if execution fails
|
||||
executorLogger.severe(e1.getMessage());
|
||||
return result;
|
||||
}
|
||||
|
||||
try {
|
||||
TimeUnit.SECONDS.sleep(1);
|
||||
} catch (InterruptedException e) {
|
||||
return result;
|
||||
// executorLogger.log(Level.SEVERE, e.getLocalizedMessage(), e);
|
||||
// Thread.currentThread().interrupt();
|
||||
}
|
||||
|
||||
executorLogger.info("Executor | Finish execution");
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
@@ -1,5 +1,10 @@
|
||||
server.port=8085
|
||||
|
||||
executor.type=COMPUTATION
|
||||
executor.uri=http://localhost:8085
|
||||
roster.uri=http://localhost:8082
|
||||
executor.pool.uri=http://localhost:8083
|
||||
|
||||
spring.profiles.active=chaos-monkey
|
||||
chaos.monkey.enabled=false
|
||||
management.endpoint.chaosmonkey.enabled=true
|
||||
|
Reference in New Issue
Block a user