Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

just testing threads, do not merge #6559

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@
import software.uncharted.terarium.hmiserver.security.Roles;
import software.uncharted.terarium.hmiserver.service.ClientEventService;
import software.uncharted.terarium.hmiserver.service.CurrentUserService;
import software.uncharted.terarium.hmiserver.service.TaskManager;
import software.uncharted.terarium.hmiserver.service.data.ProjectAssetService;
import software.uncharted.terarium.hmiserver.service.data.ProjectPermissionsService;
import software.uncharted.terarium.hmiserver.service.data.ProjectService;
import software.uncharted.terarium.hmiserver.service.data.WorkflowService;
import software.uncharted.terarium.hmiserver.service.workflowop.*;
import software.uncharted.terarium.hmiserver.utils.Messages;
import software.uncharted.terarium.hmiserver.utils.rebac.ReBACService;
import software.uncharted.terarium.hmiserver.utils.rebac.Schema;
Expand All @@ -56,6 +58,8 @@
@RequiredArgsConstructor
public class WorkflowController {

final TaskManager taskManager;

final WorkflowService workflowService;

final ProjectAssetService projectAssetService;
Expand Down Expand Up @@ -771,6 +775,25 @@ public ResponseEntity<Workflow> removeEdges(
return updated.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
}

final UUID testId = UUID.randomUUID();

@PostMapping("/{id}/abcde-start")
@Secured(Roles.USER)
@Operation(summary = "Add or update a workflow annotation")
public ResponseEntity<String> testABCDE() {
final Runnable testRunnable = new TestOp();
taskManager.submitTask(testId, testRunnable);
return ResponseEntity.ok().build();
}

@PostMapping("/{id}/abcde-cancel")
@Secured(Roles.USER)
@Operation(summary = "Add or update a workflow annotation")
public ResponseEntity<String> testABCDE2() {
taskManager.cancelTask(testId);
return ResponseEntity.ok().build();
}

@PostMapping("/{id}/annotation")
@Secured(Roles.USER)
@Operation(summary = "Add or update a workflow annotation")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package software.uncharted.terarium.hmiserver.service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Future;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class TaskManager {

private ThreadPoolTaskExecutor executor;
private Map<UUID, Future<?>> taskTable = new HashMap();

public TaskManager() {
executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2);
executor.setMaxPoolSize(2);
executor.setQueueCapacity(500);
executor.setThreadNamePrefix("GithubLookup-");
executor.initialize();
}

public void submitTask(final UUID id, Runnable r) {
final Future futureTask = executor.submit(r);
taskTable.put(id, futureTask);
}
YohannParis marked this conversation as resolved.
Show resolved Hide resolved

public void cancelTask(final UUID id) {
final Future futureTask = taskTable.get(id);
if (futureTask == null) return;
if (futureTask.isDone() || futureTask.isCancelled()) {
taskTable.remove(id);
} else {
System.out.println("cancelling task " + id);
futureTask.cancel(true);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package software.uncharted.terarium.hmiserver.service.workflowop;

public abstract class RunnableOp implements Runnable {

public abstract void handleRun() throws InterruptedException;

public abstract void handleInterrupt();

public abstract void handleError();

@Override
public void run() {
try {
handleRun();
} catch (InterruptedException ie) {
handleInterrupt();
} catch (Exception e) {
handleError();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package software.uncharted.terarium.hmiserver.service.workflowop;

public class TestOp extends RunnableOp {

@Override
public void handleRun() throws InterruptedException {
for (int i = 0; i < 20; i++) {
Thread.sleep(2000);
System.out.println(">> " + i);
}
}

@Override
public void handleInterrupt() {
System.out.println("Clean up ........ ");
}

@Override
public void handleError() {
handleInterrupt();
}
}