@systemfsoftware/effect-daemon-spec 5.0.1 → 5.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 +6 -0
- package/dist/index.d.ts +481 -432
- package/dist/index.mjs +382 -370
- package/package.json +7 -7
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
|
|
2
|
-
import { Array, Cause, Clock, Context, Deferred, Duration, Effect, Exit, Fiber, HashMap, Layer, Match, Number as Number$1, Option, Predicate, PubSub, Queue, Ref, Result, Schema, Scope } from "effect";
|
|
3
2
|
import { Handle, Sandwich, Workflow } from "@systemfsoftware/effect-cell-types";
|
|
4
|
-
import {
|
|
3
|
+
import { Array, Cause, Clock, Context, Deferred, Duration, Effect, Exit, Fiber, HashMap, Layer, Match, Number as Number$1, Option, Predicate, PubSub, Queue, Ref, Result, Schema, Scope } from "effect";
|
|
4
|
+
import * as Result$1 from "effect/Result";
|
|
5
|
+
import { absurd, dual } from "effect/Function";
|
|
5
6
|
import * as Stream from "effect/Stream";
|
|
6
7
|
import { Prototype } from "effect/Pipeable";
|
|
7
8
|
//#region src/kernel/SupervisionLimits.schema.ts
|
|
@@ -54,6 +55,99 @@ const ChildId = Schema.String;
|
|
|
54
55
|
const RequestId = Schema.String;
|
|
55
56
|
Schema.decodeResult(Ceiling);
|
|
56
57
|
//#endregion
|
|
58
|
+
//#region src/kernel/TerminationReport.schema.ts
|
|
59
|
+
/**
|
|
60
|
+
* What a medium can say about why a child died (R16): a fiber supplies a cause, a process
|
|
61
|
+
* supplies an exit status and signal, and a remote runner's death is inferred. No event
|
|
62
|
+
* claims a level its medium cannot supply — the medium declares which one it reports, and
|
|
63
|
+
* the kernel assumes nothing beyond that declaration.
|
|
64
|
+
*/
|
|
65
|
+
const CauseReport = Schema.TaggedStruct("CauseReport", { cause: Schema.String });
|
|
66
|
+
const ExitReport = Schema.TaggedStruct("ExitReport", {
|
|
67
|
+
code: Schema.Int,
|
|
68
|
+
signal: Schema.String
|
|
69
|
+
});
|
|
70
|
+
const InferredReport = Schema.TaggedStruct("InferredReport", { failedProbes: ProbeThreshold });
|
|
71
|
+
const DeadlineMissedReport = Schema.TaggedStruct("DeadlineMissed", { waitedMillis: Schema.Int });
|
|
72
|
+
const FailureReport = Schema.Union([
|
|
73
|
+
CauseReport,
|
|
74
|
+
ExitReport,
|
|
75
|
+
InferredReport,
|
|
76
|
+
DeadlineMissedReport
|
|
77
|
+
]);
|
|
78
|
+
const NormalTermination = Schema.TaggedStruct("Normal", {});
|
|
79
|
+
const ShutdownTermination = Schema.TaggedStruct("Shutdown", {});
|
|
80
|
+
const AbnormalTermination = Schema.TaggedStruct("Abnormal", { report: FailureReport });
|
|
81
|
+
const TerminationReason = Schema.Union([
|
|
82
|
+
NormalTermination,
|
|
83
|
+
ShutdownTermination,
|
|
84
|
+
AbnormalTermination
|
|
85
|
+
]);
|
|
86
|
+
/**
|
|
87
|
+
* Why the supervisor itself is terminating: a shutdown something asked of it, or a
|
|
88
|
+
* give-up after its children terminated more often than its intensity allows. The
|
|
89
|
+
* decision and command data carry this, so the shell never infers which one it is.
|
|
90
|
+
* A give-up names the child incarnation whose termination exhausted the intensity.
|
|
91
|
+
*/
|
|
92
|
+
const RequestedExit = Schema.TaggedStruct("RequestedExit", {});
|
|
93
|
+
const IntensityExceededExit = Schema.TaggedStruct("IntensityExceededExit", {
|
|
94
|
+
childId: ChildId,
|
|
95
|
+
generation: Generation
|
|
96
|
+
});
|
|
97
|
+
const SupervisorExit = Schema.Union([RequestedExit, IntensityExceededExit]);
|
|
98
|
+
const abnormalReasonOfCause = (cause) => ({
|
|
99
|
+
_tag: "Abnormal",
|
|
100
|
+
report: {
|
|
101
|
+
_tag: "CauseReport",
|
|
102
|
+
cause: Cause.pretty(cause)
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
const terminationReasonOfCause = (cause) => Option.getOrElse(Cause.findErrorOption(cause), () => abnormalReasonOfCause(cause));
|
|
106
|
+
//#endregion
|
|
107
|
+
//#region src/Supervisor/ChildExitDecision.schema.ts
|
|
108
|
+
const ChildExitTypeId = Symbol.for("@systemfsoftware/effect-daemon-spec/ChildExitDecision");
|
|
109
|
+
/** The child ended on its own success. */
|
|
110
|
+
var NormalExit = class extends Schema.TaggedClass()("Normal", {}) {
|
|
111
|
+
[ChildExitTypeId] = ChildExitTypeId;
|
|
112
|
+
};
|
|
113
|
+
/** The child was interrupted — an owned shutdown, or one it raised itself. */
|
|
114
|
+
var ShutdownExit = class extends Schema.TaggedClass()("Shutdown", {}) {
|
|
115
|
+
[ChildExitTypeId] = ChildExitTypeId;
|
|
116
|
+
};
|
|
117
|
+
/** The child failed with something other than an interruption. */
|
|
118
|
+
var AbnormalExit = class extends Schema.TaggedClass()("Abnormal", {}) {
|
|
119
|
+
[ChildExitTypeId] = ChildExitTypeId;
|
|
120
|
+
};
|
|
121
|
+
const ChildExitDecision = Schema.Union([
|
|
122
|
+
NormalExit,
|
|
123
|
+
ShutdownExit,
|
|
124
|
+
AbnormalExit
|
|
125
|
+
]);
|
|
126
|
+
const NORMAL = { _tag: "Normal" };
|
|
127
|
+
const SHUTDOWN = { _tag: "Shutdown" };
|
|
128
|
+
const causeOf = (exit) => exit.pipe(Exit.getCause, Option.getOrElse(() => Cause.empty));
|
|
129
|
+
const terminationReasonOf = (bearing) => Match.value(bearing.decision).pipe(Match.tag("Normal", () => NORMAL), Match.tag("Shutdown", () => SHUTDOWN), Match.tag("Abnormal", () => bearing.exit.pipe(causeOf, abnormalReasonOfCause)), Match.exhaustive);
|
|
130
|
+
//#endregion
|
|
131
|
+
//#region src/Supervisor/classify-child-exit.workflow.ts
|
|
132
|
+
var ClassifyChildExit = class extends Schema.TaggedClass()("ClassifyChildExit", {
|
|
133
|
+
stopping: Schema.Boolean,
|
|
134
|
+
exit: Schema.Exit(Schema.Void, Schema.Unknown, Schema.Unknown)
|
|
135
|
+
}) {
|
|
136
|
+
static [Workflow.InstrumentationBrand] = {};
|
|
137
|
+
};
|
|
138
|
+
const failureOf = (cause) => Match.value(Cause.hasInterruptsOnly(cause)).pipe(Match.when(true, () => ShutdownExit.make()), Match.when(false, () => AbnormalExit.make()), Match.exhaustive);
|
|
139
|
+
const endedOf = (exit) => Exit.match(exit, {
|
|
140
|
+
onSuccess: () => NormalExit.make(),
|
|
141
|
+
onFailure: failureOf
|
|
142
|
+
});
|
|
143
|
+
const decisionOf$1 = (command) => Match.value(command.stopping).pipe(Match.when(true, () => ShutdownExit.make()), Match.when(false, () => endedOf(command.exit)), Match.exhaustive);
|
|
144
|
+
const classifyChildExit = Workflow.make({
|
|
145
|
+
command: ClassifyChildExit,
|
|
146
|
+
decision: ChildExitDecision,
|
|
147
|
+
error: Schema.Never,
|
|
148
|
+
decide: (command) => Result$1.succeed(decisionOf$1(command))
|
|
149
|
+
});
|
|
150
|
+
//#endregion
|
|
57
151
|
//#region src/kernel/SupervisorPolicy.schema.ts
|
|
58
152
|
const RestartStrategy = Schema.Literals([
|
|
59
153
|
"one_for_one",
|
|
@@ -149,47 +243,26 @@ Schema.Struct({
|
|
|
149
243
|
childSeeds: Schema.Array(ChildSeed)
|
|
150
244
|
});
|
|
151
245
|
const SupervisionPolicy = Schema.Struct(policyFields).check(Schema.makeFilter(policyIsDecodable, { message: POLICY_REFUSAL_MESSAGE }));
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
const InferredReport = Schema.TaggedStruct("InferredReport", { failedProbes: ProbeThreshold });
|
|
166
|
-
const DeadlineMissedReport = Schema.TaggedStruct("DeadlineMissed", { waitedMillis: Schema.Int });
|
|
167
|
-
const FailureReport = Schema.Union([
|
|
168
|
-
CauseReport,
|
|
169
|
-
ExitReport,
|
|
170
|
-
InferredReport,
|
|
171
|
-
DeadlineMissedReport
|
|
172
|
-
]);
|
|
173
|
-
const NormalTermination = Schema.TaggedStruct("Normal", {});
|
|
174
|
-
const ShutdownTermination = Schema.TaggedStruct("Shutdown", {});
|
|
175
|
-
const AbnormalTermination = Schema.TaggedStruct("Abnormal", { report: FailureReport });
|
|
176
|
-
const TerminationReason = Schema.Union([
|
|
177
|
-
NormalTermination,
|
|
178
|
-
ShutdownTermination,
|
|
179
|
-
AbnormalTermination
|
|
180
|
-
]);
|
|
181
|
-
/**
|
|
182
|
-
* Why the supervisor itself is terminating: a shutdown something asked of it, or a
|
|
183
|
-
* give-up after its children terminated more often than its intensity allows. The
|
|
184
|
-
* decision and command data carry this, so the shell never infers which one it is.
|
|
185
|
-
* A give-up names the child incarnation whose termination exhausted the intensity.
|
|
186
|
-
*/
|
|
187
|
-
const RequestedExit = Schema.TaggedStruct("RequestedExit", {});
|
|
188
|
-
const IntensityExceededExit = Schema.TaggedStruct("IntensityExceededExit", {
|
|
189
|
-
childId: ChildId,
|
|
190
|
-
generation: Generation
|
|
246
|
+
const supervisionPolicyOf = (parts) => ({
|
|
247
|
+
strategy: parts.strategy,
|
|
248
|
+
intensity: parts.intensity,
|
|
249
|
+
periodMillis: parts.periodMillis,
|
|
250
|
+
autoShutdown: parts.autoShutdown,
|
|
251
|
+
coolDown: Match.value(parts.coolDownMillis).pipe(Match.when(void 0, () => ({ _tag: "NoCoolDown" })), Match.orElse((millis) => ({
|
|
252
|
+
_tag: "CoolDownAfter",
|
|
253
|
+
millis
|
|
254
|
+
}))),
|
|
255
|
+
backoff: parts.backoff,
|
|
256
|
+
dynamic: parts.dynamic,
|
|
257
|
+
livenessTickMillis: parts.livenessTickMillis,
|
|
258
|
+
childDeclarations: parts.declarations
|
|
191
259
|
});
|
|
192
|
-
const
|
|
260
|
+
const workerShutdown = {
|
|
261
|
+
_tag: "Graceful",
|
|
262
|
+
millis: 5e3
|
|
263
|
+
};
|
|
264
|
+
const supervisorShutdown = { _tag: "Infinity" };
|
|
265
|
+
const childShutdownOf = (nested) => Match.value(nested).pipe(Match.when(true, () => supervisorShutdown), Match.when(false, () => workerShutdown), Match.exhaustive);
|
|
193
266
|
//#endregion
|
|
194
267
|
//#region src/Supervisor/MediumDeclaration.schema.ts
|
|
195
268
|
/**
|
|
@@ -286,18 +359,17 @@ const fiberStarted = (fiber, scope, ready) => ({
|
|
|
286
359
|
});
|
|
287
360
|
const isFiberStarted = (evidence) => FiberStartedTypeId in evidence;
|
|
288
361
|
const fiberOf = (evidence) => Option.liftPredicate(evidence, isFiberStarted);
|
|
289
|
-
const normalTermination = { _tag: "Normal" };
|
|
290
362
|
const shutdownTermination = { _tag: "Shutdown" };
|
|
291
|
-
const
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
363
|
+
const decisionOf = (exit) => Result.match(classifyChildExit(new ClassifyChildExit({
|
|
364
|
+
stopping: false,
|
|
365
|
+
exit
|
|
366
|
+
})), {
|
|
367
|
+
onFailure: (error) => absurd(error),
|
|
368
|
+
onSuccess: (decision) => decision
|
|
297
369
|
});
|
|
298
|
-
const terminationOf$1 = (exit) =>
|
|
299
|
-
|
|
300
|
-
|
|
370
|
+
const terminationOf$1 = (exit) => terminationReasonOf({
|
|
371
|
+
decision: decisionOf(exit),
|
|
372
|
+
exit
|
|
301
373
|
});
|
|
302
374
|
const closedChild = (self) => Scope.close(self.scope, Exit.void);
|
|
303
375
|
const forcedChild = (self) => Fiber.interrupt(self.fiber);
|
|
@@ -336,20 +408,7 @@ const mediumFor = () => make$1({
|
|
|
336
408
|
const medium = mediumFor();
|
|
337
409
|
//#endregion
|
|
338
410
|
//#region src/Supervisor/bound-child.ts
|
|
339
|
-
const rendered = (failure) => (
|
|
340
|
-
_tag: "Abnormal",
|
|
341
|
-
report: {
|
|
342
|
-
_tag: "CauseReport",
|
|
343
|
-
cause: Cause.pretty(Cause.fail(failure))
|
|
344
|
-
}
|
|
345
|
-
});
|
|
346
|
-
const reasonOf = (cause) => Option.getOrElse(Cause.findErrorOption(cause), () => ({
|
|
347
|
-
_tag: "Abnormal",
|
|
348
|
-
report: {
|
|
349
|
-
_tag: "CauseReport",
|
|
350
|
-
cause: Cause.pretty(cause)
|
|
351
|
-
}
|
|
352
|
-
}));
|
|
411
|
+
const rendered = (failure) => abnormalReasonOfCause(Cause.fail(failure));
|
|
353
412
|
const startOf = (program, medium, context) => Effect.gen(function* () {
|
|
354
413
|
const parent = yield* Effect.scope;
|
|
355
414
|
const child = yield* Scope.fork(parent);
|
|
@@ -357,7 +416,7 @@ const startOf = (program, medium, context) => Effect.gen(function* () {
|
|
|
357
416
|
const outcome = yield* Effect.exit(Effect.setContext(Effect.mapError(medium.start(program), rendered), scoped));
|
|
358
417
|
return yield* Exit.match(outcome, {
|
|
359
418
|
onSuccess: (evidence) => Effect.succeed(evidence),
|
|
360
|
-
onFailure: (cause) => Effect.andThen(Scope.close(child, Exit.void), Effect.fail(
|
|
419
|
+
onFailure: (cause) => Effect.andThen(Scope.close(child, Exit.void), Effect.fail(terminationReasonOfCause(cause)))
|
|
361
420
|
});
|
|
362
421
|
});
|
|
363
422
|
const bind = (program, medium, context) => ({
|
|
@@ -374,6 +433,25 @@ const bind = (program, medium, context) => ({
|
|
|
374
433
|
*/
|
|
375
434
|
const Binder = { bind };
|
|
376
435
|
//#endregion
|
|
436
|
+
//#region src/Supervisor/DynamicOutcome.schema.ts
|
|
437
|
+
const DynamicStartAccepted = Schema.Struct({
|
|
438
|
+
outcome: Schema.Literal("accepted"),
|
|
439
|
+
childId: ChildId,
|
|
440
|
+
generation: Generation
|
|
441
|
+
});
|
|
442
|
+
const DynamicStartRefused = Schema.Struct({ outcome: Schema.Literal("refused") });
|
|
443
|
+
const DynamicStartOutcome = Schema.Union([DynamicStartAccepted, DynamicStartRefused]);
|
|
444
|
+
const DynamicStopDone = Schema.Struct({ outcome: Schema.Literal("stopped") });
|
|
445
|
+
const DynamicStopMissed = Schema.Struct({ outcome: Schema.Literal("missed") });
|
|
446
|
+
const DynamicStopOutcome = Schema.Union([DynamicStopDone, DynamicStopMissed]);
|
|
447
|
+
Schema.Union([DynamicStartOutcome, DynamicStopOutcome]);
|
|
448
|
+
const outcomeOf = (reply) => Match.value(reply).pipe(Match.tag("ReplyStartAccepted", (accepted) => ({
|
|
449
|
+
outcome: "accepted",
|
|
450
|
+
childId: accepted.childId,
|
|
451
|
+
generation: accepted.generation
|
|
452
|
+
})), Match.tag("ReplyStartRefused", () => ({ outcome: "refused" })), Match.tag("ReplyStopped", () => ({ outcome: "stopped" })), Match.exhaustive);
|
|
453
|
+
const staleOf = (event) => Match.value(event).pipe(Match.when({ _tag: "DynamicStartRequested" }, () => ({ outcome: "refused" })), Match.orElse(() => ({ outcome: "missed" })));
|
|
454
|
+
//#endregion
|
|
377
455
|
//#region src/Supervisor/running-supervisor.handle.ts
|
|
378
456
|
/** Brands a running supervisor handle. */
|
|
379
457
|
const TypeId = Symbol.for("@systemfsoftware/effect-daemon-spec/RunningSupervisor");
|
|
@@ -460,13 +538,67 @@ const resolveWaiting = (replies, requestId, outcome) => Effect.flatMap(Ref.modif
|
|
|
460
538
|
onNone: () => Effect.void,
|
|
461
539
|
onSome: (waiter) => Effect.asVoid(Deferred.succeed(waiter, outcome))
|
|
462
540
|
}));
|
|
463
|
-
const staleOf = (event) => Match.value(event).pipe(Match.when({ _tag: "DynamicStartRequested" }, () => ({ outcome: "refused" })), Match.orElse(() => ({ outcome: "missed" })));
|
|
464
541
|
const Ops = {
|
|
465
542
|
resolveWaiting,
|
|
466
543
|
staleOf
|
|
467
544
|
};
|
|
468
545
|
//#endregion
|
|
469
|
-
//#region src/kernel/
|
|
546
|
+
//#region src/kernel/SupervisorState.schema.ts
|
|
547
|
+
const StateTypeId = Symbol.for("@systemfsoftware/effect-daemon/SupervisorState");
|
|
548
|
+
const ChildStatus = Schema.Literals([
|
|
549
|
+
"starting",
|
|
550
|
+
"ready",
|
|
551
|
+
"stopping"
|
|
552
|
+
]);
|
|
553
|
+
const ChildInstance = Schema.Struct({
|
|
554
|
+
childId: ChildId,
|
|
555
|
+
generation: Generation,
|
|
556
|
+
status: ChildStatus,
|
|
557
|
+
consecutiveRestarts: RestartCount,
|
|
558
|
+
probeFailures: ProbeFailures
|
|
559
|
+
});
|
|
560
|
+
const SupervisorCore = Schema.Struct({
|
|
561
|
+
policy: SupervisionPolicy,
|
|
562
|
+
children: Schema.Array(ChildInstance),
|
|
563
|
+
restartStamps: Schema.Array(EventTime),
|
|
564
|
+
nextOrdinal: Ordinal
|
|
565
|
+
});
|
|
566
|
+
const ChildStart = Schema.Struct({
|
|
567
|
+
childId: ChildId,
|
|
568
|
+
generation: Generation
|
|
569
|
+
});
|
|
570
|
+
var Running = class extends Schema.TaggedClass()("Running", { core: SupervisorCore }) {
|
|
571
|
+
[StateTypeId] = StateTypeId;
|
|
572
|
+
};
|
|
573
|
+
var Restarting = class extends Schema.TaggedClass()("Restarting", {
|
|
574
|
+
core: SupervisorCore,
|
|
575
|
+
pending: Schema.Array(ChildStart)
|
|
576
|
+
}) {
|
|
577
|
+
[StateTypeId] = StateTypeId;
|
|
578
|
+
};
|
|
579
|
+
var CoolingDown = class extends Schema.TaggedClass()("CoolingDown", {
|
|
580
|
+
core: SupervisorCore,
|
|
581
|
+
millis: PositiveMillis
|
|
582
|
+
}) {
|
|
583
|
+
[StateTypeId] = StateTypeId;
|
|
584
|
+
};
|
|
585
|
+
var ShuttingDown = class extends Schema.TaggedClass()("ShuttingDown", {
|
|
586
|
+
core: SupervisorCore,
|
|
587
|
+
reason: TerminationReason,
|
|
588
|
+
exit: SupervisorExit
|
|
589
|
+
}) {
|
|
590
|
+
[StateTypeId] = StateTypeId;
|
|
591
|
+
};
|
|
592
|
+
var Terminated = class extends Schema.TaggedClass()("Terminated", { reason: TerminationReason }) {
|
|
593
|
+
[StateTypeId] = StateTypeId;
|
|
594
|
+
};
|
|
595
|
+
const SupervisorState = Schema.Union([
|
|
596
|
+
Running,
|
|
597
|
+
Restarting,
|
|
598
|
+
CoolingDown,
|
|
599
|
+
ShuttingDown,
|
|
600
|
+
Terminated
|
|
601
|
+
]);
|
|
470
602
|
const initialStateOf = (policy) => ({
|
|
471
603
|
policy,
|
|
472
604
|
children: Array.map(policy.childDeclarations, (declaration) => ({
|
|
@@ -479,6 +611,28 @@ const initialStateOf = (policy) => ({
|
|
|
479
611
|
restartStamps: [],
|
|
480
612
|
nextOrdinal: 0
|
|
481
613
|
});
|
|
614
|
+
const withCore = (state, core) => Match.value(state).pipe(Match.tag("Running", () => new Running({ core })), Match.tag("Restarting", (restarting) => new Restarting({
|
|
615
|
+
core,
|
|
616
|
+
pending: restarting.pending
|
|
617
|
+
})), Match.tag("CoolingDown", (cooling) => new CoolingDown({
|
|
618
|
+
core,
|
|
619
|
+
millis: cooling.millis
|
|
620
|
+
})), Match.tag("ShuttingDown", (shutting) => new ShuttingDown({
|
|
621
|
+
core,
|
|
622
|
+
reason: shutting.reason,
|
|
623
|
+
exit: shutting.exit
|
|
624
|
+
})), Match.tag("Terminated", (terminated) => new Terminated({ reason: terminated.reason })), Match.exhaustive);
|
|
625
|
+
const evolved = dual(2, (state, decision) => Match.value(decision).pipe(Match.tag("Stale", () => state), Match.tag("RefuseDynamicStart", () => state), Match.tag("Continue", (continued) => withCore(state, continued.core)), Match.tag("RestartChildren", (restarting) => new Restarting({
|
|
626
|
+
core: restarting.core,
|
|
627
|
+
pending: restarting.pending
|
|
628
|
+
})), Match.tag("StartChildren", (started) => new Running({ core: started.core })), Match.tag("CoolDown", (cooling) => new CoolingDown({
|
|
629
|
+
core: cooling.core,
|
|
630
|
+
millis: cooling.millis
|
|
631
|
+
})), Match.tag("StopChildren", (stopping) => new ShuttingDown({
|
|
632
|
+
core: stopping.core,
|
|
633
|
+
reason: stopping.reason,
|
|
634
|
+
exit: stopping.exit
|
|
635
|
+
})), Match.tag("Terminate", (terminated) => new Terminated({ reason: terminated.reason })), Match.exhaustive));
|
|
482
636
|
//#endregion
|
|
483
637
|
//#region src/kernel/SupervisionEvent.schema.ts
|
|
484
638
|
const TimerKind = Schema.Literals([
|
|
@@ -558,10 +712,6 @@ const SupervisionEvent = Schema.Union([
|
|
|
558
712
|
ShutdownRequested
|
|
559
713
|
]);
|
|
560
714
|
//#endregion
|
|
561
|
-
//#region src/kernel/SupervisionTypeIds.ts
|
|
562
|
-
const DecisionTypeId = Symbol.for("@systemfsoftware/effect-daemon/SupervisionDecision");
|
|
563
|
-
const StateTypeId = Symbol.for("@systemfsoftware/effect-daemon/SupervisorState");
|
|
564
|
-
//#endregion
|
|
565
715
|
//#region src/kernel/SupervisorCommand.schema.ts
|
|
566
716
|
const StartChild = Schema.TaggedStruct("StartChild", {
|
|
567
717
|
childId: ChildId,
|
|
@@ -617,31 +767,155 @@ const SupervisorCommands = Schema.Struct({
|
|
|
617
767
|
terminates: Schema.Array(TerminateSupervisor)
|
|
618
768
|
});
|
|
619
769
|
//#endregion
|
|
620
|
-
//#region src/
|
|
621
|
-
const
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
770
|
+
//#region src/Supervisor/SupervisorTerminated.schema.ts
|
|
771
|
+
const firstLine = (text) => text.split("\n")[0] ?? "";
|
|
772
|
+
var SupervisorTerminated = class extends Schema.TaggedError()("SupervisorTerminated", {
|
|
773
|
+
name: Schema.String,
|
|
774
|
+
reason: TerminationReason,
|
|
775
|
+
cause: Schema.Cause(Schema.Unknown, Schema.Unknown)
|
|
776
|
+
}) {
|
|
777
|
+
get message() {
|
|
778
|
+
return `Supervisor "${this.name}" terminated (${this.reason._tag}): ${firstLine(Cause.pretty(this.cause))}`;
|
|
779
|
+
}
|
|
780
|
+
};
|
|
781
|
+
//#endregion
|
|
782
|
+
//#region src/Supervisor/supervisor-commands.ts
|
|
783
|
+
const Steps = { runtimeOf: (acquired) => ({ acquired }) };
|
|
784
|
+
const stampedNow = (handle, event) => Effect.flatMap(Clock.currentTimeMillis, (now) => offerEvent(handle, {
|
|
785
|
+
...event,
|
|
786
|
+
at: now
|
|
787
|
+
}));
|
|
788
|
+
const startedEventOf = (childId, generation) => ({
|
|
789
|
+
_tag: "ChildStarted",
|
|
790
|
+
at: 0,
|
|
791
|
+
childId,
|
|
792
|
+
generation
|
|
632
793
|
});
|
|
633
|
-
const
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
794
|
+
const readyEventOf = (childId, generation) => ({
|
|
795
|
+
_tag: "ChildReady",
|
|
796
|
+
at: 0,
|
|
797
|
+
childId,
|
|
798
|
+
generation
|
|
638
799
|
});
|
|
639
|
-
const
|
|
640
|
-
|
|
641
|
-
|
|
800
|
+
const terminatedEventOf = (childId, generation, reason) => ({
|
|
801
|
+
_tag: "ChildTerminated",
|
|
802
|
+
at: 0,
|
|
803
|
+
childId,
|
|
804
|
+
generation,
|
|
805
|
+
reason
|
|
806
|
+
});
|
|
807
|
+
const stoppedEventOf = (childId, generation) => ({
|
|
808
|
+
_tag: "ChildStopped",
|
|
809
|
+
at: 0,
|
|
810
|
+
childId,
|
|
811
|
+
generation
|
|
812
|
+
});
|
|
813
|
+
const probeEventOf = (childId, generation, alive) => ({
|
|
814
|
+
_tag: "ProbeResult",
|
|
815
|
+
at: 0,
|
|
816
|
+
childId,
|
|
817
|
+
generation,
|
|
818
|
+
alive
|
|
642
819
|
});
|
|
820
|
+
const timerEventOf = (kind, childId, generation) => ({
|
|
821
|
+
_tag: "TimerElapsed",
|
|
822
|
+
at: 0,
|
|
823
|
+
kind,
|
|
824
|
+
target: {
|
|
825
|
+
_tag: "ChildTimer",
|
|
826
|
+
childId,
|
|
827
|
+
generation
|
|
828
|
+
}
|
|
829
|
+
});
|
|
830
|
+
const supervisorTimerEventOf = (kind) => ({
|
|
831
|
+
_tag: "TimerElapsed",
|
|
832
|
+
at: 0,
|
|
833
|
+
kind,
|
|
834
|
+
target: { _tag: "SupervisorTimer" }
|
|
835
|
+
});
|
|
836
|
+
const evidenceKeyOf = (childId, generation) => `${childId}#${generation}`;
|
|
837
|
+
const registerStarted = (handle, childId, generation, evidence) => Ref.update(evidenceOf(handle), (known) => HashMap.set(known, evidenceKeyOf(childId, generation), evidence));
|
|
838
|
+
const knownStarted = (handle, childId, generation) => Effect.map(Ref.get(evidenceOf(handle)), (known) => HashMap.get(known, evidenceKeyOf(childId, generation)));
|
|
839
|
+
const watchReadiness = (handle, evidence, childId, generation) => Effect.asVoid(Effect.forkIn(Effect.asVoid(Effect.andThen(evidence.ready, stampedNow(handle, readyEventOf(childId, generation)))), ownerScopeOf(handle)));
|
|
840
|
+
const watchReport = (handle, child, evidence, childId, generation) => Effect.asVoid(Effect.forkIn(Effect.flatMap(child.report(evidence), (reason) => stampedNow(handle, terminatedEventOf(childId, generation, reason))), ownerScopeOf(handle)));
|
|
841
|
+
/**
|
|
842
|
+
* The child bound to an id. Declared children are bound at acquisition and dynamic
|
|
843
|
+
* ones by `bindDynamicPrograms` before their start runs, so a missing binding is a
|
|
844
|
+
* broken invariant and dies rather than reporting a start that never ran.
|
|
845
|
+
*/
|
|
846
|
+
const boundChildFor = (handle, childId) => Effect.flatMap(Ref.get(boundChildrenOf(handle)), (children) => Effect.orDie(Effect.fromOption(HashMap.get(children, childId))));
|
|
847
|
+
const executeStart = (acquired, command) => Effect.gen(function* () {
|
|
848
|
+
const child = yield* boundChildFor(acquired.handle, command.childId);
|
|
849
|
+
const outcome = yield* Effect.exit(child.start);
|
|
850
|
+
yield* Exit.match(outcome, {
|
|
851
|
+
onSuccess: (evidence) => Effect.andThen(registerStarted(acquired.handle, command.childId, command.generation, evidence), Effect.andThen(stampedNow(acquired.handle, startedEventOf(command.childId, command.generation)), Effect.andThen(watchReadiness(acquired.handle, evidence, command.childId, command.generation), watchReport(acquired.handle, child, evidence, command.childId, command.generation)))),
|
|
852
|
+
onFailure: (cause) => stampedNow(acquired.handle, terminatedEventOf(command.childId, command.generation, terminationReasonOfCause(cause)))
|
|
853
|
+
});
|
|
854
|
+
});
|
|
855
|
+
/**
|
|
856
|
+
* Moves each answered request's pending child: an accepted start binds it to the
|
|
857
|
+
* child the kernel allocated, and every answered request drops its pending entry.
|
|
858
|
+
*/
|
|
859
|
+
const bindDynamicPrograms = (handle, replies) => Effect.andThen(Effect.forEach(Array.filter(replies, Schema.is(ReplyStartAccepted)), (accepted) => Effect.flatMap(Ref.get(pendingChildrenOf(handle)), (pending) => Effect.orDie(Effect.fromOption(HashMap.get(pending, accepted.requestId))).pipe(Effect.flatMap((child) => Ref.update(boundChildrenOf(handle), (children) => HashMap.set(children, accepted.childId, child))))), { discard: true }), Ref.update(pendingChildrenOf(handle), (pending) => Array.reduce(replies, pending, (remaining, reply) => HashMap.remove(remaining, reply.requestId))));
|
|
860
|
+
const executeStop = (acquired, command) => Effect.gen(function* () {
|
|
861
|
+
const child = yield* boundChildFor(acquired.handle, command.childId);
|
|
862
|
+
const found = yield* knownStarted(acquired.handle, command.childId, command.generation);
|
|
863
|
+
yield* Option.match(found, {
|
|
864
|
+
onNone: () => Effect.void,
|
|
865
|
+
onSome: (evidence) => Effect.asVoid(child.stop(evidence, command.shutdown))
|
|
866
|
+
});
|
|
867
|
+
yield* stampedNow(acquired.handle, stoppedEventOf(command.childId, command.generation));
|
|
868
|
+
});
|
|
869
|
+
const armTimer = (acquired, deadline, event) => Effect.flatMap(Clock.currentTimeMillis, (now) => Effect.asVoid(Effect.forkIn(Effect.andThen(Effect.sleep(Duration.millis(Math.max(0, deadline - now))), stampedNow(acquired.handle, event)), ownerScopeOf(acquired.handle))));
|
|
870
|
+
const executeArm = (acquired, arm) => Match.value(arm).pipe(Match.tag("ArmChildTimer", (timer) => armTimer(acquired, timer.deadline, timerEventOf(timer.kind, timer.childId, timer.generation))), Match.tag("ArmSupervisorTimer", (timer) => armTimer(acquired, timer.deadline, supervisorTimerEventOf(timer.kind))), Match.exhaustive);
|
|
871
|
+
const executeReply = (acquired, reply) => Ops.resolveWaiting(repliesOf(acquired.handle), reply.requestId, outcomeOf(reply));
|
|
872
|
+
const giveUpCauseOf = (handle, exit, reason) => Effect.flatMap(Ref.get(evidenceOf(handle)), (evidence) => Option.match(HashMap.get(evidence, evidenceKeyOf(exit.childId, exit.generation)), {
|
|
873
|
+
onNone: () => Effect.succeed(Cause.fail(reason)),
|
|
874
|
+
onSome: (started) => Option.match(failureCauseOf(started), {
|
|
875
|
+
onNone: () => Effect.succeed(Cause.fail(reason)),
|
|
876
|
+
onSome: (awaited) => Effect.map(awaited, (cause) => Option.getOrElse(cause, () => Cause.fail(reason)))
|
|
877
|
+
})
|
|
878
|
+
}));
|
|
879
|
+
const clearEvidence = (handle) => Ref.update(evidenceOf(handle), () => HashMap.empty());
|
|
880
|
+
const executeTerminate = (acquired, command) => Match.value(command.exit).pipe(Match.tag("RequestedExit", () => Effect.andThen(clearEvidence(acquired.handle), Effect.asVoid(Deferred.succeed(terminatedLatchOf(acquired.handle), void 0)))), Match.tag("IntensityExceededExit", (exit) => Effect.flatMap(giveUpCauseOf(acquired.handle, exit, command.reason), (cause) => Effect.andThen(clearEvidence(acquired.handle), Effect.asVoid(Deferred.fail(terminatedLatchOf(acquired.handle), new SupervisorTerminated({
|
|
881
|
+
name: acquired.handle.name,
|
|
882
|
+
reason: command.reason,
|
|
883
|
+
cause
|
|
884
|
+
})))))), Match.exhaustive);
|
|
885
|
+
const bucketRunnerOf = (acquired) => ({
|
|
886
|
+
stops: (commands) => Effect.forEach(commands.stops, (command) => executeStop(acquired, command), { discard: true }),
|
|
887
|
+
starts: (commands) => Effect.forEach(commands.starts, (command) => executeStart(acquired, command), { discard: true }),
|
|
888
|
+
arms: (commands) => Effect.forEach(commands.arms, (command) => executeArm(acquired, command), { discard: true }),
|
|
889
|
+
replies: (commands) => Effect.forEach(commands.replies, (reply) => executeReply(acquired, reply), { discard: true }),
|
|
890
|
+
terminates: (commands) => Effect.forEach(commands.terminates, (command) => executeTerminate(acquired, command), { discard: true })
|
|
891
|
+
});
|
|
892
|
+
const Commands = {
|
|
893
|
+
runBuckets: (acquired, commands) => runBuckets(acquired, commands),
|
|
894
|
+
probeOnce: (acquired, childId, generation) => probeOnce(acquired, childId, generation),
|
|
895
|
+
answerStale: (acquired, event) => answerStale(acquired, event),
|
|
896
|
+
requestIdOf: (event) => requestIdOf(event)
|
|
897
|
+
};
|
|
898
|
+
const runBuckets = (acquired, commands) => {
|
|
899
|
+
const buckets = bucketRunnerOf(acquired);
|
|
900
|
+
return Effect.andThen(buckets.stops(commands), Effect.andThen(Effect.andThen(bindDynamicPrograms(acquired.handle, commands.replies), buckets.starts(commands)), Effect.andThen(buckets.arms(commands), Effect.andThen(buckets.replies(commands), buckets.terminates(commands)))));
|
|
901
|
+
};
|
|
902
|
+
const probeOnce = (acquired, childId, generation) => Effect.gen(function* () {
|
|
903
|
+
const child = yield* boundChildFor(acquired.handle, childId);
|
|
904
|
+
const found = yield* knownStarted(acquired.handle, childId, generation);
|
|
905
|
+
const alive = yield* Option.match(found, {
|
|
906
|
+
onNone: () => Effect.succeed(false),
|
|
907
|
+
onSome: (evidence) => child.probe(evidence)
|
|
908
|
+
});
|
|
909
|
+
yield* stampedNow(acquired.handle, probeEventOf(childId, generation, alive));
|
|
910
|
+
});
|
|
911
|
+
const requestIdOf = (event) => Match.value(event).pipe(Match.when({ _tag: "DynamicStartRequested" }, (requested) => requested.requestId), Match.when({ _tag: "DynamicStopRequested" }, (requested) => requested.requestId), Match.orElse(() => void 0));
|
|
912
|
+
const answerStale = (acquired, event) => {
|
|
913
|
+
const requestId = requestIdOf(event);
|
|
914
|
+
return requestId === void 0 ? Effect.void : Ops.resolveWaiting(repliesOf(acquired.handle), requestId, Ops.staleOf(event));
|
|
915
|
+
};
|
|
643
916
|
//#endregion
|
|
644
917
|
//#region src/kernel/interpret-supervision-event.workflow.ts
|
|
918
|
+
const DecisionTypeId = Symbol.for("@systemfsoftware/effect-daemon/SupervisionDecision");
|
|
645
919
|
var Stale = class extends Schema.TaggedClass()("Stale", {}) {
|
|
646
920
|
[DecisionTypeId] = DecisionTypeId;
|
|
647
921
|
};
|
|
@@ -698,40 +972,8 @@ const SupervisionDecision = Schema.Union([
|
|
|
698
972
|
Terminate,
|
|
699
973
|
RefuseDynamicStart
|
|
700
974
|
]);
|
|
701
|
-
var Running$1 = class extends Schema.TaggedClass()("Running", { core: SupervisorCore }) {
|
|
702
|
-
[StateTypeId] = StateTypeId;
|
|
703
|
-
};
|
|
704
|
-
var Restarting$1 = class extends Schema.TaggedClass()("Restarting", {
|
|
705
|
-
core: SupervisorCore,
|
|
706
|
-
pending: Schema.Array(ChildStart)
|
|
707
|
-
}) {
|
|
708
|
-
[StateTypeId] = StateTypeId;
|
|
709
|
-
};
|
|
710
|
-
var CoolingDown$1 = class extends Schema.TaggedClass()("CoolingDown", {
|
|
711
|
-
core: SupervisorCore,
|
|
712
|
-
millis: PositiveMillis
|
|
713
|
-
}) {
|
|
714
|
-
[StateTypeId] = StateTypeId;
|
|
715
|
-
};
|
|
716
|
-
var ShuttingDown$1 = class extends Schema.TaggedClass()("ShuttingDown", {
|
|
717
|
-
core: SupervisorCore,
|
|
718
|
-
reason: TerminationReason,
|
|
719
|
-
exit: SupervisorExit
|
|
720
|
-
}) {
|
|
721
|
-
[StateTypeId] = StateTypeId;
|
|
722
|
-
};
|
|
723
|
-
var Terminated$1 = class extends Schema.TaggedClass()("Terminated", { reason: TerminationReason }) {
|
|
724
|
-
[StateTypeId] = StateTypeId;
|
|
725
|
-
};
|
|
726
|
-
const SupervisorState$1 = Schema.Union([
|
|
727
|
-
Running$1,
|
|
728
|
-
Restarting$1,
|
|
729
|
-
CoolingDown$1,
|
|
730
|
-
ShuttingDown$1,
|
|
731
|
-
Terminated$1
|
|
732
|
-
]);
|
|
733
975
|
var SupervisionStep = class extends Schema.TaggedClass()("SupervisionStep", {
|
|
734
|
-
state: SupervisorState
|
|
976
|
+
state: SupervisorState,
|
|
735
977
|
event: SupervisionEvent
|
|
736
978
|
}) {
|
|
737
979
|
static [Workflow.InstrumentationBrand] = {};
|
|
@@ -1216,66 +1458,12 @@ const interpretSupervisionEvent = Workflow.make({
|
|
|
1216
1458
|
});
|
|
1217
1459
|
//#endregion
|
|
1218
1460
|
//#region src/kernel/evolve-supervisor.workflow.ts
|
|
1219
|
-
var Running = class extends Schema.TaggedClass()("Running", { core: SupervisorCore }) {
|
|
1220
|
-
[StateTypeId] = StateTypeId;
|
|
1221
|
-
};
|
|
1222
|
-
var Restarting = class extends Schema.TaggedClass()("Restarting", {
|
|
1223
|
-
core: SupervisorCore,
|
|
1224
|
-
pending: Schema.Array(ChildStart)
|
|
1225
|
-
}) {
|
|
1226
|
-
[StateTypeId] = StateTypeId;
|
|
1227
|
-
};
|
|
1228
|
-
var CoolingDown = class extends Schema.TaggedClass()("CoolingDown", {
|
|
1229
|
-
core: SupervisorCore,
|
|
1230
|
-
millis: PositiveMillis
|
|
1231
|
-
}) {
|
|
1232
|
-
[StateTypeId] = StateTypeId;
|
|
1233
|
-
};
|
|
1234
|
-
var ShuttingDown = class extends Schema.TaggedClass()("ShuttingDown", {
|
|
1235
|
-
core: SupervisorCore,
|
|
1236
|
-
reason: TerminationReason,
|
|
1237
|
-
exit: SupervisorExit
|
|
1238
|
-
}) {
|
|
1239
|
-
[StateTypeId] = StateTypeId;
|
|
1240
|
-
};
|
|
1241
|
-
var Terminated = class extends Schema.TaggedClass()("Terminated", { reason: TerminationReason }) {
|
|
1242
|
-
[StateTypeId] = StateTypeId;
|
|
1243
|
-
};
|
|
1244
|
-
const SupervisorState = Schema.Union([
|
|
1245
|
-
Running,
|
|
1246
|
-
Restarting,
|
|
1247
|
-
CoolingDown,
|
|
1248
|
-
ShuttingDown,
|
|
1249
|
-
Terminated
|
|
1250
|
-
]);
|
|
1251
1461
|
var SupervisionEvolution = class extends Schema.TaggedClass()("SupervisionEvolution", {
|
|
1252
1462
|
state: SupervisorState,
|
|
1253
1463
|
decision: SupervisionDecision
|
|
1254
1464
|
}) {
|
|
1255
1465
|
static [Workflow.InstrumentationBrand] = {};
|
|
1256
1466
|
};
|
|
1257
|
-
const withCore = (state, core) => Match.value(state).pipe(Match.tag("Running", () => new Running({ core })), Match.tag("Restarting", (restarting) => new Restarting({
|
|
1258
|
-
core,
|
|
1259
|
-
pending: restarting.pending
|
|
1260
|
-
})), Match.tag("CoolingDown", (cooling) => new CoolingDown({
|
|
1261
|
-
core,
|
|
1262
|
-
millis: cooling.millis
|
|
1263
|
-
})), Match.tag("ShuttingDown", (shutting) => new ShuttingDown({
|
|
1264
|
-
core,
|
|
1265
|
-
reason: shutting.reason,
|
|
1266
|
-
exit: shutting.exit
|
|
1267
|
-
})), Match.tag("Terminated", (terminated) => new Terminated({ reason: terminated.reason })), Match.exhaustive);
|
|
1268
|
-
const evolved = (state, decision) => Match.value(decision).pipe(Match.tag("Stale", () => state), Match.tag("RefuseDynamicStart", () => state), Match.tag("Continue", (continued) => withCore(state, continued.core)), Match.tag("RestartChildren", (restarting) => new Restarting({
|
|
1269
|
-
core: restarting.core,
|
|
1270
|
-
pending: restarting.pending
|
|
1271
|
-
})), Match.tag("StartChildren", (started) => new Running({ core: started.core })), Match.tag("CoolDown", (cooling) => new CoolingDown({
|
|
1272
|
-
core: cooling.core,
|
|
1273
|
-
millis: cooling.millis
|
|
1274
|
-
})), Match.tag("StopChildren", (stopping) => new ShuttingDown({
|
|
1275
|
-
core: stopping.core,
|
|
1276
|
-
reason: stopping.reason,
|
|
1277
|
-
exit: stopping.exit
|
|
1278
|
-
})), Match.tag("Terminate", (terminated) => new Terminated({ reason: terminated.reason })), Match.exhaustive);
|
|
1279
1467
|
const evolveSupervisor = Workflow.make({
|
|
1280
1468
|
command: SupervisionEvolution,
|
|
1281
1469
|
decision: SupervisorState,
|
|
@@ -1283,164 +1471,6 @@ const evolveSupervisor = Workflow.make({
|
|
|
1283
1471
|
decide: (step) => Result.succeed(evolved(step.state, step.decision))
|
|
1284
1472
|
});
|
|
1285
1473
|
//#endregion
|
|
1286
|
-
//#region src/Supervisor/SupervisorTerminated.schema.ts
|
|
1287
|
-
const firstLine = (text) => text.split("\n")[0] ?? "";
|
|
1288
|
-
var SupervisorTerminated = class extends Schema.TaggedError()("SupervisorTerminated", {
|
|
1289
|
-
name: Schema.String,
|
|
1290
|
-
reason: TerminationReason,
|
|
1291
|
-
cause: Schema.Cause(Schema.Unknown, Schema.Unknown)
|
|
1292
|
-
}) {
|
|
1293
|
-
get message() {
|
|
1294
|
-
return `Supervisor "${this.name}" terminated (${this.reason._tag}): ${firstLine(Cause.pretty(this.cause))}`;
|
|
1295
|
-
}
|
|
1296
|
-
};
|
|
1297
|
-
//#endregion
|
|
1298
|
-
//#region src/Supervisor/supervisor-commands.ts
|
|
1299
|
-
const stampedNow = (handle, event) => Effect.flatMap(Clock.currentTimeMillis, (now) => offerEvent(handle, {
|
|
1300
|
-
...event,
|
|
1301
|
-
at: now
|
|
1302
|
-
}));
|
|
1303
|
-
const startedEventOf = (childId, generation) => ({
|
|
1304
|
-
_tag: "ChildStarted",
|
|
1305
|
-
at: 0,
|
|
1306
|
-
childId,
|
|
1307
|
-
generation
|
|
1308
|
-
});
|
|
1309
|
-
const readyEventOf = (childId, generation) => ({
|
|
1310
|
-
_tag: "ChildReady",
|
|
1311
|
-
at: 0,
|
|
1312
|
-
childId,
|
|
1313
|
-
generation
|
|
1314
|
-
});
|
|
1315
|
-
const terminatedEventOf = (childId, generation, reason) => ({
|
|
1316
|
-
_tag: "ChildTerminated",
|
|
1317
|
-
at: 0,
|
|
1318
|
-
childId,
|
|
1319
|
-
generation,
|
|
1320
|
-
reason
|
|
1321
|
-
});
|
|
1322
|
-
const stoppedEventOf = (childId, generation) => ({
|
|
1323
|
-
_tag: "ChildStopped",
|
|
1324
|
-
at: 0,
|
|
1325
|
-
childId,
|
|
1326
|
-
generation
|
|
1327
|
-
});
|
|
1328
|
-
const probeEventOf = (childId, generation, alive) => ({
|
|
1329
|
-
_tag: "ProbeResult",
|
|
1330
|
-
at: 0,
|
|
1331
|
-
childId,
|
|
1332
|
-
generation,
|
|
1333
|
-
alive
|
|
1334
|
-
});
|
|
1335
|
-
const timerEventOf = (kind, childId, generation) => ({
|
|
1336
|
-
_tag: "TimerElapsed",
|
|
1337
|
-
at: 0,
|
|
1338
|
-
kind,
|
|
1339
|
-
target: {
|
|
1340
|
-
_tag: "ChildTimer",
|
|
1341
|
-
childId,
|
|
1342
|
-
generation
|
|
1343
|
-
}
|
|
1344
|
-
});
|
|
1345
|
-
const supervisorTimerEventOf = (kind) => ({
|
|
1346
|
-
_tag: "TimerElapsed",
|
|
1347
|
-
at: 0,
|
|
1348
|
-
kind,
|
|
1349
|
-
target: { _tag: "SupervisorTimer" }
|
|
1350
|
-
});
|
|
1351
|
-
const evidenceKeyOf = (childId, generation) => `${childId}#${generation}`;
|
|
1352
|
-
const registerStarted = (handle, childId, generation, evidence) => Ref.update(evidenceOf(handle), (known) => HashMap.set(known, evidenceKeyOf(childId, generation), evidence));
|
|
1353
|
-
const knownStarted = (handle, childId, generation) => Effect.map(Ref.get(evidenceOf(handle)), (known) => HashMap.get(known, evidenceKeyOf(childId, generation)));
|
|
1354
|
-
const watchReadiness = (handle, evidence, childId, generation) => Effect.asVoid(Effect.forkIn(Effect.asVoid(Effect.andThen(evidence.ready, stampedNow(handle, readyEventOf(childId, generation)))), ownerScopeOf(handle)));
|
|
1355
|
-
const watchReport = (handle, child, evidence, childId, generation) => Effect.asVoid(Effect.forkIn(Effect.flatMap(child.report(evidence), (reason) => stampedNow(handle, terminatedEventOf(childId, generation, reason))), ownerScopeOf(handle)));
|
|
1356
|
-
/**
|
|
1357
|
-
* The child bound to an id. Declared children are bound at acquisition and dynamic
|
|
1358
|
-
* ones by `bindDynamicPrograms` before their start runs, so a missing binding is a
|
|
1359
|
-
* broken invariant and dies rather than reporting a start that never ran.
|
|
1360
|
-
*/
|
|
1361
|
-
const boundChildFor = (handle, childId) => Effect.flatMap(Ref.get(boundChildrenOf(handle)), (children) => Effect.orDie(Effect.fromOption(HashMap.get(children, childId))));
|
|
1362
|
-
const failureReasonOf = (cause) => Option.getOrElse(Cause.findErrorOption(cause), () => ({
|
|
1363
|
-
_tag: "Abnormal",
|
|
1364
|
-
report: {
|
|
1365
|
-
_tag: "CauseReport",
|
|
1366
|
-
cause: Cause.pretty(cause)
|
|
1367
|
-
}
|
|
1368
|
-
}));
|
|
1369
|
-
const executeStart = (acquired, command) => Effect.gen(function* () {
|
|
1370
|
-
const child = yield* boundChildFor(acquired.handle, command.childId);
|
|
1371
|
-
const outcome = yield* Effect.exit(child.start);
|
|
1372
|
-
yield* Exit.match(outcome, {
|
|
1373
|
-
onSuccess: (evidence) => Effect.andThen(registerStarted(acquired.handle, command.childId, command.generation, evidence), Effect.andThen(stampedNow(acquired.handle, startedEventOf(command.childId, command.generation)), Effect.andThen(watchReadiness(acquired.handle, evidence, command.childId, command.generation), watchReport(acquired.handle, child, evidence, command.childId, command.generation)))),
|
|
1374
|
-
onFailure: (cause) => stampedNow(acquired.handle, terminatedEventOf(command.childId, command.generation, failureReasonOf(cause)))
|
|
1375
|
-
});
|
|
1376
|
-
});
|
|
1377
|
-
/**
|
|
1378
|
-
* Moves each answered request's pending child: an accepted start binds it to the
|
|
1379
|
-
* child the kernel allocated, and every answered request drops its pending entry.
|
|
1380
|
-
*/
|
|
1381
|
-
const bindDynamicPrograms = (handle, replies) => Effect.andThen(Effect.forEach(Array.filter(replies, Schema.is(ReplyStartAccepted)), (accepted) => Effect.flatMap(Ref.get(pendingChildrenOf(handle)), (pending) => Effect.orDie(Effect.fromOption(HashMap.get(pending, accepted.requestId))).pipe(Effect.flatMap((child) => Ref.update(boundChildrenOf(handle), (children) => HashMap.set(children, accepted.childId, child))))), { discard: true }), Ref.update(pendingChildrenOf(handle), (pending) => Array.reduce(replies, pending, (remaining, reply) => HashMap.remove(remaining, reply.requestId))));
|
|
1382
|
-
const executeStop = (acquired, command) => Effect.gen(function* () {
|
|
1383
|
-
const child = yield* boundChildFor(acquired.handle, command.childId);
|
|
1384
|
-
const found = yield* knownStarted(acquired.handle, command.childId, command.generation);
|
|
1385
|
-
yield* Option.match(found, {
|
|
1386
|
-
onNone: () => Effect.void,
|
|
1387
|
-
onSome: (evidence) => Effect.asVoid(child.stop(evidence, command.shutdown))
|
|
1388
|
-
});
|
|
1389
|
-
yield* stampedNow(acquired.handle, stoppedEventOf(command.childId, command.generation));
|
|
1390
|
-
});
|
|
1391
|
-
const armTimer = (acquired, deadline, event) => Effect.flatMap(Clock.currentTimeMillis, (now) => Effect.asVoid(Effect.forkIn(Effect.andThen(Effect.sleep(Duration.millis(Math.max(0, deadline - now))), stampedNow(acquired.handle, event)), ownerScopeOf(acquired.handle))));
|
|
1392
|
-
const executeArm = (acquired, arm) => Match.value(arm).pipe(Match.tag("ArmChildTimer", (timer) => armTimer(acquired, timer.deadline, timerEventOf(timer.kind, timer.childId, timer.generation))), Match.tag("ArmSupervisorTimer", (timer) => armTimer(acquired, timer.deadline, supervisorTimerEventOf(timer.kind))), Match.exhaustive);
|
|
1393
|
-
const outcomeOf = (reply) => Match.value(reply).pipe(Match.tag("ReplyStartAccepted", (accepted) => ({
|
|
1394
|
-
outcome: "accepted",
|
|
1395
|
-
childId: accepted.childId,
|
|
1396
|
-
generation: accepted.generation
|
|
1397
|
-
})), Match.tag("ReplyStartRefused", () => ({ outcome: "refused" })), Match.tag("ReplyStopped", () => ({ outcome: "stopped" })), Match.exhaustive);
|
|
1398
|
-
const executeReply = (acquired, reply) => Ops.resolveWaiting(repliesOf(acquired.handle), reply.requestId, outcomeOf(reply));
|
|
1399
|
-
const giveUpCauseOf = (handle, exit, reason) => Effect.flatMap(Ref.get(evidenceOf(handle)), (evidence) => Option.match(HashMap.get(evidence, evidenceKeyOf(exit.childId, exit.generation)), {
|
|
1400
|
-
onNone: () => Effect.succeed(Cause.fail(reason)),
|
|
1401
|
-
onSome: (started) => Option.match(failureCauseOf(started), {
|
|
1402
|
-
onNone: () => Effect.succeed(Cause.fail(reason)),
|
|
1403
|
-
onSome: (awaited) => Effect.map(awaited, (cause) => Option.getOrElse(cause, () => Cause.fail(reason)))
|
|
1404
|
-
})
|
|
1405
|
-
}));
|
|
1406
|
-
const clearEvidence = (handle) => Ref.update(evidenceOf(handle), () => HashMap.empty());
|
|
1407
|
-
const executeTerminate = (acquired, command) => Match.value(command.exit).pipe(Match.tag("RequestedExit", () => Effect.andThen(clearEvidence(acquired.handle), Effect.asVoid(Deferred.succeed(terminatedLatchOf(acquired.handle), void 0)))), Match.tag("IntensityExceededExit", (exit) => Effect.flatMap(giveUpCauseOf(acquired.handle, exit, command.reason), (cause) => Effect.andThen(clearEvidence(acquired.handle), Effect.asVoid(Deferred.fail(terminatedLatchOf(acquired.handle), new SupervisorTerminated({
|
|
1408
|
-
name: acquired.handle.name,
|
|
1409
|
-
reason: command.reason,
|
|
1410
|
-
cause
|
|
1411
|
-
})))))), Match.exhaustive);
|
|
1412
|
-
const bucketRunnerOf = (acquired) => ({
|
|
1413
|
-
stops: (commands) => Effect.forEach(commands.stops, (command) => executeStop(acquired, command), { discard: true }),
|
|
1414
|
-
starts: (commands) => Effect.forEach(commands.starts, (command) => executeStart(acquired, command), { discard: true }),
|
|
1415
|
-
arms: (commands) => Effect.forEach(commands.arms, (command) => executeArm(acquired, command), { discard: true }),
|
|
1416
|
-
replies: (commands) => Effect.forEach(commands.replies, (reply) => executeReply(acquired, reply), { discard: true }),
|
|
1417
|
-
terminates: (commands) => Effect.forEach(commands.terminates, (command) => executeTerminate(acquired, command), { discard: true })
|
|
1418
|
-
});
|
|
1419
|
-
const Commands = {
|
|
1420
|
-
runBuckets: (acquired, commands) => runBuckets(acquired, commands),
|
|
1421
|
-
probeOnce: (acquired, childId, generation) => probeOnce(acquired, childId, generation),
|
|
1422
|
-
answerStale: (acquired, event) => answerStale(acquired, event),
|
|
1423
|
-
requestIdOf: (event) => requestIdOf(event)
|
|
1424
|
-
};
|
|
1425
|
-
const runBuckets = (acquired, commands) => {
|
|
1426
|
-
const buckets = bucketRunnerOf(acquired);
|
|
1427
|
-
return Effect.andThen(buckets.stops(commands), Effect.andThen(Effect.andThen(bindDynamicPrograms(acquired.handle, commands.replies), buckets.starts(commands)), Effect.andThen(buckets.arms(commands), Effect.andThen(buckets.replies(commands), buckets.terminates(commands)))));
|
|
1428
|
-
};
|
|
1429
|
-
const probeOnce = (acquired, childId, generation) => Effect.gen(function* () {
|
|
1430
|
-
const child = yield* boundChildFor(acquired.handle, childId);
|
|
1431
|
-
const found = yield* knownStarted(acquired.handle, childId, generation);
|
|
1432
|
-
const alive = yield* Option.match(found, {
|
|
1433
|
-
onNone: () => Effect.succeed(false),
|
|
1434
|
-
onSome: (evidence) => child.probe(evidence)
|
|
1435
|
-
});
|
|
1436
|
-
yield* stampedNow(acquired.handle, probeEventOf(childId, generation, alive));
|
|
1437
|
-
});
|
|
1438
|
-
const requestIdOf = (event) => Match.value(event).pipe(Match.when({ _tag: "DynamicStartRequested" }, (requested) => requested.requestId), Match.when({ _tag: "DynamicStopRequested" }, (requested) => requested.requestId), Match.orElse(() => void 0));
|
|
1439
|
-
const answerStale = (acquired, event) => {
|
|
1440
|
-
const requestId = requestIdOf(event);
|
|
1441
|
-
return requestId === void 0 ? Effect.void : Ops.resolveWaiting(repliesOf(acquired.handle), requestId, Ops.staleOf(event));
|
|
1442
|
-
};
|
|
1443
|
-
//#endregion
|
|
1444
1474
|
//#region src/Supervisor/supervisor-step.cell.ts
|
|
1445
1475
|
const readStep = (runtime, event) => Effect.map(Effect.zip(Clock.currentTimeMillis, Ref.get(stateOf(runtime.acquired.handle))), ([now, state]) => new SupervisionStep({
|
|
1446
1476
|
state,
|
|
@@ -1448,17 +1478,20 @@ const readStep = (runtime, event) => Effect.map(Effect.zip(Clock.currentTimeMill
|
|
|
1448
1478
|
...event,
|
|
1449
1479
|
at: now
|
|
1450
1480
|
}
|
|
1451
|
-
}
|
|
1452
|
-
const persistedOf = (previous, decision) =>
|
|
1481
|
+
}));
|
|
1482
|
+
const persistedOf = (previous, decision) => Effect.flatMap(Schema.decodeEffect(SupervisionDecision)(decision), (decoded) => Effect.fromResult(evolveSupervisor(new SupervisionEvolution({
|
|
1453
1483
|
state: previous,
|
|
1454
|
-
decision:
|
|
1455
|
-
})));
|
|
1456
|
-
const persistStep = (runtime, event, previous, decision) => Effect.flatMap(Effect.
|
|
1484
|
+
decision: decoded
|
|
1485
|
+
}))));
|
|
1486
|
+
const persistStep = (runtime, event, previous, decision) => Effect.flatMap(Effect.orDie(persistedOf(previous, decision)), (next) => Effect.andThen(Ref.set(stateOf(runtime.acquired.handle), next), PubSub.publish(tracePubSubOf(runtime.acquired.handle), {
|
|
1457
1487
|
event,
|
|
1458
1488
|
decision
|
|
1459
1489
|
})));
|
|
1460
1490
|
const runBucketsOf = (runtime, commands) => Commands.runBuckets(runtime.acquired, commands);
|
|
1461
|
-
const
|
|
1491
|
+
const persistCommandRejection = (runtime, event) => PubSub.publish(tracePubSubOf(runtime.acquired.handle), {
|
|
1492
|
+
event,
|
|
1493
|
+
decision: new Stale({})
|
|
1494
|
+
}).pipe(Effect.asVoid);
|
|
1462
1495
|
const supervisorStepFor = (runtime) => Sandwich.named("supervisor.step")((event) => readStep(runtime, event)).decide(interpretSupervisionEvent).write({
|
|
1463
1496
|
Stale: (decision, command) => Effect.andThen(persistStep(runtime, command.event, command.state, decision), Commands.answerStale(runtime.acquired, command.event)),
|
|
1464
1497
|
Continue: (decision, command) => Effect.andThen(persistStep(runtime, command.event, command.state, decision), runBucketsOf(runtime, decision.commands)),
|
|
@@ -1470,35 +1503,12 @@ const supervisorStepFor = (runtime) => Sandwich.named("supervisor.step")((event)
|
|
|
1470
1503
|
RefuseDynamicStart: (decision, command) => Effect.andThen(persistStep(runtime, command.event, command.state, decision), runBucketsOf(runtime, decision.commands)),
|
|
1471
1504
|
CommandRejected: (_rejected, command) => Effect.andThen(persistCommandRejection(runtime, command.event), Commands.answerStale(runtime.acquired, command.event))
|
|
1472
1505
|
});
|
|
1473
|
-
const persistCommandRejection = (runtime, event) => PubSub.publish(tracePubSubOf(runtime.acquired.handle), {
|
|
1474
|
-
event,
|
|
1475
|
-
decision: new Stale({})
|
|
1476
|
-
}).pipe(Effect.asVoid);
|
|
1477
1506
|
//#endregion
|
|
1478
1507
|
//#region src/Supervisor/Supervisor.ts
|
|
1479
1508
|
const SpecTypeId = Symbol.for("@systemfsoftware/effect-daemon-spec/SupervisorSpec");
|
|
1480
1509
|
const hasSpecTag = Predicate.hasProperty(SpecTypeId);
|
|
1481
1510
|
const isSupervisorSpec = (value) => hasSpecTag(value) && value[SpecTypeId] === SpecTypeId;
|
|
1482
|
-
const WORKER_SHUTDOWN = {
|
|
1483
|
-
_tag: "Graceful",
|
|
1484
|
-
millis: 5e3
|
|
1485
|
-
};
|
|
1486
|
-
const SUPERVISOR_SHUTDOWN = { _tag: "Infinity" };
|
|
1487
1511
|
const LIVENESS_TICK_MILLIS = 1e3;
|
|
1488
|
-
const policyOfParts = (parts) => ({
|
|
1489
|
-
strategy: parts.strategy,
|
|
1490
|
-
intensity: parts.intensity,
|
|
1491
|
-
periodMillis: parts.periodMillis,
|
|
1492
|
-
autoShutdown: parts.autoShutdown,
|
|
1493
|
-
coolDown: parts.coolDownMillis === void 0 ? { _tag: "NoCoolDown" } : {
|
|
1494
|
-
_tag: "CoolDownAfter",
|
|
1495
|
-
millis: parts.coolDownMillis
|
|
1496
|
-
},
|
|
1497
|
-
backoff: parts.backoff,
|
|
1498
|
-
dynamic: parts.dynamic,
|
|
1499
|
-
livenessTickMillis: parts.livenessTickMillis,
|
|
1500
|
-
childDeclarations: parts.declarations
|
|
1501
|
-
});
|
|
1502
1512
|
const drainOf = (handle, step) => Effect.flatMap(Queue.take(mailboxOf(handle)), (event) => Effect.flatMap(Effect.asVoid(step.run(event)), () => Effect.flatMap(Ref.get(stateOf(handle)), (state) => Match.value(state).pipe(Match.when({ _tag: "Terminated" }, () => Effect.void), Match.orElse(() => drainOf(handle, step))))));
|
|
1503
1513
|
const fiberMediumOf = () => Effect.map(Effect.serviceOption(fiberPort), (found) => Option.match(found, {
|
|
1504
1514
|
onNone: () => mediumFor(),
|
|
@@ -1506,7 +1516,7 @@ const fiberMediumOf = () => Effect.map(Effect.serviceOption(fiberPort), (found)
|
|
|
1506
1516
|
}));
|
|
1507
1517
|
const scopedOf = (parts) => Effect.gen(function* () {
|
|
1508
1518
|
const supervisorScope = yield* Effect.scope;
|
|
1509
|
-
const initial = new Running
|
|
1519
|
+
const initial = new Running({ core: initialStateOf(supervisionPolicyOf(parts)) });
|
|
1510
1520
|
const bound = yield* Effect.forEach(parts.bindings, (entry) => Effect.map(entry.binding, (child) => [entry.declaration.childId, child]), { concurrency: 1 });
|
|
1511
1521
|
const context = yield* Effect.context();
|
|
1512
1522
|
const fiber = yield* fiberMediumOf();
|
|
@@ -1588,7 +1598,7 @@ const livenessTick = dual(2, (self, millis) => specOf({
|
|
|
1588
1598
|
const dynamicKindOf = (options) => ({
|
|
1589
1599
|
_tag: "DynamicChildren",
|
|
1590
1600
|
restartType: "permanent",
|
|
1591
|
-
shutdown:
|
|
1601
|
+
shutdown: childShutdownOf(false),
|
|
1592
1602
|
startTimeoutMillis: 5e3,
|
|
1593
1603
|
probeFailureThreshold: 2,
|
|
1594
1604
|
...options
|
|
@@ -1600,7 +1610,7 @@ const dynamic = dual(2, (self, options) => specOf({
|
|
|
1600
1610
|
const declarationOf = (childId, nested, options) => ({
|
|
1601
1611
|
childId,
|
|
1602
1612
|
restartType: "permanent",
|
|
1603
|
-
shutdown: nested
|
|
1613
|
+
shutdown: childShutdownOf(nested),
|
|
1604
1614
|
significant: false,
|
|
1605
1615
|
startTimeoutMillis: 5e3,
|
|
1606
1616
|
probeFailureThreshold: 2,
|
|
@@ -1661,6 +1671,7 @@ const child = dual((args) => isSupervisorSpec(args[0]), (self, childId, program,
|
|
|
1661
1671
|
//#region src/Supervisor/mod.ts
|
|
1662
1672
|
var Supervisor_exports = /* @__PURE__ */ __exportAll({
|
|
1663
1673
|
ChildSpecs: () => ChildSpecs,
|
|
1674
|
+
ClassifyChildExit: () => ClassifyChildExit,
|
|
1664
1675
|
FiberMedium: () => FiberMedium_exports,
|
|
1665
1676
|
Medium: () => Medium_exports,
|
|
1666
1677
|
SpecTypeId: () => SpecTypeId,
|
|
@@ -1671,6 +1682,7 @@ var Supervisor_exports = /* @__PURE__ */ __exportAll({
|
|
|
1671
1682
|
backoff: () => backoff,
|
|
1672
1683
|
child: () => child,
|
|
1673
1684
|
children: () => children,
|
|
1685
|
+
classifyChildExit: () => classifyChildExit,
|
|
1674
1686
|
coolDown: () => coolDown,
|
|
1675
1687
|
dynamic: () => dynamic,
|
|
1676
1688
|
intensity: () => intensity,
|