@systemfsoftware/effect-daemon-cluster 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -27
- package/README.md +44 -3
- package/dist/mod.d.ts +25 -9
- package/dist/mod.mjs +100 -21
- package/dist/rolldown-runtime-D3KUYPsf.mjs +12 -0
- package/package.json +27 -27
- package/dist/rolldown-runtime-D7D4PA-g.mjs +0 -13
package/CHANGELOG.md
CHANGED
|
@@ -1,32 +1,9 @@
|
|
|
1
1
|
# @systemfsoftware/effect-daemon-cluster
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
# @systemfsoftware/effect-daemon-cluster@1.1.0
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Run singletons across processes over Postgres. `ClusterMedium.sqlRunnerLayer` builds `Sharding` over `SqlRunnerStorage` with no-op runner transport, health and message storage, from a caller's `SqlClient` and `ClusterMedium.SqlRunnerSettings`. The oldest live runner owns every shard, so a joining process waits as a standby, and a runner whose sessions are gone leaves at the next assignment poll. The layer fails with `RunnerSessionsUnnamed` unless the client's sessions carry the runner address as `application_name`.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
A singleton whose shard another runner owns is now announced ready at once instead of never, and a run interrupted because its shard moved away keeps the child registered instead of reporting it terminated. Stopping a child with no run in flight reports `Shutdown`.
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
- Updated dependencies:
|
|
12
|
-
- @systemfsoftware/effect-cell-types@12.0.0
|
|
13
|
-
- @systemfsoftware/effect-daemon-conformance@1.0.0
|
|
14
|
-
- @systemfsoftware/effect-daemon-spec@6.0.0
|
|
15
|
-
|
|
16
|
-
## 0.2.1
|
|
17
|
-
|
|
18
|
-
### Patch Changes
|
|
19
|
-
|
|
20
|
-
- These packages now list `@systemfsoftware/effect-cell-types` as a runtime dependency.
|
|
21
|
-
|
|
22
|
-
## 0.2.0
|
|
23
|
-
|
|
24
|
-
### Minor Changes
|
|
25
|
-
|
|
26
|
-
- First release. `ClusterMedium` supervises Effect cluster singletons and entities as children of an `effect-daemon-spec` supervisor: bind it with `ClusterMedium.layer` and declare children with `Supervisor.ChildSpecs.on(ClusterMedium.port)`. A child is ready once it is registered, its death is inferred from failed liveness calls, and a group stop completes on the cluster's own schedule.
|
|
27
|
-
|
|
28
|
-
### Patch Changes
|
|
29
|
-
|
|
30
|
-
- Updated dependencies:
|
|
31
|
-
- @systemfsoftware/effect-daemon-conformance@0.2.0
|
|
32
|
-
- @systemfsoftware/effect-daemon-spec@5.0.0
|
|
9
|
+
Requires Effect ^4.1.2.
|
package/README.md
CHANGED
|
@@ -5,9 +5,7 @@ and single-owner singletons are supervised with inferred death and eventual grou
|
|
|
5
5
|
|
|
6
6
|
## Installation
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
pnpm add @systemfsoftware/effect-daemon-cluster
|
|
10
|
-
```
|
|
8
|
+
This package is not on npm: take `@systemfsoftware/effect-daemon-cluster` from nix.systemfsoftware.com, the package registry for `@systemfsoftware` packages, through a flake input (see the repository README).
|
|
11
9
|
|
|
12
10
|
## Features
|
|
13
11
|
|
|
@@ -19,6 +17,13 @@ pnpm add @systemfsoftware/effect-daemon-cluster
|
|
|
19
17
|
an inferred failure.
|
|
20
18
|
- **The declared loss**: the medium declares `{ reporting: 'inferred', groupStop: 'eventual' }`, so
|
|
21
19
|
the conformance kit holds it only to what it can observe and honour.
|
|
20
|
+
- **Standby and handoff**: a singleton whose shard another runner owns is announced ready at once
|
|
21
|
+
and waits; it starts when the shard arrives. A run interrupted because its shard moved away is a
|
|
22
|
+
handoff, not a termination: the child stays registered and nothing is reported.
|
|
23
|
+
- **Singletons across processes over Postgres**: `ClusterMedium.sqlRunnerLayer` runs `Sharding`
|
|
24
|
+
over `SqlRunnerStorage` with no-op runner transport, health and message storage. The oldest live
|
|
25
|
+
runner owns every shard, so a joining process waits as a standby, and a runner whose sessions are
|
|
26
|
+
gone leaves at the next assignment poll instead of after its heartbeat expires.
|
|
22
27
|
- **Proven, not asserted**: `ClusterMedium.conformanceDriver` runs the whole scenario catalogue
|
|
23
28
|
against the fiber reference.
|
|
24
29
|
|
|
@@ -55,6 +60,42 @@ const program = work.pipe(
|
|
|
55
60
|
declared with `ClusterMedium.entityChild({ entity, entityId, register, probe })`, where `probe` is
|
|
56
61
|
the direct liveness call to the entity.
|
|
57
62
|
|
|
63
|
+
### Singletons across processes
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
import { PgClient } from '@effect/sql-pg'
|
|
67
|
+
import { ClusterMedium } from '@systemfsoftware/effect-daemon-cluster'
|
|
68
|
+
import { Layer, Redacted } from 'effect'
|
|
69
|
+
|
|
70
|
+
const address = { host: `worker-${crypto.randomUUID().slice(0, 8)}`, port: 1 }
|
|
71
|
+
|
|
72
|
+
const ClusterRunner = ClusterMedium.sqlRunnerLayer.pipe(
|
|
73
|
+
Layer.provide(Layer.succeed(ClusterMedium.SqlRunnerSettings, {
|
|
74
|
+
prefix: 'cluster',
|
|
75
|
+
runnerAddress: address,
|
|
76
|
+
sharding: { refreshAssignmentsInterval: '1 second' },
|
|
77
|
+
})),
|
|
78
|
+
Layer.provide(PgClient.layer({
|
|
79
|
+
url: Redacted.make(databaseUrl),
|
|
80
|
+
applicationName: `${address.host}:${address.port}`,
|
|
81
|
+
maxConnections: 2,
|
|
82
|
+
})),
|
|
83
|
+
)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
- The `SqlClient` must open every session with the runner address `host:port` as its
|
|
87
|
+
`application_name`; the layer fails with `RunnerSessionsUnnamed` otherwise. A runner is live while
|
|
88
|
+
any session carries its address, so give each process its own address.
|
|
89
|
+
- The runner and lock tables are `<prefix>_runners` and `<prefix>_locks` (a dotted prefix names a
|
|
90
|
+
schema). The layer never creates them: apply `SqlRunnerStorage.migrations` with an owner role
|
|
91
|
+
first. No message tables are needed.
|
|
92
|
+
- One process uses two sessions: the reserved lock session, whose `idle_session_timeout` is the
|
|
93
|
+
shard lock expiration, and one pooled session for the polls.
|
|
94
|
+
- On a clean stop the owner's singletons end, it unregisters and releases its locks, and the next
|
|
95
|
+
oldest runner starts them at its next poll. A runner whose sessions all end leaves at the next
|
|
96
|
+
poll; a frozen one leaves when its heartbeat and lock session expire (`shardLockExpiration`).
|
|
97
|
+
- Placement is fixed: the live runner with the lowest `machine_id` owns every shard.
|
|
98
|
+
|
|
58
99
|
## Conformance
|
|
59
100
|
|
|
60
101
|
```ts
|
package/dist/mod.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { Conformance } from "@systemfsoftware/effect-daemon-conformance";
|
|
2
|
-
import { Effect, Layer, Scope } from "effect";
|
|
2
|
+
import { Context, Effect, Layer, Schema, Scope } from "effect";
|
|
3
3
|
import { Supervisor } from "@systemfsoftware/effect-daemon-spec";
|
|
4
|
-
import { Sharding } from "effect/cluster";
|
|
5
|
-
|
|
4
|
+
import { Sharding, ShardingConfig } from "effect/cluster";
|
|
5
|
+
import * as SqlClient from "effect/sql/SqlClient";
|
|
6
|
+
import { SqlError } from "effect/sql/SqlError";
|
|
6
7
|
declare const SingletonTag: {
|
|
7
8
|
readonly _tag: 'Singleton';
|
|
8
9
|
};
|
|
@@ -33,11 +34,7 @@ interface EntityChildOptions<R = never> {
|
|
|
33
34
|
}
|
|
34
35
|
declare const singletonChild: <R = never>(options: SingletonChildOptions<R>) => SingletonChild<R>;
|
|
35
36
|
declare const entityChild: <R = never>(options: EntityChildOptions<R>) => EntityChild<R>;
|
|
36
|
-
//#endregion
|
|
37
|
-
//#region src/ClusterMedium/conformance-driver.d.ts
|
|
38
37
|
declare const conformanceDriver: Conformance.ConformanceDriver<ClusterProgram, never, Sharding.Sharding>;
|
|
39
|
-
//#endregion
|
|
40
|
-
//#region src/ClusterMedium/medium.d.ts
|
|
41
38
|
type ClusterMediumRequirement = ClusterProgramRequirements;
|
|
42
39
|
declare const declaration: Supervisor.Medium.MediumDeclaration;
|
|
43
40
|
interface ClusterMediumOptions {
|
|
@@ -47,8 +44,27 @@ declare const mediumFor: (options: ClusterMediumOptions) => Supervisor.Medium.Me
|
|
|
47
44
|
type ClusterMediumPort = Supervisor.Medium.MediumPortShape<ClusterProgram, never, ClusterMediumRequirement>;
|
|
48
45
|
declare const port: import("effect/Context").Service<Supervisor.Medium.MediumPortShape<ClusterProgram, never, ClusterProgramRequirements>, Supervisor.Medium.MediumPortShape<ClusterProgram, never, ClusterProgramRequirements>>;
|
|
49
46
|
declare const layer: (options?: ClusterMediumOptions) => Layer.Layer<ClusterMediumPort>;
|
|
47
|
+
declare const RunnerSessionsUnnamed_base: Schema.Class<RunnerSessionsUnnamed, Schema.TaggedStruct<"RunnerSessionsUnnamed", {
|
|
48
|
+
readonly runnerAddress: Schema.String;
|
|
49
|
+
readonly applicationName: Schema.Option<Schema.String>;
|
|
50
|
+
}>, import("effect/Cause").YieldableError & {
|
|
51
|
+
readonly brand: unique symbol;
|
|
52
|
+
}>;
|
|
53
|
+
declare class RunnerSessionsUnnamed extends RunnerSessionsUnnamed_base {
|
|
54
|
+
get message(): string;
|
|
55
|
+
}
|
|
56
|
+
interface SqlRunnerOptions {
|
|
57
|
+
readonly prefix: string;
|
|
58
|
+
readonly runnerAddress: {
|
|
59
|
+
readonly host: string;
|
|
60
|
+
readonly port: number;
|
|
61
|
+
};
|
|
62
|
+
readonly sharding?: Partial<Omit<ShardingConfig.ShardingConfig['Service'], 'runnerAddress' | 'runnerListenAddress'>>;
|
|
63
|
+
}
|
|
64
|
+
declare const SqlRunnerSettings_base: Context.ServiceClass<SqlRunnerSettings, "@systemfsoftware/effect-daemon-cluster/SqlRunnerSettings", SqlRunnerOptions>;
|
|
65
|
+
declare class SqlRunnerSettings extends SqlRunnerSettings_base {}
|
|
66
|
+
declare const sqlRunnerLayer: Layer.Layer<Sharding.Sharding, RunnerSessionsUnnamed | SqlError, SqlClient.SqlClient | SqlRunnerSettings>;
|
|
50
67
|
declare namespace mod_d_exports {
|
|
51
|
-
export { ClusterMediumOptions, ClusterMediumPort, ClusterMediumRequirement, ClusterProgram, ClusterProgramRequirements, EntityChild, EntityChildOptions, SingletonChild, SingletonChildOptions, conformanceDriver, declaration, entityChild, layer, mediumFor, port, singletonChild };
|
|
68
|
+
export { ClusterMediumOptions, ClusterMediumPort, ClusterMediumRequirement, ClusterProgram, ClusterProgramRequirements, EntityChild, EntityChildOptions, RunnerSessionsUnnamed, SingletonChild, SingletonChildOptions, SqlRunnerOptions, SqlRunnerSettings, conformanceDriver, declaration, entityChild, layer, mediumFor, port, singletonChild, sqlRunnerLayer };
|
|
52
69
|
}
|
|
53
|
-
//#endregion
|
|
54
70
|
export { mod_d_exports as ClusterMedium };
|
package/dist/mod.mjs
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { t as __exportAll } from "./rolldown-runtime-
|
|
1
|
+
import { t as __exportAll } from "./rolldown-runtime-D3KUYPsf.mjs";
|
|
2
2
|
import "@systemfsoftware/effect-daemon-conformance";
|
|
3
|
-
import { Deferred, Duration, Effect, Exit, Layer, Match, Option, Queue, Result, Scope } from "effect";
|
|
3
|
+
import { Array, Cause, Context, Deferred, Duration, Effect, Exit, Layer, Match, Option, Queue, Ref, Result, Schema, Scope } from "effect";
|
|
4
4
|
import { Supervisor } from "@systemfsoftware/effect-daemon-spec";
|
|
5
|
-
import { Sharding } from "effect/cluster";
|
|
5
|
+
import { ClusterError, EntityId, MessageStorage, RunnerAddress, RunnerHealth, RunnerStorage, Runners, Sharding, ShardingConfig, SqlRunnerStorage } from "effect/cluster";
|
|
6
6
|
import { absurd } from "effect/Function";
|
|
7
|
-
|
|
7
|
+
import * as SqlClient from "effect/sql/SqlClient";
|
|
8
|
+
// src/ClusterMedium/ClusterProgram.ts
|
|
8
9
|
const SingletonTag = { _tag: "Singleton" };
|
|
9
10
|
const EntityTag = { _tag: "Entity" };
|
|
10
11
|
const singletonChild = (options) => ({
|
|
@@ -15,19 +16,19 @@ const entityChild = (options) => ({
|
|
|
15
16
|
...EntityTag,
|
|
16
17
|
...options
|
|
17
18
|
});
|
|
18
|
-
|
|
19
|
-
//#region src/ClusterMedium/medium.ts
|
|
19
|
+
// src/ClusterMedium/medium.ts
|
|
20
20
|
const declaration = {
|
|
21
21
|
reporting: "inferred",
|
|
22
22
|
groupStop: "eventual"
|
|
23
23
|
};
|
|
24
24
|
const LIVENESS_PROBE_MILLIS = 1e3;
|
|
25
25
|
const ClusterStartedTypeId = Symbol.for("@systemfsoftware/effect-daemon-cluster/ClusterMedium/Started");
|
|
26
|
-
const clusterStarted = (ready, registration, ended, probe) => ({
|
|
26
|
+
const clusterStarted = (ready, registration, ended, stopping, probe) => ({
|
|
27
27
|
...Supervisor.Medium.started(ready),
|
|
28
28
|
[ClusterStartedTypeId]: ClusterStartedTypeId,
|
|
29
29
|
registration,
|
|
30
30
|
ended,
|
|
31
|
+
stopping,
|
|
31
32
|
probe
|
|
32
33
|
});
|
|
33
34
|
const isClusterStarted = (evidence) => ClusterStartedTypeId in evidence;
|
|
@@ -41,45 +42,55 @@ const inferredDeath = {
|
|
|
41
42
|
failedProbes: 1
|
|
42
43
|
}
|
|
43
44
|
};
|
|
44
|
-
const terminationOf = (exit) => Match.value(Result.match(Supervisor.classifyChildExit(new Supervisor.ClassifyChildExit({
|
|
45
|
-
stopping
|
|
45
|
+
const terminationOf = (stopping, exit) => Match.value(Result.match(Supervisor.classifyChildExit(new Supervisor.ClassifyChildExit({
|
|
46
|
+
stopping,
|
|
46
47
|
exit
|
|
47
48
|
})), {
|
|
48
49
|
onFailure: (error) => absurd(error),
|
|
49
50
|
onSuccess: (decision) => decision
|
|
50
51
|
})).pipe(Match.tag("Normal", () => normalTermination), Match.tag("Shutdown", () => shutdownTermination), Match.tag("Abnormal", () => inferredDeath), Match.exhaustive);
|
|
52
|
+
const lostItsShard = (stopping, exit) => !stopping && Exit.match(exit, {
|
|
53
|
+
onSuccess: () => false,
|
|
54
|
+
onFailure: Cause.hasInterruptsOnly
|
|
55
|
+
});
|
|
56
|
+
const reportedOnExit = (ended, stopping) => (exit) => Effect.flatMap(Ref.get(stopping), (stopped) => lostItsShard(stopped, exit) ? Effect.void : Effect.asVoid(Deferred.succeed(ended, terminationOf(stopped, exit))));
|
|
57
|
+
const ownsSingleton = (sharding, name, shardGroup) => sharding.hasShardId(sharding.getShardId(EntityId.make(name), shardGroup ?? "default"));
|
|
58
|
+
const readyWhen = (owned, signalled) => owned ? Deferred.await(signalled) : Effect.void;
|
|
51
59
|
const startSingleton = (program, shardGroup) => Effect.gen(function* () {
|
|
52
60
|
const evidenceScope = yield* Effect.scope;
|
|
53
61
|
const registration = yield* Scope.fork(evidenceScope);
|
|
54
62
|
const signalled = yield* Deferred.make();
|
|
55
63
|
const ended = yield* Deferred.make();
|
|
64
|
+
const stopping = yield* Ref.make(false);
|
|
56
65
|
const sharding = yield* Sharding.Sharding;
|
|
57
|
-
const run = program.run(Deferred.succeed(signalled, void 0)).pipe(Effect.onExit((
|
|
66
|
+
const run = program.run(Deferred.succeed(signalled, void 0)).pipe(Effect.onExit(reportedOnExit(ended, stopping)));
|
|
58
67
|
yield* sharding.registerSingleton(program.name, run, { shardGroup }).pipe(Effect.provideService(Scope.Scope, registration));
|
|
68
|
+
const ready = readyWhen(ownsSingleton(sharding, program.name, shardGroup), signalled);
|
|
59
69
|
const probe = Deferred.isDone(ended).pipe(Effect.map((done) => done === false));
|
|
60
|
-
return clusterStarted(
|
|
70
|
+
return clusterStarted(ready, registration, ended, stopping, probe);
|
|
61
71
|
});
|
|
62
72
|
const startEntity = (program) => Effect.gen(function* () {
|
|
63
73
|
const evidenceScope = yield* Effect.scope;
|
|
64
74
|
const registration = yield* Scope.fork(evidenceScope);
|
|
65
75
|
const signalled = yield* Deferred.make();
|
|
66
76
|
const ended = yield* Deferred.make();
|
|
77
|
+
const stopping = yield* Ref.make(false);
|
|
67
78
|
const watch = Effect.gen(function* () {
|
|
68
79
|
const exit = yield* Effect.exit(program.register.pipe(Effect.provideService(Scope.Scope, registration)));
|
|
69
80
|
yield* Exit.match(exit, {
|
|
70
81
|
onSuccess: () => Effect.asVoid(Deferred.succeed(signalled, void 0)),
|
|
71
|
-
onFailure: () =>
|
|
82
|
+
onFailure: () => reportedOnExit(ended, stopping)(exit)
|
|
72
83
|
});
|
|
73
84
|
return yield* Effect.never;
|
|
74
85
|
});
|
|
75
86
|
yield* Effect.forkIn(watch, evidenceScope);
|
|
76
87
|
const probe = program.probe.pipe(Effect.timeoutOption(Duration.millis(LIVENESS_PROBE_MILLIS)), Effect.map((answered) => Option.getOrElse(answered, () => false)));
|
|
77
|
-
return clusterStarted(Deferred.await(signalled), registration, ended, probe);
|
|
88
|
+
return clusterStarted(Deferred.await(signalled), registration, ended, stopping, probe);
|
|
78
89
|
});
|
|
79
90
|
const startOf = (program, shardGroup) => Match.value(program).pipe(Match.tag("Singleton", (child) => startSingleton(child, shardGroup)), Match.tag("Entity", (child) => startEntity(child)), Match.exhaustive);
|
|
80
91
|
const reportOf = (evidence) => Option.match(clusterOf(evidence), {
|
|
81
92
|
onNone: () => Effect.succeed(shutdownTermination),
|
|
82
|
-
onSome: (self) => Deferred.await(self.ended)
|
|
93
|
+
onSome: (self) => Deferred.await(self.ended)
|
|
83
94
|
});
|
|
84
95
|
const probeOf = (evidence) => Option.match(clusterOf(evidence), {
|
|
85
96
|
onNone: () => Effect.succeed(false),
|
|
@@ -87,7 +98,7 @@ const probeOf = (evidence) => Option.match(clusterOf(evidence), {
|
|
|
87
98
|
});
|
|
88
99
|
const stopOf = (evidence) => Option.match(clusterOf(evidence), {
|
|
89
100
|
onNone: () => Effect.succeed(Supervisor.Medium.stopped),
|
|
90
|
-
onSome: (self) => Effect.
|
|
101
|
+
onSome: (self) => Ref.set(self.stopping, true).pipe(Effect.andThen(Scope.close(self.registration, Exit.void)), Effect.andThen(Deferred.succeed(self.ended, shutdownTermination)), Effect.as(Supervisor.Medium.stopped))
|
|
91
102
|
});
|
|
92
103
|
const mediumFor = (options) => Supervisor.Medium.make({
|
|
93
104
|
declaration,
|
|
@@ -98,8 +109,7 @@ const mediumFor = (options) => Supervisor.Medium.make({
|
|
|
98
109
|
});
|
|
99
110
|
const port = Supervisor.Medium.MediumPort("ClusterMedium");
|
|
100
111
|
const layer = (options) => Layer.succeed(port, { medium: mediumFor(options ?? {}) });
|
|
101
|
-
|
|
102
|
-
//#region src/ClusterMedium/conformance-driver.ts
|
|
112
|
+
// src/ClusterMedium/conformance-driver.ts
|
|
103
113
|
const consume = (steps, ready) => Effect.flatMap(Queue.take(steps), (step) => continueOf(step, steps, ready));
|
|
104
114
|
const continueOf = (step, steps, ready) => Match.value(step).pipe(Match.tag("BecomeReady", () => Effect.andThen(ready, consume(steps, ready))), Match.tag("ExitNormal", () => Effect.void), Match.tag("ExitAbnormal", () => Effect.die(/* @__PURE__ */ new Error("scripted abnormal exit"))), Match.tag("IgnoreGracefulStop", () => Effect.never), Match.tag("NeverBecomeReady", () => Effect.never), Match.exhaustive);
|
|
105
115
|
const launch = (childId, _script) => Effect.map(Queue.unbounded(), (steps) => ({
|
|
@@ -115,16 +125,85 @@ const conformanceDriver = {
|
|
|
115
125
|
port,
|
|
116
126
|
launch
|
|
117
127
|
};
|
|
118
|
-
|
|
119
|
-
|
|
128
|
+
// src/ClusterMedium/RunnerSessionsUnnamed.schema.ts
|
|
129
|
+
var RunnerSessionsUnnamed = class extends Schema.TaggedError()("RunnerSessionsUnnamed", {
|
|
130
|
+
runnerAddress: Schema.String,
|
|
131
|
+
applicationName: Schema.Option(Schema.String)
|
|
132
|
+
}) {
|
|
133
|
+
get message() {
|
|
134
|
+
const named = Option.getOrElse(this.applicationName, () => "no application_name");
|
|
135
|
+
return `the SqlClient's sessions must carry the runner address ${this.runnerAddress} as application_name, got ${named}`;
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
// src/ClusterMedium/sql-runner.ts
|
|
139
|
+
var SqlRunnerSettings = class extends Context.Service()("@systemfsoftware/effect-daemon-cluster/SqlRunnerSettings") {};
|
|
140
|
+
const addressText = (address) => `${address.host}:${address.port}`;
|
|
141
|
+
const persisted = (effect) => Effect.mapError(effect, (cause) => new ClusterError.PersistenceError({ cause }));
|
|
142
|
+
const sessionsNamed = (address) => Effect.gen(function* () {
|
|
143
|
+
const rows = yield* (yield* SqlClient.SqlClient)`SELECT current_setting('application_name') AS name`;
|
|
144
|
+
const applicationName = Option.map(Array.head(rows), (row) => row.name);
|
|
145
|
+
if (!Option.contains(applicationName, address)) return yield* new RunnerSessionsUnnamed({
|
|
146
|
+
runnerAddress: address,
|
|
147
|
+
applicationName
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
const shardingConfig = Layer.unwrap(Effect.gen(function* () {
|
|
151
|
+
const settings = yield* SqlRunnerSettings;
|
|
152
|
+
const address = RunnerAddress.make(settings.runnerAddress.host, settings.runnerAddress.port);
|
|
153
|
+
return ShardingConfig.layer({
|
|
154
|
+
...settings.sharding,
|
|
155
|
+
runnerAddress: Option.some(address),
|
|
156
|
+
runnerListenAddress: Option.some(address)
|
|
157
|
+
});
|
|
158
|
+
}));
|
|
159
|
+
const sqlStorage = Layer.unwrap(Effect.gen(function* () {
|
|
160
|
+
const settings = yield* SqlRunnerSettings;
|
|
161
|
+
return SqlRunnerStorage.layerStorage({ prefix: settings.prefix });
|
|
162
|
+
}));
|
|
163
|
+
const standbyStorage = Layer.effect(RunnerStorage.RunnerStorage, Effect.gen(function* () {
|
|
164
|
+
const settings = yield* SqlRunnerSettings;
|
|
165
|
+
const self = addressText(settings.runnerAddress);
|
|
166
|
+
yield* sessionsNamed(self);
|
|
167
|
+
const storage = yield* RunnerStorage.RunnerStorage;
|
|
168
|
+
const sql = (yield* SqlClient.SqlClient).withoutTransforms();
|
|
169
|
+
const config = yield* ShardingConfig.ShardingConfig;
|
|
170
|
+
const runners = sql(`${settings.prefix}_runners`);
|
|
171
|
+
const dropDeadRunners = sql`
|
|
172
|
+
DELETE FROM ${runners} r
|
|
173
|
+
WHERE r.last_heartbeat < NOW() - ${Math.ceil(Duration.toSeconds(Duration.fromInputUnsafe(config.shardLockExpiration)))} * INTERVAL '1 second'
|
|
174
|
+
OR NOT EXISTS (SELECT 1 FROM pg_stat_activity a WHERE a.application_name = r.address)
|
|
175
|
+
`;
|
|
176
|
+
const incumbent = sql`
|
|
177
|
+
SELECT address FROM ${runners} WHERE healthy ORDER BY machine_id LIMIT 1
|
|
178
|
+
`;
|
|
179
|
+
const placed = Effect.gen(function* () {
|
|
180
|
+
yield* dropDeadRunners;
|
|
181
|
+
const owner = Option.map(Array.head(yield* incumbent), (row) => row.address);
|
|
182
|
+
const listed = yield* storage.getRunners;
|
|
183
|
+
return Array.map(listed, ([runner]) => [runner, Option.contains(owner, addressText(runner.address))]);
|
|
184
|
+
});
|
|
185
|
+
return RunnerStorage.RunnerStorage.of({
|
|
186
|
+
...storage,
|
|
187
|
+
getRunners: persisted(placed)
|
|
188
|
+
});
|
|
189
|
+
})).pipe(Layer.provide(sqlStorage));
|
|
190
|
+
const noopTransport = Layer.provideMerge(Runners.layerNoop, MessageStorage.layerNoop);
|
|
191
|
+
const sqlRunnerLayer = Sharding.layer.pipe(Layer.provide([
|
|
192
|
+
noopTransport,
|
|
193
|
+
standbyStorage,
|
|
194
|
+
RunnerHealth.layerNoop
|
|
195
|
+
]), Layer.provide(shardingConfig));
|
|
196
|
+
// src/ClusterMedium/mod.ts
|
|
120
197
|
var ClusterMedium_exports = /* @__PURE__ */ __exportAll({
|
|
198
|
+
RunnerSessionsUnnamed: () => RunnerSessionsUnnamed,
|
|
199
|
+
SqlRunnerSettings: () => SqlRunnerSettings,
|
|
121
200
|
conformanceDriver: () => conformanceDriver,
|
|
122
201
|
declaration: () => declaration,
|
|
123
202
|
entityChild: () => entityChild,
|
|
124
203
|
layer: () => layer,
|
|
125
204
|
mediumFor: () => mediumFor,
|
|
126
205
|
port: () => port,
|
|
127
|
-
singletonChild: () => singletonChild
|
|
206
|
+
singletonChild: () => singletonChild,
|
|
207
|
+
sqlRunnerLayer: () => sqlRunnerLayer
|
|
128
208
|
});
|
|
129
|
-
//#endregion
|
|
130
209
|
export { ClusterMedium_exports as ClusterMedium };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// \0rolldown/runtime.js
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __exportAll = (all, flags) => {
|
|
4
|
+
let target = Object.setPrototypeOf({}, null);
|
|
5
|
+
for (var name in all) __defProp(target, name, {
|
|
6
|
+
get: all[name],
|
|
7
|
+
enumerable: true
|
|
8
|
+
});
|
|
9
|
+
if (!(flags & 1)) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
10
|
+
return flags & 2 ? target : Object.preventExtensions(target);
|
|
11
|
+
};
|
|
12
|
+
export { __exportAll as t };
|
package/package.json
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@systemfsoftware/effect-daemon-cluster",
|
|
3
3
|
"license": "Apache-2.0",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.1.0",
|
|
5
5
|
"author": "Ryan Lee <drdgvhbh@gmail.com>",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
|
-
"url": "git+https://github.com/systemfsoftware/
|
|
9
|
-
"directory": "packages/
|
|
8
|
+
"url": "git+https://github.com/systemfsoftware/effect-daemon.git",
|
|
9
|
+
"directory": "packages/effect-daemon-cluster"
|
|
10
10
|
},
|
|
11
|
-
"homepage": "https://github.com/systemfsoftware/
|
|
12
|
-
"bugs": "https://github.com/systemfsoftware/
|
|
11
|
+
"homepage": "https://github.com/systemfsoftware/effect-daemon/tree/main/packages/effect-daemon-cluster#readme",
|
|
12
|
+
"bugs": "https://github.com/systemfsoftware/effect-daemon/issues",
|
|
13
13
|
"description": "The effect-daemon-spec medium over effect/cluster — entities and single-owner singletons supervised with inferred death and eventual group stop.",
|
|
14
14
|
"keywords": [
|
|
15
15
|
"effect",
|
|
@@ -35,34 +35,34 @@
|
|
|
35
35
|
"./package.json": "./package.json"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@systemfsoftware/effect-cell-types": "^12.0.
|
|
39
|
-
"@systemfsoftware/effect-daemon-conformance": "^1.0.
|
|
40
|
-
"@systemfsoftware/effect-daemon-spec": "^6.0.
|
|
38
|
+
"@systemfsoftware/effect-cell-types": "^12.0.2",
|
|
39
|
+
"@systemfsoftware/effect-daemon-conformance": "^1.0.4",
|
|
40
|
+
"@systemfsoftware/effect-daemon-spec": "^6.0.4"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@effect/platform-node": "
|
|
44
|
-
"@effect/sql-pglite": "
|
|
45
|
-
"@microsoft/api-extractor": "^7.59.
|
|
43
|
+
"@effect/platform-node": "4.1.2",
|
|
44
|
+
"@effect/sql-pglite": "4.1.2",
|
|
45
|
+
"@microsoft/api-extractor": "^7.59.4",
|
|
46
46
|
"@systemfsoftware/arethetypeswrong-cli": "^4.2.0",
|
|
47
|
-
"@systemfsoftware/conformance-spec": "^1.0.
|
|
48
|
-
"@systemfsoftware/effect-gherkin-spec": "^
|
|
49
|
-
"@systemfsoftware/effect-schema-law": "^
|
|
50
|
-
"@systemfsoftware/effect-schema-vite": "^3.
|
|
51
|
-
"@systemfsoftware/oxlint-config-recommended": "^
|
|
52
|
-
"@systemfsoftware/tsconfig": "^2.0.
|
|
53
|
-
"@systemfsoftware/tsdown-config": "^0.1.
|
|
54
|
-
"@systemfsoftware/vitest": "^
|
|
55
|
-
"@systemfsoftware/vitest-config": "^0.1.
|
|
56
|
-
"@types/node": "^26",
|
|
57
|
-
"effect": "
|
|
58
|
-
"oxlint": "~1.
|
|
47
|
+
"@systemfsoftware/conformance-spec": "^1.0.1",
|
|
48
|
+
"@systemfsoftware/effect-gherkin-spec": "^8.0.0",
|
|
49
|
+
"@systemfsoftware/effect-schema-law": "^6.1.0",
|
|
50
|
+
"@systemfsoftware/effect-schema-vite": "^3.1.1",
|
|
51
|
+
"@systemfsoftware/oxlint-config-recommended": "^5.0.0",
|
|
52
|
+
"@systemfsoftware/tsconfig": "^2.0.2",
|
|
53
|
+
"@systemfsoftware/tsdown-config": "^0.1.1",
|
|
54
|
+
"@systemfsoftware/vitest": "^3.0.0",
|
|
55
|
+
"@systemfsoftware/vitest-config": "^0.1.1",
|
|
56
|
+
"@types/node": "^26.6.5",
|
|
57
|
+
"effect": "4.1.2",
|
|
58
|
+
"oxlint": "~1.90.0",
|
|
59
59
|
"rimraf": "^6.1.3",
|
|
60
60
|
"tsdown": "^0.23.0",
|
|
61
|
-
"typescript": "^7",
|
|
62
|
-
"vitest": "^5"
|
|
61
|
+
"typescript": "^7.0.2",
|
|
62
|
+
"vitest": "^5.0.5"
|
|
63
63
|
},
|
|
64
64
|
"peerDependencies": {
|
|
65
|
-
"effect": "^4",
|
|
65
|
+
"effect": "^4.1.2",
|
|
66
66
|
"typescript": ">=7.0.0"
|
|
67
67
|
},
|
|
68
68
|
"publishConfig": {
|
|
@@ -73,7 +73,7 @@
|
|
|
73
73
|
"clean": "rimraf dist",
|
|
74
74
|
"build": "tsdown -l warn && pnpm api:check",
|
|
75
75
|
"typecheck": "tsc -b",
|
|
76
|
-
"test": "vitest run --project unit
|
|
76
|
+
"test": "vitest run --project unit",
|
|
77
77
|
"lint": "f=${OXLINT_FORMAT:-${AGENT:+agent}}; oxlint . --format=${f:-default}",
|
|
78
78
|
"attw": "attw --pack .",
|
|
79
79
|
"api:check": "api-extractor-quiet run",
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
//#region \0rolldown/runtime.js
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __exportAll = (all, no_symbols) => {
|
|
4
|
-
let target = {};
|
|
5
|
-
for (var name in all) __defProp(target, name, {
|
|
6
|
-
get: all[name],
|
|
7
|
-
enumerable: true
|
|
8
|
-
});
|
|
9
|
-
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
10
|
-
return target;
|
|
11
|
-
};
|
|
12
|
-
//#endregion
|
|
13
|
-
export { __exportAll as t };
|