Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
Empty file.
Empty file.
Empty file.
2 changes: 2 additions & 0 deletions api-gateway/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ spring:
filters:
- JwtAuthFilter


eureka:
client:
service-url:
Expand All @@ -43,6 +44,7 @@ eureka:
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${server.port}

app:
jwt:
secret: dGhpcy1pcy1hLXNlY3JldC1rZXktZm9yLWZsb3dmb3JnZS1hdXRoLXNlcnZpY2U=
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.flowforge.worker_service.adapter.out.http;

import java.util.Map;

public record HttpConfig(String url,
String method,
Map<String, String> headers,
String body) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.flowforge.worker_service.adapter.out.http;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.flowforge.worker_service.domain.model.WorkerResult;
import com.flowforge.worker_service.domain.model.WorkerTask;
import com.flowforge.worker_service.domain.worker.WorkerHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
@RequiredArgsConstructor
public class HttpWorker implements WorkerHandler {

private final ObjectMapper objectMapper;
private final RestTemplate restTemplate;

@Override
public String getType() {
return "HTTP";
}

@Override
public WorkerResult execute(WorkerTask task) {
try {
HttpConfig config = objectMapper.readValue(task.getPayload(), HttpConfig.class);

HttpHeaders headers = new HttpHeaders();
if (config.headers() != null) {
config.headers().forEach(headers::add);
}

HttpEntity<String> requestEntity = new HttpEntity<>(config.body(), headers);
HttpMethod method = HttpMethod.valueOf(config.method().toUpperCase());

ResponseEntity<String> response = restTemplate.exchange(
config.url(), method, requestEntity, String.class
);

if (response.getStatusCode().is2xxSuccessful()) {
return WorkerResult.success(task.getId(), "HTTP call succeeded: " + response.getStatusCode());
} else {
return WorkerResult.failure(task.getId(), "HTTP call failed: " + response.getStatusCode());
}

} catch (Exception exception) {
return WorkerResult.failure(task.getId(), exception.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
@Slf4j
public class WorkerRegistry {
private final Map<String, WorkerHandler> registry=new HashMap<>();

public WorkerRegistry(List<WorkerHandler> handlers){
handlers.forEach(workerHandler -> registry.put(workerHandler.getType(),workerHandler));
handlers.forEach(
workerHandler -> registry.put(workerHandler.getType(),workerHandler));
log.info("Registered workers: {}", registry.keySet());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
public class KafkaConfig {

@Bean
public ConsumerFactory<String, String> consumerFactory(
@Value("${spring.kafka.bootstrap-servers}") String bootstrapServers,
@Value("${spring.kafka.consumer.group-id}") String groupId
) {
public ConsumerFactory<String, String> consumerFactory(@Value("${spring.kafka.bootstrap-servers}") String bootstrapServers, @Value("${spring.kafka.consumer.group-id}") String groupId) {
Map<String, Object> props = new HashMap<>();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
props.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
Expand All @@ -30,11 +27,8 @@ public ConsumerFactory<String, String> consumerFactory(
}

@Bean
public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory(
ConsumerFactory<String, String> consumerFactory
) {
ConcurrentKafkaListenerContainerFactory<String, String> factory =
new ConcurrentKafkaListenerContainerFactory<>();
public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory(ConsumerFactory<String, String> consumerFactory) {
ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory);
return factory;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.flowforge.worker_service.config;

public class RestTemplateConfig {
}
Original file line number Diff line number Diff line change
@@ -1,45 +1,22 @@
package com.flowforge.worker_service.domain.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class WorkerResult {
private String taskId;
private boolean success;
private String message;
public WorkerResult(){}

private WorkerResult(String taskId,boolean success,String message){
this.taskId=taskId;
this.success=success;
this.message=message;
}

public static WorkerResult success(String taskId, String message){
return new WorkerResult(taskId,true,message);
}
public static WorkerResult failure(String taskId, String error) {
return new WorkerResult(taskId, false, error);
}

public String getTaskId() {
return taskId;
}

public void setTaskId(String taskId) {
this.taskId = taskId;
}

public boolean isSuccess() {
return success;
}

public void setSuccess(boolean success) {
this.success = success;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package com.flowforge.worker_service.domain.model;

import com.flowforge.worker_service.domain.enums.TaskStatus;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class WorkerTask {
private String id;
private String workflowExecutionId;
Expand All @@ -15,7 +22,7 @@ public class WorkerTask {
private LocalDateTime createdAt;
private LocalDateTime updatedAt;

public WorkerTask() {}


public WorkerTask(String id, String workflowExecutionId, String nodeId, String type, String payload) {
this.id = id;
Expand Down Expand Up @@ -45,25 +52,4 @@ public void markFailed(String error) {
this.updatedAt = LocalDateTime.now();
}

public String getId() { return id; }
public String getWorkflowExecutionId() { return workflowExecutionId; }
public String getNodeId() { return nodeId; }
public String getType() { return type; }
public String getPayload() { return payload; }
public TaskStatus getStatus() { return status; }
public String getResult() { return result; }
public String getError() { return error; }
public LocalDateTime getCreatedAt() { return createdAt; }
public LocalDateTime getUpdatedAt() { return updatedAt; }

public void setId(String id) { this.id = id; }
public void setWorkflowExecutionId(String workflowExecutionId) { this.workflowExecutionId = workflowExecutionId; }
public void setNodeId(String nodeId) { this.nodeId = nodeId; }
public void setType(String type) { this.type = type; }
public void setPayload(String payload) { this.payload = payload; }
public void setStatus(TaskStatus status) { this.status = status; }
public void setResult(String result) { this.result = result; }
public void setError(String error) { this.error = error; }
public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; }
public void setUpdatedAt(LocalDateTime updatedAt) { this.updatedAt = updatedAt; }
}