compose real headless runtime with task build and lifecycle
This commit is contained in:
@@ -121,22 +121,29 @@ public final class DefaultTaskTools implements TaskTools {
|
||||
handle = session.submit(data.task, "headless " + action, () -> {
|
||||
HeadlessReply result;
|
||||
if (action == HeadlessAction.BOOT) {
|
||||
if (data.runtime != null) throw failure(AutomationFailure.Code.BUSY, "A headless session is already active");
|
||||
if (data.runtime != null && !data.reset) throw failure(AutomationFailure.Code.BUSY, "A headless session is already active");
|
||||
var build = buildId == null ? null : data.builds.get(buildId);
|
||||
if (build == null) throw failure(AutomationFailure.Code.BUILD_NOT_FOUND, "No successful current task build with this ID");
|
||||
var port = gateway.orElseThrow(() -> failure(AutomationFailure.Code.RUNTIME_UNAVAILABLE, "Headless process adapter is not composed"));
|
||||
var runtime = port.boot(catalog.openProject(data.task.projectRoot()), buildId, build);
|
||||
try { session.attachRuntime(data.task, runtime); }
|
||||
catch (RuntimeException denied) { runtime.cancel(); runtime.shutdown().toCompletableFuture().join(); throw denied; }
|
||||
data.runtime = runtime;
|
||||
result = new HeadlessReply(Map.of("sessionId", runtime.sessionId(), "buildId", buildId), null);
|
||||
if (data.runtime != null) {
|
||||
if (!buildId.equals(data.runtimeBuildId)) throw failure(AutomationFailure.Code.BUILD_NOT_FOUND, "Reset session belongs to another build");
|
||||
result = data.runtime.call(HeadlessAction.BOOT, Map.of());
|
||||
} else {
|
||||
var port = gateway.orElseThrow(() -> failure(AutomationFailure.Code.RUNTIME_UNAVAILABLE, "Headless process adapter is not composed"));
|
||||
var runtime = port.boot(catalog.openProject(data.task.projectRoot()), buildId, build);
|
||||
try { session.attachRuntime(data.task, runtime); }
|
||||
catch (RuntimeException denied) { runtime.cancel(); runtime.shutdown().toCompletableFuture().join(); throw denied; }
|
||||
data.runtime = runtime;
|
||||
data.runtimeBuildId = buildId;
|
||||
result = new HeadlessReply(Map.of("sessionId", runtime.sessionId(), "buildId", buildId), null);
|
||||
}
|
||||
data.reset = false;
|
||||
} else if (action == HeadlessAction.SHUTDOWN) {
|
||||
String sessionId = requireRuntime(data).sessionId();
|
||||
stopRuntime(data);
|
||||
result = new HeadlessReply(Map.of("sessionId", sessionId), null);
|
||||
} else {
|
||||
result = requireRuntime(data).call(action, input);
|
||||
if (action == HeadlessAction.RESET) session.markCaptureStale();
|
||||
if (action == HeadlessAction.RESET) { data.reset = true; session.markCaptureStale(); }
|
||||
}
|
||||
reply.set(result);
|
||||
return new AutomationOutcome(AutomationOutcome.Kind.SUCCEEDED, 0, 0, List.of(), result.capture());
|
||||
@@ -150,6 +157,7 @@ public final class DefaultTaskTools implements TaskTools {
|
||||
handle.cancel(); Thread.currentThread().interrupt();
|
||||
throw failure(AutomationFailure.Code.INFRASTRUCTURE_FAILURE, "Headless call interrupted");
|
||||
} catch (ExecutionException error) {
|
||||
handle.termination().toCompletableFuture().join();
|
||||
if (error.getCause() instanceof RuntimeException cause) throw cause;
|
||||
throw failure(AutomationFailure.Code.INFRASTRUCTURE_FAILURE, "Headless call failed");
|
||||
} catch (CancellationException error) {
|
||||
@@ -164,6 +172,8 @@ public final class DefaultTaskTools implements TaskTools {
|
||||
runtime.shutdown().toCompletableFuture().join();
|
||||
data.session.detachRuntime(data.task, runtime);
|
||||
data.runtime = null;
|
||||
data.runtimeBuildId = null;
|
||||
data.reset = false;
|
||||
}
|
||||
|
||||
private TaskData data(AutomationSession session, String id) {
|
||||
@@ -229,6 +239,8 @@ public final class DefaultTaskTools implements TaskTools {
|
||||
final Map<String, Record> requests = new HashMap<>(), operations = new HashMap<>();
|
||||
final Map<String, ShipperPrepareResult> builds = new ConcurrentHashMap<>();
|
||||
volatile HeadlessGateway.Session runtime;
|
||||
String runtimeBuildId;
|
||||
boolean reset;
|
||||
TaskData(AutomationSession session, TaskIdentity task) { this.session = session; this.task = task; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user