Exercise 8 PR #95

Merged
reynisson merged 68 commits from dev into main 2021-11-29 06:46:44 +00:00
21 changed files with 215 additions and 105 deletions
Showing only changes of commit f99aa7536e - Show all commits

View File

@ -26,14 +26,28 @@ services:
- "traefik.http.routers.http-catchall.middlewares=redirect-to-https"
- "traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https"
tapas-db:
image: mongo
restart: unless-stopped
container_name: tapas_mongodb
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: password
ports:
- "27017:27017"
command: mongod --quiet --logpath /dev/null
tapas-tasks:
image: openjdk
command: "java -jar /data/tapas-tasks-0.0.1-SNAPSHOT.jar"
restart: unless-stopped
depends_on:
- tapas-db
volumes:
- ./:/data/
environment:
roster.uri: http://roster:8082
spring.data.mongodb.uri: mongodb://root:password@mongodb:27017
labels:
- "traefik.enable=true"
- "traefik.http.routers.tapas-tasks.rule=Host(`tapas-tasks.${PUB_IP}.nip.io`)"
@ -47,6 +61,8 @@ services:
image: openjdk
command: "java -jar /data/tapas-auction-house-0.0.1-SNAPSHOT.jar"
restart: unless-stopped
depends_on:
- tapas-db
volumes:
- ./:/data/
environment:
@ -64,6 +80,8 @@ services:
image: openjdk
command: "java -jar /data/roster-0.0.1-SNAPSHOT.jar"
restart: unless-stopped
depends_on:
- tapas-db
volumes:
- ./:/data/
environment:
@ -71,6 +89,7 @@ services:
executor-robot.uri: http://executor-robot:8084
executor-computation.uri: http://executor-computation:8085
mqtt.broker.uri: tcp://broker.hivemq.com:1883
spring.data.mongodb.uri: mongodb://root:password@mongodb:27017
labels:
- "traefik.enable=true"
- "traefik.http.routers.roster.rule=Host(`roster.${PUB_IP}.nip.io`)"
@ -84,10 +103,13 @@ services:
image: openjdk
command: "java -jar /data/executor-pool-0.0.1-SNAPSHOT.jar"
restart: unless-stopped
depends_on:
- tapas-db
volumes:
- ./:/data/
environment:
mqtt.broker.uri: tcp://broker.hivemq.com:1883
spring.data.mongodb.uri: mongodb://root:password@mongodb:27017
labels:
- "traefik.enable=true"
- "traefik.http.routers.executor-pool.rule=Host(`executor-pool.${PUB_IP}.nip.io`)"
@ -104,6 +126,7 @@ services:
depends_on:
- executor-pool
- roster
- tapas-db
volumes:
- ./:/data/
environment:
@ -124,6 +147,7 @@ services:
depends_on:
- executor-pool
- roster
- tapas-db
volumes:
- ./:/data/
environment:

View File

@ -1,62 +1,77 @@
version: "3.6"
services:
tapas-tasks:
container_name: tapas-tasks
build:
context: "./tapas-tasks"
dockerfile: "Dockerfile"
target: development
# tapas-tasks:
# container_name: tapas-tasks
# build:
# context: "./tapas-tasks"
# dockerfile: "Dockerfile"
# target: development
# ports:
# - "8081:8081"
# - "5005:5005"
# volumes:
# - ./tapas-tasks/src:/opt/app/src
# - ./tapas-tasks/target:/opt/app/target
# assignment:
# container_name: assignment
# build:
# context: "./assignment"
# dockerfile: "Dockerfile"
# target: development
# ports:
# - "8082:8082"
# - "5006:5005"
# volumes:
# - ./assignment/src:/opt/app/src
# - ./assignment/target:/opt/app/target
# executor-pool:
# container_name: executor-pool
# build:
# context: "./executor-pool"
# dockerfile: "Dockerfile"
# target: development
# ports:
# - "8083:8083"
# - "5007:5005"
# volumes:
# - ./executor-pool/src:/opt/app/src
# - ./executor-pool/target:/opt/app/target
# executor1:
# container_name: executor1
# build:
# context: "./executor1"
# dockerfile: "Dockerfile"
# target: development
# ports:
# - "8084:8084"
# - "5008:5005"
# volumes:
# - ./executor1/src:/opt/app/src
# - ./executor1/target:/opt/app/target
# executor2:
# container_name: executor2
# build:
# context: "./executor2"
# dockerfile: "Dockerfile"
# target: development
# ports:
# - "8085:8085"
# - "5009:5005"
# volumes:
# - ./executor2/src:/opt/app/src
# - ./executor2/target:/opt/app/target
tapas-db:
image: mongo
restart: unless-stopped
container_name: tapas_mongodb
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: password
ports:
- "8081:8081"
- "5005:5005"
volumes:
- ./tapas-tasks/src:/opt/app/src
- ./tapas-tasks/target:/opt/app/target
assignment:
container_name: assignment
build:
context: "./assignment"
dockerfile: "Dockerfile"
target: development
- "27017:27017"
command: mongod --quiet --logpath /dev/null
hivemq:
image: hivemq/hivemq4
restart: unless-stopped
ports:
- "8082:8082"
- "5006:5005"
volumes:
- ./assignment/src:/opt/app/src
- ./assignment/target:/opt/app/target
executor-pool:
container_name: executor-pool
build:
context: "./executor-pool"
dockerfile: "Dockerfile"
target: development
ports:
- "8083:8083"
- "5007:5005"
volumes:
- ./executor-pool/src:/opt/app/src
- ./executor-pool/target:/opt/app/target
executor1:
container_name: executor1
build:
context: "./executor1"
dockerfile: "Dockerfile"
target: development
ports:
- "8084:8084"
- "5008:5005"
volumes:
- ./executor1/src:/opt/app/src
- ./executor1/target:/opt/app/target
executor2:
container_name: executor2
build:
context: "./executor2"
dockerfile: "Dockerfile"
target: development
ports:
- "8085:8085"
- "5009:5005"
volumes:
- ./executor2/src:/opt/app/src
- ./executor2/target:/opt/app/target
- "1883:1883"

View File

@ -34,11 +34,11 @@ public class Executor extends ExecutorBase {
double result = Double.NaN;
// try {
// TimeUnit.SECONDS.sleep(5);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (operator.equalsIgnoreCase("+")) {
String[] parts = inputData.split("\\+");

View File

@ -1,13 +1,31 @@
package ch.unisg.executorpool;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import ch.unisg.executorpool.adapter.out.persistence.mongodb.ExecutorRepository;
import ch.unisg.executorpool.application.port.out.LoadExecutorPort;
import ch.unisg.executorpool.domain.ExecutorPool;
@SpringBootApplication
@EnableMongoRepositories(basePackageClasses = ExecutorRepository.class)
public class ExecutorPoolApplication {
@Autowired
private LoadExecutorPort loadExecutorPort;
public static void main(String[] args) {
SpringApplication.run(ExecutorPoolApplication.class, args);
}
@PostConstruct
public void initializeExecutorPool() {
// Is this allowed in main or does it need to be in a service?
ExecutorPool.getExecutorPool().initExecutorPool(loadExecutorPort.loadAllExecutors());
}
}

View File

@ -1,5 +1,8 @@
package ch.unisg.executorpool.adapter.out.persistence.mongodb;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -26,8 +29,7 @@ public class ExecutorPersistenceAdapter implements AddExecutorPort, RemoveExecut
@Override
public void removeExecutor(ExecutorClass executorClass) {
MongoExecutorDocument mongoExecutorDocument = executorMapper.mapToMongoDocument(executorClass);
executorRepository.delete(mongoExecutorDocument);
executorRepository.deleteByExecutorUri(executorClass.getExecutorUri().getValue().toString());
}
@Override
@ -37,4 +39,13 @@ public class ExecutorPersistenceAdapter implements AddExecutorPort, RemoveExecut
return executorClass;
}
@Override
public List<ExecutorClass> loadAllExecutors() {
List<ExecutorClass> executorClasses = new ArrayList<>();
for (MongoExecutorDocument exe : executorRepository.findAll()) {
executorClasses.add(executorMapper.mapToDomainEntity(exe));
}
return executorClasses;
}
}

View File

@ -12,4 +12,6 @@ public interface ExecutorRepository extends MongoRepository<MongoExecutorDocumen
public List<MongoExecutorDocument> findByExecutorTaskType(String executorTaskType);
public void deleteByExecutorUri(String executorUri);
}

View File

@ -1,8 +1,11 @@
package ch.unisg.executorpool.adapter.out.persistence.mongodb;
import org.springframework.data.mongodb.core.mapping.Document;
import lombok.Data;
@Data
@Document(collection = "executors")
public class MongoExecutorDocument {
public String executorUri;

View File

@ -1,9 +1,13 @@
package ch.unisg.executorpool.application.port.out;
import java.util.List;
import ch.unisg.executorpool.domain.ExecutorClass;
public interface LoadExecutorPort {
ExecutorClass loadExecutor(ExecutorClass.ExecutorUri executorUri, ExecutorClass.ExecutorTaskType executorTaskType);
List<ExecutorClass> loadAllExecutors();
}

View File

@ -31,11 +31,10 @@ public class RemoveExecutorFromExecutorPoolService implements RemoveExecutorFrom
if(removedExecutor.isPresent()){
var executorRemovedEvent = new ExecutorRemovedEvent(removedExecutor.get());
removeExecutorFromRepositoryPort.removeExecutor(removedExecutor.get());
executorRemovedEventPort.publishExecutorRemovedEvent(executorRemovedEvent);
}
removeExecutorFromRepositoryPort.removeExecutor(removedExecutor);
return removedExecutor;
}
}

View File

@ -67,6 +67,12 @@ public class ExecutorPool {
return Optional.empty();
}
public void initExecutorPool(List<ExecutorClass> executors){
for (ExecutorClass executor : executors) {
listOfExecutors.value.add(executor);
}
}
@Value
public static class ListOfExecutors {
private List<ExecutorClass> value;

View File

@ -1,3 +1,6 @@
server.port=8083
mqtt.broker.uri=tcp://localhost:1883
spring.data.mongodb.uri=mongodb://root:password@localhost:27017
spring.data.mongodb.database=tapas-executors

View File

@ -76,6 +76,11 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>3.2.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

View File

@ -4,11 +4,14 @@ import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import ch.unisg.roster.roster.adapter.out.persistence.mongodb.RosterRepository;
import ch.unisg.roster.roster.application.port.in.LoadRosterItemPort;
import ch.unisg.roster.roster.domain.Roster;
import ch.unisg.roster.roster.domain.RosterItem;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.ConfigurableEnvironment;
@ -26,14 +29,14 @@ public class RosterApplication {
private static ConfigurableEnvironment ENVIRONMENT;
private static final LoadRosterItemPort loadRosterItemPort;
@Autowired
private LoadRosterItemPort loadRosterItemPort;
public static void main(String[] args) {
SpringApplication rosterApp = new SpringApplication(RosterApplication.class);
ENVIRONMENT = rosterApp.run(args).getEnvironment();
bootstrapMarketplaceWithMqtt();
initialiseRoster();
}
/**
@ -52,9 +55,9 @@ public class RosterApplication {
}
}
private static void initialiseRoster(){
List<RosterItem> rosterItemList = loadRosterItemPort.loadAllRosterItems();
Roster.getInstance().initialiseRoster(rosterItemList);
@PostConstruct
private void initialiseRoster(){
Roster.getInstance().initialiseRoster(loadRosterItemPort.loadAllRosterItems());
}
}

View File

@ -1,6 +1,5 @@
package ch.unisg.roster.roster.adapter.out.persistence.mongodb;
import ch.unisg.roster.roster.application.port.in.AddRosterItemPort;
import ch.unisg.roster.roster.application.port.in.DeleteRosterItem;
import ch.unisg.roster.roster.application.port.in.LoadRosterItemPort;

View File

@ -3,3 +3,6 @@ executor-robot.uri=http://127.0.0.1:8084
executor-computation.uri=http://127.0.0.1:8085
task-list.uri=http://127.0.0.1:8081
mqtt.broker.uri=tcp://localhost:1883
spring.data.mongodb.uri=mongodb://root:password@localhost:27017/
spring.data.mongodb.database=tapas-roster

View File

@ -15,18 +15,23 @@ public class MongoTaskDocument {
public String taskType;
public String originalTaskUri;
public String taskStatus;
public String inputData;
public String outputData;
public String taskListName;
public MongoTaskDocument(String taskId, String taskName, String taskType,
String originalTaskUri,
String taskStatus, String taskListName) {
String taskStatus, String inputData, String outputData,
String taskListName) {
this.taskId = taskId;
this.taskName = taskName;
this.taskType = taskType;
this.originalTaskUri = originalTaskUri;
this.taskStatus = taskStatus;
this.inputData = inputData;
this.outputData = outputData;
this.taskListName = taskListName;
}
}

View File

@ -2,18 +2,23 @@ package ch.unisg.tapastasks.tasks.adapter.out.persistence.mongodb;
import ch.unisg.tapastasks.tasks.domain.Task;
import ch.unisg.tapastasks.tasks.domain.TaskList;
import java.util.Objects;
import org.springframework.stereotype.Component;
@Component
class TaskMapper {
Task mapToDomainEntity(MongoTaskDocument task) {
return Task.withIdNameTypeOriginaluriStatus(
return Task.createTaskWithIdNameTypeOriginaluriStatusInputAndOutputData(
new Task.TaskId(task.taskId),
new Task.TaskName(task.taskName),
new Task.TaskType(task.taskType),
new Task.OriginalTaskUri(task.originalTaskUri),
new Task.TaskStatus(Task.Status.valueOf(task.taskStatus))
new Task.TaskStatus(Task.Status.valueOf(task.taskStatus)),
new Task.InputData(task.inputData),
new Task.OutputData(task.outputData)
);
}
@ -22,8 +27,10 @@ class TaskMapper {
task.getTaskId().getValue(),
task.getTaskName().getValue(),
task.getTaskType().getValue(),
task.getOriginalTaskUri().getValue(),
Objects.isNull(task.getOriginalTaskUri()) ? null : task.getOriginalTaskUri().getValue(),
task.getTaskStatus().getValue().toString(),
Objects.isNull(task.getInputData()) ? null : task.getInputData().getValue(),
Objects.isNull(task.getOutputData()) ? null : task.getOutputData().getValue(),
TaskList.getTapasTaskList().getTaskListName().getValue()
);
}

View File

@ -13,6 +13,8 @@ import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.Objects;
import javax.transaction.Transactional;
@RequiredArgsConstructor
@ -29,7 +31,7 @@ public class AddNewTaskToTaskListService implements AddNewTaskToTaskListUseCase
public Task addNewTaskToTaskList(AddNewTaskToTaskListCommand command) {
TaskList taskList = TaskList.getTapasTaskList();
taskListLock.lockTaskList(taskList.getTaskListName());
// taskListLock.lockTaskList(taskList.getTaskListName());
Task newTask;
@ -47,7 +49,7 @@ public class AddNewTaskToTaskListService implements AddNewTaskToTaskListUseCase
}
addTaskToRepositoryPort.addTask(newTask);
taskListLock.releaseTaskList(taskList.getTaskListName());
// taskListLock.releaseTaskList(taskList.getTaskListName());
//Here we are using the application service to emit the domain event to the outside of the bounded context.
//This event should be considered as a light-weight "integration event" to communicate with other services.
@ -60,7 +62,7 @@ public class AddNewTaskToTaskListService implements AddNewTaskToTaskListUseCase
taskList.getTaskListName().getValue(),
newTask.getTaskId().getValue(),
newTask.getTaskType().getValue(),
newTask.getInputData().getValue()
Objects.isNull(newTask.getInputData()) ? null : newTask.getInputData().getValue()
);
newTaskAddedEventPort.publishNewTaskAddedEvent(newTaskAdded);
}

View File

@ -81,18 +81,18 @@ public class Task {
}
public Task(TaskId taskId, TaskName taskName, TaskType taskType, OriginalTaskUri taskUri,
TaskStatus taskStatus) {
TaskStatus taskStatus, InputData inputData, OutputData outputData) {
this.taskId = taskId;
this.taskName = taskName;
this.taskType = taskType;
this.originalTaskUri = taskUri;
this.taskStatus = taskStatus;
this.inputData = null;
this.outputData = null;
this.inputData = inputData;
this.outputData = outputData;
}
protected static Task createTaskWithNameAndType(TaskName name, TaskType type) {
return new Task(name, type);
}
public static Task createTaskWithNameAndTypeAndOriginalTaskUri(TaskName name, TaskType type,
@ -110,11 +110,10 @@ this.outputData = null;
return new Task(name, type, originalTaskUri, inputData);
}
public static Task withIdNameTypeOriginaluriStatus(TaskId taskId, TaskName taskName,
TaskType taskType,
OriginalTaskUri originalTaskUri,
TaskStatus taskStatus) {
return new Task(taskId, taskName, taskType, originalTaskUri, taskStatus);
public static Task createTaskWithIdNameTypeOriginaluriStatusInputAndOutputData(TaskId taskId,
TaskName taskName, TaskType taskType, OriginalTaskUri originalTaskUri, TaskStatus taskStatus,
InputData inputData, OutputData outputData) {
return new Task(taskId, taskName, taskType, originalTaskUri, taskStatus, inputData, outputData);
}
@Value

View File

@ -74,9 +74,12 @@ public class TaskList {
//However, we skip this here as it makes the core even more complex (e.g., we have to implement a light-weight
//domain event publisher and subscribers (see "Implementing Domain-Driven Design by V. Vernon, pp. 296ff).
listOfTasks.value.add(newTask);
logger.log(Level.INFO, "New task created! Id: " + newTask.getTaskId().getValue() +
" | Name: " + newTask.getTaskName().getValue() +
" | InputData: " + newTask.getInputData().getValue());
String message = "New task created! Id: " + newTask.getTaskId().getValue() +
" | Name: " + newTask.getTaskName().getValue();
if (newTask.getInputData() != null) {
message += " | Input data: " + newTask.getInputData().getValue();
}
logger.log(Level.INFO, message);
logger.log(Level.INFO, "Number of tasks: {0}", listOfTasks.value.size());
}

View File

@ -1,6 +1,5 @@
server.port=8081
spring.data.mongodb.uri=mongodb://127.0.0.1:27017
#spring.data.mongodb.uri=mongodb://root:8nP7s0a@mongodb:27017/
spring.data.mongodb.uri=mongodb://root:password@localhost:27017/
spring.data.mongodb.database=tapas-tasks
baseuri=https://tapas-tasks.86-119-34-23.nip.io/