Skip to content

Commit

Permalink
Added Controller!
Browse files Browse the repository at this point in the history
Co-authored-by: Abdelrahman Elsalh <abdof1723@gmail.com>
Co-authored-by: Ziad Othman <zoth@bendingspoons.com>
  • Loading branch information
2 people authored and Ahmad45123 committed May 17, 2024
1 parent cc890f4 commit c9cec6a
Show file tree
Hide file tree
Showing 23 changed files with 446 additions and 112 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ build:
docker build ./services/users --tag workup:service_users
docker build ./services/contracts --tag workup:service_contracts
docker build ./webserver --tag workup:webserver
docker build ./controller --tag workup:controller

up:
docker stack deploy -c compose.yaml -c compose.override.yaml workup
Expand Down
2 changes: 0 additions & 2 deletions compose.override.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,3 @@ services:

service_webserver:
image: workup:webserver
controller:
image: workup:controller
26 changes: 14 additions & 12 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,21 @@ services:
- frontend
env_file:
- ./webserver/.env
controller:
image: ahmad45123/workup:controller
depends_on:
- service_mq
networks:
- default
env_file:
- ./controller/.env
stdin_open: true
tty: true
# controller:
# image: ahmad45123/workup:controller
# depends_on:
# - service_mq
# networks:
# - default
# env_file:
# - ./controller/.env
# stdin_open: true
# tty: true

service_mq:
image: rabbitmq:3.13-management
ports:
- "5672:5672"
healthcheck:
test: rabbitmq-diagnostics -q ping
interval: 30s
Expand All @@ -35,7 +37,7 @@ services:
networks:
- default
- frontend

service_redis:
image: redis:7.2.4
healthcheck:
Expand Down Expand Up @@ -90,7 +92,7 @@ services:
POSTGRES_USER: payments_user
POSTGRES_DB: payments_database
healthcheck:
test: ["CMD", "pg_isready"]
test: [ "CMD", "pg_isready" ]
interval: 20s
timeout: 10s
retries: 10
Expand Down
4 changes: 3 additions & 1 deletion controller/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM eclipse-temurin:21-jre-alpine
VOLUME /tmp
ARG JAR_FILE=target/*.jar
ARG JOBS_JAR_FILE=target/dependency/jobs-0.0.1-SNAPSHOT.jar
COPY ${JAR_FILE} controller.jar
ENTRYPOINT ["sh", "-c", "java ${JAVA_OPTS} -jar /controller.jar ${0} ${@}"]
COPY ${JOBS_JAR_FILE} jobs.jar
ENTRYPOINT ["tail", "-f", "/dev/null"]
38 changes: 37 additions & 1 deletion controller/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,21 @@
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>com.workup</groupId>
<artifactId>jobs</artifactId>
<version>${project.version}</version>
<exclusions>
<exclusion>
<groupId>org.testcontainers</groupId>
<artifactId>cassandra</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand All @@ -49,6 +63,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
<dependency>
<groupId>com.workup</groupId>
<artifactId>shared</artifactId>
Expand All @@ -67,6 +86,23 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- Configure the plugin as needed -->
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
Expand Down
76 changes: 64 additions & 12 deletions controller/src/main/java/com/workup/controller/CLIHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import asg.cliche.CLIException;
import asg.cliche.Command;
import com.workup.shared.commands.controller.SetLoggingLevelRequest;
import com.workup.shared.commands.controller.SetMaxThreadsRequest;
import com.workup.shared.commands.controller.*;
import com.workup.shared.commands.jobs.requests.CreateJobRequest;
import com.workup.shared.enums.ControllerQueueNames;
import com.workup.shared.enums.ServiceQueueNames;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javassist.*;
import org.apache.logging.log4j.Level;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -41,18 +42,40 @@ public String maxThreads(String app, int maxThreads) throws CLIException {
}

@Command(description = "Set the maximum number of DB connections for a specific app")
public String maxdb(String app, int appNum, String maxDBConn) {
return "maxdb";
public String maxdb(String app, int maxDBConn) {
app = app.toLowerCase();
if (!appQueueMap.containsKey(app)) {
return "Error: app can only be jobs, users, contracts or payments!";
}
if (maxDBConn > 100 || maxDBConn < 1) {
return "Error: Max threads must have a value between 1 and 100";
}
rabbitTemplate.convertAndSend(
appQueueMap.get(app),
"",
SetMaxDBConnectionsRequest.builder().withMaxDBConnections(maxDBConn).build());
return "Command Sent!";
}

@Command(description = "starts a specific app")
public String start(String app, int appNum) {
return "start";
public String start(String app) {
app = app.toLowerCase();
if (!appQueueMap.containsKey(app)) {
return "Error: app can only be jobs, users, contracts or payments!";
}

rabbitTemplate.convertAndSend(appQueueMap.get(app), "", ContinueRequest.builder().build());
return "Command sent";
}

@Command(description = "stops a specific app")
public String freeze(String app, int appNum) {
return "freeze";
public String freeze(String app) {
app = app.toLowerCase();
if (!appQueueMap.containsKey(app)) {
return "Error: app can only be jobs, users, contracts or payments!";
}
rabbitTemplate.convertAndSend(appQueueMap.get(app), "", FreezeRequest.builder().build());
return "Command sent";
}

@Command(description = "stops a specific app")
Expand All @@ -75,8 +98,8 @@ public String setLoggingLevel(String app, String level) {

@Command(description = "test")
public void test() {
rabbitTemplate.convertSendAndReceive(
ServiceQueueNames.JOBS, CreateJobRequest.builder().withTitle("Ziko").build());
CreateJobRequest request = CreateJobRequest.builder().withTitle("Ziko").build();
rabbitTemplate.convertSendAndReceive(ServiceQueueNames.JOBS, request);
}

@Command(description = "Creates a new command")
Expand All @@ -85,8 +108,37 @@ public String addcommand(String app, String commandName, String className) {
}

@Command(description = "Updates an existing command")
public String updatecommand(String app, String commandName, String className) {
return "Update Command";
public String updateCommand(String app, String commandName, String className) throws Exception {
app = app.toLowerCase();
if (!appQueueMap.containsKey(app)) {
return "Error: app can only be jobs, users, contracts or payments!";
}
try {
byte[] byteArray = getByteCode(commandName, className);
rabbitTemplate.convertAndSend(
appQueueMap.get(app),
"",
UpdateCommandRequest.builder()
.withCommandName(commandName)
.withByteCode(byteArray)
.build());
} catch (Exception ex) {
ex.printStackTrace();
}
return "Command sent!!";
}

private byte[] getByteCode(String commandName, String className)
throws InstantiationException,
IllegalAccessException,
NotFoundException,
IOException,
CannotCompileException {
ClassPool pool = ClassPool.getDefault();
pool.insertClassPath(new ClassClassPath(ControllerApplication.class));
CtClass ctClass = pool.get(className);
// That's the compiled class byte code
return ctClass.toBytecode();
}

@Command(description = "Deletes an existing command")
Expand Down
6 changes: 3 additions & 3 deletions controller/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
spring.application.name=controller
spring.rabbitmq.host=${JOBS_MQ_URL}
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=${JOBS_MQ_USER}
spring.rabbitmq.password=${JOBS_MQ_PASSWORD}
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
19 changes: 9 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

<modules>
<module>shared</module>
<module>controller</module>
<module>services/jobs</module>
<module>services/payments</module>
<module>services/users</module>
Expand Down Expand Up @@ -61,18 +60,18 @@
<artifactId>git-build-hook-maven-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<gitConfig>
<pull.rebase>true</pull.rebase>
</gitConfig>
<gitConfig>
<pull.rebase>true</pull.rebase>
</gitConfig>
</configuration>
<executions>
<execution>
<goals>
<goal>configure</goal>
</goals>
</execution>
<execution>
<goals>
<goal>configure</goal>
</goals>
</execution>
</executions>
</plugin>
</plugin>
</plugins>
</build>

Expand Down
8 changes: 4 additions & 4 deletions services/jobs/cassandra-config/cassandra.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ seed_provider:
parameters:
# seeds is actually a comma-delimited list of addresses.
# Ex: "<ip1>,<ip2>,<ip3>"
- seeds: "10.0.10.22"
- seeds: "10.0.4.12"

# For workloads with more data than can fit in memory, Cassandra's
# bottleneck will be reads that need to fetch data from
Expand Down Expand Up @@ -663,7 +663,7 @@ ssl_storage_port: 7001
#
# Setting listen_address to 0.0.0.0 is always wrong.
#
listen_address: 10.0.10.22
listen_address: 10.0.4.12

# Set listen_address OR listen_interface, not both. Interfaces must correspond
# to a single address, IP aliasing is not supported.
Expand All @@ -677,7 +677,7 @@ listen_address: 10.0.10.22

# Address to broadcast to other Cassandra nodes
# Leaving this blank will set it to the same value as listen_address
broadcast_address: 10.0.10.22
broadcast_address: 10.0.4.12

# When using multiple physical network interfaces, set this
# to true to listen on broadcast_address in addition to
Expand Down Expand Up @@ -763,7 +763,7 @@ rpc_address: 0.0.0.0
# be set to 0.0.0.0. If left blank, this will be set to the value of
# rpc_address. If rpc_address is set to 0.0.0.0, broadcast_rpc_address must
# be set.
broadcast_rpc_address: 10.0.10.22
broadcast_rpc_address: 10.0.4.12

# enable or disable keepalive on rpc/native connections
rpc_keepalive: true
Expand Down
5 changes: 0 additions & 5 deletions services/jobs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@
<artifactId>shared</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Loading

0 comments on commit c9cec6a

Please sign in to comment.