Skip to content
Closed
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
60 changes: 60 additions & 0 deletions apps/server/src/cloud/ManagedEndpointRuntime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import * as RelayClient from "@t3tools/shared/relayClient";

import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
import * as ManagedEndpointRuntime from "./ManagedEndpointRuntime.ts";
import * as RelayClientLedger from "./RelayClientLedger.ts";

const layerRelayClientAvailable = Layer.succeed(
RelayClient.RelayClient,
Expand Down Expand Up @@ -528,6 +529,65 @@ describe("CloudManagedEndpointRuntime", () => {
}),
);

it.effect.each([{ ledger: "without" }, { ledger: "with" }])(
"starts and stops the connector the same way $ledger a relay client ledger",
({ ledger }) =>
Effect.gen(function* () {
const events: Array<string> = [];
const spawner = ChildProcessSpawner.make(() =>
Effect.gen(function* () {
const handle = makeHandle({
pid: 500,
onKill: () => {
events.push("kill:500");
},
});
yield* Effect.addFinalizer(() => handle.kill().pipe(Effect.ignore));
return handle;
}),
);
const layerRuntime = ManagedEndpointRuntime.layer.pipe(
Layer.provide(runtimeDependencies(spawner)),
);
const context = yield* Layer.build(
ledger === "without"
? layerRuntime
: layerRuntime.pipe(
Layer.provide(
Layer.succeed(
RelayClientLedger.RelayClientLedger,
RelayClientLedger.RelayClientLedger.of({
track: ({ pid }) =>
Effect.sync(() => {
events.push(`track:${pid}`);
return Effect.sync(() => {
events.push(`forget:${pid}`);
});
}),
}),
),
),
),
);
const runtime = yield* Effect.service(
ManagedEndpointRuntime.CloudManagedEndpointRuntime,
).pipe(Effect.provide(context));

const started = yield* runtime.applyConfig({
providerKind: "cloudflare_tunnel",
connectorToken: "token",
});
const stopped = yield* runtime.applyConfig(null);

expect(started).toMatchObject({ status: "running", pid: 500 });
expect(stopped).toEqual({ status: "disabled" });
// The record outlives the client: it is forgotten only after the kill.
expect(events).toEqual(
ledger === "without" ? ["kill:500"] : ["track:500", "kill:500", "forget:500"],
);
}),
);

it.effect("supervises the active connector and restarts it after process exit", () =>
Effect.gen(function* () {
const spawned: Array<number> = [];
Expand Down
12 changes: 12 additions & 0 deletions apps/server/src/cloud/ManagedEndpointRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { HttpClient, HttpClientResponse } from "effect/http";
import * as ChildProcess from "effect/process/ChildProcess";
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";

import * as RelayClientLedger from "./RelayClientLedger.ts";

export type CloudManagedEndpointRuntimeStatus =
| {
readonly status: "disabled";
Expand Down Expand Up @@ -177,6 +179,7 @@ const stopConnector = (connector: ActiveConnector | null) =>
export const make = Effect.gen(function* () {
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const relayClient = yield* RelayClient.RelayClient;
const relayClientLedger = yield* Effect.serviceOption(RelayClientLedger.RelayClientLedger);
const httpClient = HttpClient.withScope(yield* HttpClient.HttpClient);
const activeRef = yield* Ref.make<ActiveConnector | null>(null);
const desiredConfigRef = yield* Ref.make<RelayManagedEndpointRuntimeConfig | null>(null);
Expand Down Expand Up @@ -602,6 +605,12 @@ export const make = Effect.gen(function* () {

const http2 = yield* Ref.get(http2Ref);
const connectorScope = yield* Scope.make("sequential");
// Finalizers run in reverse order; forget only after the spawner stops the client.
let forgetRelayClient: Effect.Effect<void> = Effect.void;
yield* Scope.addFinalizer(
connectorScope,
Effect.suspend(() => forgetRelayClient),
);
const child = yield* spawner
.spawn(
ChildProcess.make(
Expand Down Expand Up @@ -666,6 +675,9 @@ export const make = Effect.gen(function* () {
}

if (!("status" in child)) {
if (Option.isSome(relayClientLedger)) {
forgetRelayClient = yield* relayClientLedger.value.track({ pid: Number(child.pid) });
}
const connector = {
child,
executable,
Expand Down
Loading
Loading