@mark1russell7/service 0.8.18 → 0.8.20
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/README.md +19 -0
- package/build/backoff.d.ts +44 -2
- package/build/{builder-zO48Vgoj.d.ts → builder.d.ts} +14 -101
- package/build/builder.js +499 -0
- package/build/builder.js.map +1 -0
- package/build/{call-context-DDhcVvn5.d.ts → call-context.d.ts} +3 -4
- package/build/channel.d.ts +1 -1
- package/build/{core-BSzt4RI2.d.ts → client/core.d.ts} +32 -61
- package/build/{machine-BhVeHr_m.js → client/core.js} +4 -153
- package/build/client/core.js.map +1 -0
- package/build/{dial-ChwIcvd4.d.ts → client/dial.d.ts} +3 -4
- package/build/client/dial.js +52 -0
- package/build/client/dial.js.map +1 -0
- package/build/client/machine.js +29 -0
- package/build/client/machine.js.map +1 -0
- package/build/client/meets.d.ts +35 -0
- package/build/client/meets.js +78 -0
- package/build/client/meets.js.map +1 -0
- package/build/clock.d.ts +1 -1
- package/build/conformance.d.ts +122 -0
- package/build/conformance.js +470 -0
- package/build/conformance.js.map +1 -0
- package/build/contract.d.ts +28 -0
- package/build/contract.js +184 -0
- package/build/contract.js.map +1 -0
- package/build/description.d.ts +161 -0
- package/build/description.js +11 -0
- package/build/description.js.map +1 -0
- package/build/drains.js +35 -0
- package/build/drains.js.map +1 -0
- package/build/events.d.ts +25 -0
- package/build/fakes.d.ts +43 -0
- package/build/fakes.js +93 -0
- package/build/fakes.js.map +1 -0
- package/build/grpc/client.d.ts +153 -0
- package/build/grpc/client.js +474 -0
- package/build/grpc/client.js.map +1 -0
- package/build/grpc/connection.d.ts +97 -0
- package/build/grpc/connection.js +59 -0
- package/build/grpc/connection.js.map +1 -0
- package/build/grpc/machine.d.ts +12 -0
- package/build/grpc/machine.js +149 -0
- package/build/grpc/machine.js.map +1 -0
- package/build/grpc/meets.d.ts +14 -0
- package/build/grpc/meets.js +105 -0
- package/build/grpc/meets.js.map +1 -0
- package/build/grpc/starting.d.ts +33 -0
- package/build/grpc/starting.js +89 -0
- package/build/grpc/starting.js.map +1 -0
- package/build/grpc.d.ts +9 -303
- package/build/grpc.js +7 -854
- package/build/health.d.ts +93 -0
- package/build/health.js +223 -0
- package/build/health.js.map +1 -0
- package/build/heartbeat.d.ts +1 -1
- package/build/host/discovery.d.ts +61 -0
- package/build/host/discovery.js +113 -0
- package/build/host/discovery.js.map +1 -0
- package/build/host/env.d.ts +14 -0
- package/build/host/env.js +57 -0
- package/build/host/env.js.map +1 -0
- package/build/host/fakes.d.ts +82 -0
- package/build/host/fakes.js +239 -0
- package/build/host/fakes.js.map +1 -0
- package/build/host/group.d.ts +142 -0
- package/build/host/group.js +517 -0
- package/build/host/group.js.map +1 -0
- package/build/host/install.d.ts +96 -0
- package/build/host/install.js +330 -0
- package/build/host/install.js.map +1 -0
- package/build/host/lock.d.ts +78 -0
- package/build/host/lock.js +190 -0
- package/build/host/lock.js.map +1 -0
- package/build/host/logs.d.ts +26 -0
- package/build/host/logs.js +48 -0
- package/build/host/logs.js.map +1 -0
- package/build/host/node.d.ts +14 -0
- package/build/host/node.js +309 -0
- package/build/host/node.js.map +1 -0
- package/build/{ports-B1ayTL-j.d.ts → host/ports.d.ts} +15 -16
- package/build/host/ports.js +17 -0
- package/build/host/ports.js.map +1 -0
- package/build/host/probes.d.ts +51 -0
- package/build/host/probes.js +139 -0
- package/build/host/probes.js.map +1 -0
- package/build/host/processes.d.ts +32 -0
- package/build/host/processes.js +63 -0
- package/build/host/processes.js.map +1 -0
- package/build/host/render.d.ts +84 -0
- package/build/host/render.js +235 -0
- package/build/host/render.js.map +1 -0
- package/build/host/watch.d.ts +90 -0
- package/build/host/watch.js +165 -0
- package/build/host/watch.js.map +1 -0
- package/build/host.d.ts +13 -671
- package/build/host.js +12 -2126
- package/build/http/answers.js +29 -0
- package/build/http/answers.js.map +1 -0
- package/build/http/fake-server.js +61 -0
- package/build/http/fake-server.js.map +1 -0
- package/build/http/outcome.d.ts +115 -0
- package/build/http/outcome.js +325 -0
- package/build/http/outcome.js.map +1 -0
- package/build/http/probes.d.ts +83 -0
- package/build/http/probes.js +232 -0
- package/build/http/probes.js.map +1 -0
- package/build/http/transport.d.ts +165 -0
- package/build/http/transport.js +517 -0
- package/build/http/transport.js.map +1 -0
- package/build/http-client/client.d.ts +121 -0
- package/build/http-client/client.js +330 -0
- package/build/http-client/client.js.map +1 -0
- package/build/http-client/connection.d.ts +78 -0
- package/build/http-client/connection.js +54 -0
- package/build/http-client/connection.js.map +1 -0
- package/build/http-client/machine.d.ts +14 -0
- package/build/http-client/machine.js +109 -0
- package/build/http-client/machine.js.map +1 -0
- package/build/http-client/meets.d.ts +11 -0
- package/build/http-client/meets.js +74 -0
- package/build/http-client/meets.js.map +1 -0
- package/build/http-client/session.d.ts +87 -0
- package/build/http-client/session.js +99 -0
- package/build/http-client/session.js.map +1 -0
- package/build/http-client/starting.d.ts +25 -0
- package/build/http-client/starting.js +65 -0
- package/build/http-client/starting.js.map +1 -0
- package/build/http-client.d.ts +9 -330
- package/build/http-client.js +8 -707
- package/build/http.d.ts +5 -353
- package/build/http.js +3 -1086
- package/build/identity.d.ts +16 -0
- package/build/identity.js +44 -0
- package/build/identity.js.map +1 -0
- package/build/index.d.ts +35 -412
- package/build/index.js +24 -3277
- package/build/knobs.d.ts +1 -0
- package/build/{knobs-BJcz6m01.js → knobs.js} +2 -34
- package/build/knobs.js.map +1 -0
- package/build/latch.d.ts +29 -0
- package/build/latch.js +122 -0
- package/build/latch.js.map +1 -0
- package/build/{rules-HyFCBStQ.d.ts → law.d.ts} +19 -37
- package/build/law.js +149 -0
- package/build/law.js.map +1 -0
- package/build/lifecycle.d.ts +144 -0
- package/build/lifecycle.js +411 -0
- package/build/lifecycle.js.map +1 -0
- package/build/limiter/machine.d.ts +40 -0
- package/build/limiter/machine.js +279 -0
- package/build/limiter/machine.js.map +1 -0
- package/build/{policy-CBcK2XGg.d.ts → limiter/policy.d.ts} +28 -29
- package/build/{policy-UYALkqVq.js → limiter/policy.js} +10 -9
- package/build/limiter/policy.js.map +1 -0
- package/build/{limiter-Cm8nC-Gd.d.ts → limiter/quota.d.ts} +50 -76
- package/build/{limiter-Bgy6Gmvl.js → limiter/quota.js} +32 -425
- package/build/limiter/quota.js.map +1 -0
- package/build/{store-DFwFKrsB.d.ts → limiter/store.d.ts} +10 -11
- package/build/limiter/store.js +145 -0
- package/build/limiter/store.js.map +1 -0
- package/build/limiter.d.ts +4 -3
- package/build/limiter.js +4 -2
- package/build/lint.d.ts +152 -0
- package/build/lint.js +576 -0
- package/build/lint.js.map +1 -0
- package/build/logger.d.ts +71 -0
- package/build/logger.js +183 -0
- package/build/logger.js.map +1 -0
- package/build/machines.d.ts +41 -0
- package/build/machines.js +451 -0
- package/build/machines.js.map +1 -0
- package/build/metrics.d.ts +53 -0
- package/build/metrics.js +111 -0
- package/build/metrics.js.map +1 -0
- package/build/mongodb/client.d.ts +131 -0
- package/build/mongodb/client.js +388 -0
- package/build/mongodb/client.js.map +1 -0
- package/build/mongodb/connection.d.ts +112 -0
- package/build/mongodb/connection.js +12 -0
- package/build/mongodb/connection.js.map +1 -0
- package/build/mongodb/machine.d.ts +14 -0
- package/build/mongodb/machine.js +122 -0
- package/build/mongodb/machine.js.map +1 -0
- package/build/mongodb/meets.d.ts +15 -0
- package/build/mongodb/meets.js +103 -0
- package/build/mongodb/meets.js.map +1 -0
- package/build/mongodb/starting.d.ts +29 -0
- package/build/mongodb/starting.js +77 -0
- package/build/mongodb/starting.js.map +1 -0
- package/build/mongodb.d.ts +8 -296
- package/build/mongodb.js +7 -682
- package/build/operation.d.ts +27 -0
- package/build/{operation-DF73RZOI.js → operation.js} +2 -2
- package/build/operation.js.map +1 -0
- package/build/otel.d.ts +2 -2
- package/build/pg/client.d.ts +96 -0
- package/build/pg/client.js +277 -0
- package/build/pg/client.js.map +1 -0
- package/build/pg/connection.d.ts +75 -0
- package/build/pg/connection.js +11 -0
- package/build/pg/connection.js.map +1 -0
- package/build/pg/machine.d.ts +13 -0
- package/build/pg/machine.js +111 -0
- package/build/pg/machine.js.map +1 -0
- package/build/pg/meets.d.ts +14 -0
- package/build/pg/meets.js +83 -0
- package/build/pg/meets.js.map +1 -0
- package/build/pg/starting.d.ts +26 -0
- package/build/pg/starting.js +62 -0
- package/build/pg/starting.js.map +1 -0
- package/build/pg.d.ts +8 -219
- package/build/pg.js +7 -525
- package/build/poller.d.ts +9 -0
- package/build/poller.js +152 -0
- package/build/poller.js.map +1 -0
- package/build/process.d.ts +114 -0
- package/build/process.js +170 -0
- package/build/process.js.map +1 -0
- package/build/profiles.d.ts +68 -0
- package/build/profiles.js +120 -0
- package/build/profiles.js.map +1 -0
- package/build/{queue-DXdw1vLO.d.ts → queue.d.ts} +16 -29
- package/build/queue.js +279 -0
- package/build/queue.js.map +1 -0
- package/build/rabbitmq/client.d.ts +144 -0
- package/build/rabbitmq/client.js +426 -0
- package/build/rabbitmq/client.js.map +1 -0
- package/build/rabbitmq/connection.d.ts +91 -0
- package/build/rabbitmq/connection.js +15 -0
- package/build/rabbitmq/connection.js.map +1 -0
- package/build/rabbitmq/machine.d.ts +13 -0
- package/build/rabbitmq/machine.js +159 -0
- package/build/rabbitmq/machine.js.map +1 -0
- package/build/rabbitmq/meets.d.ts +12 -0
- package/build/rabbitmq/meets.js +96 -0
- package/build/rabbitmq/meets.js.map +1 -0
- package/build/rabbitmq/starting.d.ts +27 -0
- package/build/rabbitmq/starting.js +67 -0
- package/build/rabbitmq/starting.js.map +1 -0
- package/build/rabbitmq.d.ts +8 -283
- package/build/rabbitmq.js +7 -742
- package/build/redis/client.d.ts +86 -0
- package/build/redis/client.js +285 -0
- package/build/redis/client.js.map +1 -0
- package/build/redis/connection.d.ts +57 -0
- package/build/redis/connection.js +11 -0
- package/build/redis/connection.js.map +1 -0
- package/build/redis/limiter.d.ts +43 -0
- package/build/redis/limiter.js +164 -0
- package/build/redis/limiter.js.map +1 -0
- package/build/redis/machine.d.ts +12 -0
- package/build/redis/machine.js +144 -0
- package/build/redis/machine.js.map +1 -0
- package/build/redis/meets.d.ts +11 -0
- package/build/redis/meets.js +75 -0
- package/build/redis/meets.js.map +1 -0
- package/build/redis/operations.d.ts +5 -0
- package/build/redis/operations.js +114 -0
- package/build/redis/operations.js.map +1 -0
- package/build/redis.d.ts +10 -207
- package/build/redis.js +9 -771
- package/build/requests.d.ts +66 -0
- package/build/requests.js +279 -0
- package/build/requests.js.map +1 -0
- package/build/retrier.d.ts +62 -0
- package/build/{retrier-Dnjhvxol.js → retrier.js} +2 -2
- package/build/retrier.js.map +1 -0
- package/build/routes.d.ts +35 -0
- package/build/routes.js +77 -0
- package/build/routes.js.map +1 -0
- package/build/rules.d.ts +19 -2
- package/build/stops.d.ts +51 -0
- package/build/stops.js +86 -0
- package/build/stops.js.map +1 -0
- package/build/testing.d.ts +8 -270
- package/build/testing.js +4 -1027
- package/build/testing.js.map +1 -1
- package/build/timing.d.ts +63 -0
- package/build/{timing-DZl36uXs.js → timing.js} +2 -2
- package/build/timing.js.map +1 -0
- package/build/units.d.ts +17 -0
- package/build/units.js +236 -0
- package/build/units.js.map +1 -0
- package/build/watchdog.d.ts +34 -0
- package/build/watchdog.js +53 -0
- package/build/watchdog.js.map +1 -0
- package/build/websocket/client.d.ts +136 -0
- package/build/websocket/client.js +422 -0
- package/build/websocket/client.js.map +1 -0
- package/build/websocket/connection.d.ts +83 -0
- package/build/websocket/connection.js +57 -0
- package/build/websocket/connection.js.map +1 -0
- package/build/websocket/machine.d.ts +12 -0
- package/build/websocket/machine.js +155 -0
- package/build/websocket/machine.js.map +1 -0
- package/build/websocket/meets.d.ts +14 -0
- package/build/websocket/meets.js +92 -0
- package/build/websocket/meets.js.map +1 -0
- package/build/websocket/starting.d.ts +25 -0
- package/build/websocket/starting.js +69 -0
- package/build/websocket/starting.js.map +1 -0
- package/build/websocket.d.ts +9 -264
- package/build/websocket.js +8 -774
- package/build/worker.d.ts +92 -0
- package/build/worker.js +153 -0
- package/build/worker.js.map +1 -0
- package/package.json +4 -3
- package/build/backoff-QTRGfkEK.d.ts +0 -45
- package/build/clock-DpVgpY4h.d.ts +0 -2
- package/build/grpc.js.map +0 -1
- package/build/host.js.map +0 -1
- package/build/http-client.js.map +0 -1
- package/build/http.js.map +0 -1
- package/build/index.js.map +0 -1
- package/build/knobs-BJcz6m01.js.map +0 -1
- package/build/lifecycle-D0J-zwZF.d.ts +0 -374
- package/build/limiter-Bgy6Gmvl.js.map +0 -1
- package/build/logger-B6drv9w0.d.ts +0 -488
- package/build/machine-BhVeHr_m.js.map +0 -1
- package/build/mongodb.js.map +0 -1
- package/build/operation-DF73RZOI.js.map +0 -1
- package/build/pg.js.map +0 -1
- package/build/policy-UYALkqVq.js.map +0 -1
- package/build/process-7R9M29wN.js +0 -437
- package/build/process-7R9M29wN.js.map +0 -1
- package/build/rabbitmq.js.map +0 -1
- package/build/redis.js.map +0 -1
- package/build/requests-DD2ALv0_.js +0 -583
- package/build/requests-DD2ALv0_.js.map +0 -1
- package/build/retrier-Dnjhvxol.js.map +0 -1
- package/build/timing-DZl36uXs.js.map +0 -1
- package/build/websocket.js.map +0 -1
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { FakeClock } from "../fakes.js";
|
|
2
|
+
import { Logger } from "../logger.js";
|
|
3
|
+
import { ChildHandle, CommandOptions, CommandResult, HostPorts, ProcessMark, SpawnRequest, StopSignal } from "./ports.js";
|
|
4
|
+
/** A fake child is a child process of a fake host. A test ends it, or a stop ends it. */
|
|
5
|
+
export interface FakeChild extends ChildHandle {
|
|
6
|
+
readonly request: SpawnRequest;
|
|
7
|
+
/** These are the messages that went on its IPC channel. */
|
|
8
|
+
readonly sent: readonly unknown[];
|
|
9
|
+
/** This flag tells if the child ended. */
|
|
10
|
+
readonly ended: boolean;
|
|
11
|
+
/** This method ends the child with a code, or with a signal and a null code. A second use does nothing. */
|
|
12
|
+
exit(code: number | null, signal?: string | null): void;
|
|
13
|
+
/** This method sends data on the output of the child. */
|
|
14
|
+
output(stream: "stdout" | "stderr", chunk: string | Uint8Array): void;
|
|
15
|
+
}
|
|
16
|
+
/** A recorded command is one command that went through the fake command port. */
|
|
17
|
+
export interface RecordedCommand {
|
|
18
|
+
readonly file: string;
|
|
19
|
+
readonly args: readonly string[];
|
|
20
|
+
readonly options: CommandOptions | undefined;
|
|
21
|
+
}
|
|
22
|
+
/** A recorded signal is one signal that went through the fake process port. */
|
|
23
|
+
export interface RecordedSignal {
|
|
24
|
+
readonly pid: number;
|
|
25
|
+
readonly signal: StopSignal;
|
|
26
|
+
readonly group: boolean;
|
|
27
|
+
}
|
|
28
|
+
/** These options go with a fake host. */
|
|
29
|
+
export interface FakeHostOptions {
|
|
30
|
+
/** This text is the platform. The default is `linux`. */
|
|
31
|
+
readonly platform?: string | undefined;
|
|
32
|
+
readonly env?: Readonly<Record<string, string | undefined>> | undefined;
|
|
33
|
+
/** The default is `/home/test`. */
|
|
34
|
+
readonly homeDir?: string | undefined;
|
|
35
|
+
/** The default is `test`. */
|
|
36
|
+
readonly user?: string | undefined;
|
|
37
|
+
/** This record names the host process. The default is the ID 100, which started at the wall time 0. */
|
|
38
|
+
readonly self?: ProcessMark | undefined;
|
|
39
|
+
/** This time is the wall time at the clock time 0, in milliseconds after the epoch. The default is 1 000 000 000 000. */
|
|
40
|
+
readonly wallStart?: number | undefined;
|
|
41
|
+
/** These files are in the fake file system at the start. */
|
|
42
|
+
readonly files?: Readonly<Record<string, string | Uint8Array>> | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* This function gives the result of a command. When it gives undefined, the fake answers. `taskkill`
|
|
45
|
+
* with `/F` ends the process and gives 0. `taskkill` without `/F` gives 1, or ends a polite child. Each other command gives 0.
|
|
46
|
+
*/
|
|
47
|
+
readonly answer?: ((command: RecordedCommand) => CommandResult | undefined) | undefined;
|
|
48
|
+
readonly logger?: Logger | undefined;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* A fake host is the host ports of a test, and the state behind them. It starts no process, writes no
|
|
52
|
+
* file and installs no task. Each effect is a record or a change of its state.
|
|
53
|
+
*/
|
|
54
|
+
export interface FakeHost {
|
|
55
|
+
readonly ports: HostPorts;
|
|
56
|
+
readonly clock: FakeClock;
|
|
57
|
+
/** These are the files of the fake file system. */
|
|
58
|
+
readonly files: Map<string, string | Uint8Array>;
|
|
59
|
+
readonly commands: RecordedCommand[];
|
|
60
|
+
readonly signals: RecordedSignal[];
|
|
61
|
+
readonly children: FakeChild[];
|
|
62
|
+
/** This table holds each live process: its ID, and its start time or null. */
|
|
63
|
+
readonly processes: Map<number, number | null>;
|
|
64
|
+
/** This map gives the HTTP status of each address. An address that it does not have gives no answer. */
|
|
65
|
+
readonly http: Map<string, number | null>;
|
|
66
|
+
/** This map gives the text of the answer of each address, for `httpText`. The default text is empty. */
|
|
67
|
+
readonly bodies: Map<string, string>;
|
|
68
|
+
/** This set holds each `host:port` that takes a TCP connection. */
|
|
69
|
+
readonly tcp: Set<string>;
|
|
70
|
+
/** True makes each child end with the code 0 at a staged stop: SIGTERM, SIGINT, an IPC message or `taskkill` without `/F`. */
|
|
71
|
+
polite: boolean;
|
|
72
|
+
/** This function makes a file operation fail. It gets the name of the operation and the path. */
|
|
73
|
+
failFiles: ((operation: string, path: string) => boolean) | null;
|
|
74
|
+
/** This function makes a spawn fail, thus the child has no ID and ends. */
|
|
75
|
+
failSpawn: ((request: SpawnRequest) => boolean) | null;
|
|
76
|
+
/** This method gives the text of a file, or null. */
|
|
77
|
+
text(path: string): string | null;
|
|
78
|
+
/** This method lets the promises that are ready continue, without a move of the clock. */
|
|
79
|
+
settle(): Promise<void>;
|
|
80
|
+
}
|
|
81
|
+
/** This function makes a fake host. Give its `ports` to the host functions in a test. */
|
|
82
|
+
export declare const fakeHost: (options?: FakeHostOptions) => FakeHost;
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { fakeClock } from "../fakes.js";
|
|
2
|
+
// src/host/fakes.ts
|
|
3
|
+
const encoder = new TextEncoder();
|
|
4
|
+
const decoder = new TextDecoder();
|
|
5
|
+
const bytesOf = (data) => typeof data === "string" ? encoder.encode(data) : data;
|
|
6
|
+
/** This function makes a fake host. Give its `ports` to the host functions in a test. */
|
|
7
|
+
const fakeHost = (options = {}) => {
|
|
8
|
+
const clock = fakeClock();
|
|
9
|
+
const wallStart = options.wallStart ?? 0xe8d4a51000;
|
|
10
|
+
const wall = () => wallStart + clock.now();
|
|
11
|
+
const self = options.self ?? {
|
|
12
|
+
pid: 100,
|
|
13
|
+
startedAt: wallStart
|
|
14
|
+
};
|
|
15
|
+
const files = new Map(Object.entries(options.files ?? {}));
|
|
16
|
+
const commands = [];
|
|
17
|
+
const signals = [];
|
|
18
|
+
const children = [];
|
|
19
|
+
const processes = /* @__PURE__ */ new Map([[self.pid, self.startedAt]]);
|
|
20
|
+
const live = /* @__PURE__ */ new Map();
|
|
21
|
+
let nextPid = 1e3;
|
|
22
|
+
const check = (operation, path) => {
|
|
23
|
+
if (host.failFiles?.(operation, path) === true) throw new Error(`the fake ${operation} of ${path} failed`);
|
|
24
|
+
};
|
|
25
|
+
const politeStop = (pid) => {
|
|
26
|
+
const child = live.get(pid);
|
|
27
|
+
if (host.polite && child !== void 0) queueMicrotask(() => child.exit(0));
|
|
28
|
+
};
|
|
29
|
+
const hardStop = (pid, signal) => {
|
|
30
|
+
const child = live.get(pid);
|
|
31
|
+
if (child !== void 0) queueMicrotask(() => child.exit(null, signal));
|
|
32
|
+
else processes.delete(pid);
|
|
33
|
+
};
|
|
34
|
+
const spawn = (request) => {
|
|
35
|
+
const pid = host.failSpawn?.(request) === true ? null : nextPid++;
|
|
36
|
+
const outputs = [];
|
|
37
|
+
const exits = [];
|
|
38
|
+
const sent = [];
|
|
39
|
+
let end = null;
|
|
40
|
+
const child = {
|
|
41
|
+
request,
|
|
42
|
+
pid,
|
|
43
|
+
ipc: request.ipc,
|
|
44
|
+
sent,
|
|
45
|
+
get ended() {
|
|
46
|
+
return end !== null;
|
|
47
|
+
},
|
|
48
|
+
onOutput(handler) {
|
|
49
|
+
outputs.push(handler);
|
|
50
|
+
},
|
|
51
|
+
onExit(handler) {
|
|
52
|
+
exits.push(handler);
|
|
53
|
+
if (end !== null) handler(end.code, end.signal);
|
|
54
|
+
},
|
|
55
|
+
send(message) {
|
|
56
|
+
if (!request.ipc || end !== null) return false;
|
|
57
|
+
sent.push(message);
|
|
58
|
+
if (pid !== null) politeStop(pid);
|
|
59
|
+
return true;
|
|
60
|
+
},
|
|
61
|
+
exit(code, signal = null) {
|
|
62
|
+
if (end !== null) return;
|
|
63
|
+
end = {
|
|
64
|
+
code,
|
|
65
|
+
signal
|
|
66
|
+
};
|
|
67
|
+
if (pid !== null) {
|
|
68
|
+
processes.delete(pid);
|
|
69
|
+
live.delete(pid);
|
|
70
|
+
}
|
|
71
|
+
for (const handler of exits) handler(code, signal);
|
|
72
|
+
},
|
|
73
|
+
output(stream, chunk) {
|
|
74
|
+
for (const handler of outputs) handler(stream, chunk);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
children.push(child);
|
|
78
|
+
if (pid === null) queueMicrotask(() => child.exit(null));
|
|
79
|
+
else {
|
|
80
|
+
processes.set(pid, wall());
|
|
81
|
+
live.set(pid, child);
|
|
82
|
+
}
|
|
83
|
+
return child;
|
|
84
|
+
};
|
|
85
|
+
const signal = (pid, sig, group) => {
|
|
86
|
+
if (!processes.has(pid)) return false;
|
|
87
|
+
signals.push({
|
|
88
|
+
pid,
|
|
89
|
+
signal: sig,
|
|
90
|
+
group
|
|
91
|
+
});
|
|
92
|
+
if (sig === "SIGKILL") hardStop(pid, "SIGKILL");
|
|
93
|
+
else politeStop(pid);
|
|
94
|
+
return true;
|
|
95
|
+
};
|
|
96
|
+
const defaultAnswer = (command) => {
|
|
97
|
+
if (command.file !== "taskkill") return {
|
|
98
|
+
code: 0,
|
|
99
|
+
out: ""
|
|
100
|
+
};
|
|
101
|
+
const at = command.args.indexOf("/PID");
|
|
102
|
+
const pid = Number(command.args[at + 1]);
|
|
103
|
+
if (at < 0 || !processes.has(pid)) return {
|
|
104
|
+
code: 128,
|
|
105
|
+
out: `ERROR: The process "${command.args[at + 1] ?? ""}" not found.`
|
|
106
|
+
};
|
|
107
|
+
if (command.args.includes("/F")) {
|
|
108
|
+
hardStop(pid, null);
|
|
109
|
+
return {
|
|
110
|
+
code: 0,
|
|
111
|
+
out: `SUCCESS: The process with PID ${pid} has been terminated.`
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
if (host.polite && live.has(pid)) {
|
|
115
|
+
politeStop(pid);
|
|
116
|
+
return {
|
|
117
|
+
code: 0,
|
|
118
|
+
out: `SUCCESS: Sent termination signal to the process with PID ${pid}.`
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
code: 1,
|
|
123
|
+
out: `ERROR: The process with PID ${pid} could not be terminated.`
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
const ports = {
|
|
127
|
+
platform: options.platform ?? "linux",
|
|
128
|
+
commands: { async run(file, args, commandOptions) {
|
|
129
|
+
const command = {
|
|
130
|
+
file,
|
|
131
|
+
args: [...args],
|
|
132
|
+
options: commandOptions
|
|
133
|
+
};
|
|
134
|
+
commands.push(command);
|
|
135
|
+
return options.answer?.(command) ?? defaultAnswer(command);
|
|
136
|
+
} },
|
|
137
|
+
files: {
|
|
138
|
+
async read(path) {
|
|
139
|
+
check("read", path);
|
|
140
|
+
const data = files.get(path);
|
|
141
|
+
return data === void 0 ? null : typeof data === "string" ? data : decoder.decode(data);
|
|
142
|
+
},
|
|
143
|
+
async write(path, data) {
|
|
144
|
+
check("write", path);
|
|
145
|
+
files.set(path, data);
|
|
146
|
+
},
|
|
147
|
+
async create(path, data) {
|
|
148
|
+
check("create", path);
|
|
149
|
+
if (files.has(path)) return false;
|
|
150
|
+
files.set(path, data);
|
|
151
|
+
return true;
|
|
152
|
+
},
|
|
153
|
+
async append(path, data) {
|
|
154
|
+
check("append", path);
|
|
155
|
+
const before = files.get(path);
|
|
156
|
+
if (before === void 0) files.set(path, data);
|
|
157
|
+
else if (typeof before === "string" && typeof data === "string") files.set(path, before + data);
|
|
158
|
+
else {
|
|
159
|
+
const head = bytesOf(before);
|
|
160
|
+
const tail = bytesOf(data);
|
|
161
|
+
const joined = new Uint8Array(head.length + tail.length);
|
|
162
|
+
joined.set(head, 0);
|
|
163
|
+
joined.set(tail, head.length);
|
|
164
|
+
files.set(path, joined);
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
async size(path) {
|
|
168
|
+
check("size", path);
|
|
169
|
+
const data = files.get(path);
|
|
170
|
+
return data === void 0 ? null : bytesOf(data).length;
|
|
171
|
+
},
|
|
172
|
+
async rename(from, to) {
|
|
173
|
+
check("rename", from);
|
|
174
|
+
const data = files.get(from);
|
|
175
|
+
if (data === void 0) throw new Error(`the fake file ${from} does not exist`);
|
|
176
|
+
files.delete(from);
|
|
177
|
+
files.set(to, data);
|
|
178
|
+
},
|
|
179
|
+
async remove(path) {
|
|
180
|
+
check("remove", path);
|
|
181
|
+
files.delete(path);
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
processes: {
|
|
185
|
+
alive: (pid) => processes.has(pid),
|
|
186
|
+
startOf: async (pid) => processes.get(pid) ?? null,
|
|
187
|
+
signal: (pid, sig) => signal(pid, sig, false),
|
|
188
|
+
signalGroup: (pid, sig) => ports.platform !== "win32" && live.has(pid) && signal(pid, sig, true),
|
|
189
|
+
spawn
|
|
190
|
+
},
|
|
191
|
+
net: {
|
|
192
|
+
httpStatus: async (url) => host.http.get(url) ?? null,
|
|
193
|
+
httpText: async (url, _timeout, limit) => {
|
|
194
|
+
const status = host.http.get(url) ?? null;
|
|
195
|
+
if (status === null) return null;
|
|
196
|
+
const bytes = encoder.encode(host.bodies.get(url) ?? "");
|
|
197
|
+
return {
|
|
198
|
+
status,
|
|
199
|
+
body: decoder.decode(bytes.subarray(0, limit)),
|
|
200
|
+
complete: bytes.length <= limit
|
|
201
|
+
};
|
|
202
|
+
},
|
|
203
|
+
tcpConnect: async (hostName, port) => host.tcp.has(`${hostName}:${port}`)
|
|
204
|
+
},
|
|
205
|
+
clock,
|
|
206
|
+
wall,
|
|
207
|
+
self,
|
|
208
|
+
env: options.env ?? {},
|
|
209
|
+
homeDir: options.homeDir ?? "/home/test",
|
|
210
|
+
user: () => options.user ?? "test",
|
|
211
|
+
...options.logger === void 0 ? {} : { logger: options.logger }
|
|
212
|
+
};
|
|
213
|
+
const host = {
|
|
214
|
+
ports,
|
|
215
|
+
clock,
|
|
216
|
+
files,
|
|
217
|
+
commands,
|
|
218
|
+
signals,
|
|
219
|
+
children,
|
|
220
|
+
processes,
|
|
221
|
+
http: /* @__PURE__ */ new Map(),
|
|
222
|
+
bodies: /* @__PURE__ */ new Map(),
|
|
223
|
+
tcp: /* @__PURE__ */ new Set(),
|
|
224
|
+
polite: false,
|
|
225
|
+
failFiles: null,
|
|
226
|
+
failSpawn: null,
|
|
227
|
+
text(path) {
|
|
228
|
+
const data = files.get(path);
|
|
229
|
+
return data === void 0 ? null : typeof data === "string" ? data : decoder.decode(data);
|
|
230
|
+
},
|
|
231
|
+
async settle() {
|
|
232
|
+
for (let turn = 0; turn < 2e3; turn += 1) await Promise.resolve();
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
return host;
|
|
236
|
+
};
|
|
237
|
+
export { fakeHost };
|
|
238
|
+
|
|
239
|
+
//# sourceMappingURL=fakes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fakes.js","names":[],"sources":["../../src/host/fakes.ts"],"sourcesContent":["import type { FakeClock } from \"../fakes.ts\";\nimport { fakeClock } from \"../fakes.ts\";\nimport type { Logger } from \"../logger.ts\";\nimport type { ChildHandle, CommandOptions, CommandResult, HostPorts, ProcessMark, SpawnRequest, StopSignal } from \"./ports.ts\";\n\n/** A fake child is a child process of a fake host. A test ends it, or a stop ends it. */\nexport interface FakeChild extends ChildHandle {\n readonly request: SpawnRequest;\n /** These are the messages that went on its IPC channel. */\n readonly sent: readonly unknown[];\n /** This flag tells if the child ended. */\n readonly ended: boolean;\n /** This method ends the child with a code, or with a signal and a null code. A second use does nothing. */\n exit(code: number | null, signal?: string | null): void;\n /** This method sends data on the output of the child. */\n output(stream: \"stdout\" | \"stderr\", chunk: string | Uint8Array): void;\n}\n\n/** A recorded command is one command that went through the fake command port. */\nexport interface RecordedCommand {\n readonly file: string;\n readonly args: readonly string[];\n readonly options: CommandOptions | undefined;\n}\n\n/** A recorded signal is one signal that went through the fake process port. */\nexport interface RecordedSignal {\n readonly pid: number;\n readonly signal: StopSignal;\n readonly group: boolean;\n}\n\n/** These options go with a fake host. */\nexport interface FakeHostOptions {\n /** This text is the platform. The default is `linux`. */\n readonly platform?: string | undefined;\n readonly env?: Readonly<Record<string, string | undefined>> | undefined;\n /** The default is `/home/test`. */\n readonly homeDir?: string | undefined;\n /** The default is `test`. */\n readonly user?: string | undefined;\n /** This record names the host process. The default is the ID 100, which started at the wall time 0. */\n readonly self?: ProcessMark | undefined;\n /** This time is the wall time at the clock time 0, in milliseconds after the epoch. The default is 1 000 000 000 000. */\n readonly wallStart?: number | undefined;\n /** These files are in the fake file system at the start. */\n readonly files?: Readonly<Record<string, string | Uint8Array>> | undefined;\n /**\n * This function gives the result of a command. When it gives undefined, the fake answers. `taskkill`\n * with `/F` ends the process and gives 0. `taskkill` without `/F` gives 1, or ends a polite child. Each other command gives 0.\n */\n readonly answer?: ((command: RecordedCommand) => CommandResult | undefined) | undefined;\n readonly logger?: Logger | undefined;\n}\n\n/**\n * A fake host is the host ports of a test, and the state behind them. It starts no process, writes no\n * file and installs no task. Each effect is a record or a change of its state.\n */\nexport interface FakeHost {\n readonly ports: HostPorts;\n readonly clock: FakeClock;\n /** These are the files of the fake file system. */\n readonly files: Map<string, string | Uint8Array>;\n readonly commands: RecordedCommand[];\n readonly signals: RecordedSignal[];\n readonly children: FakeChild[];\n /** This table holds each live process: its ID, and its start time or null. */\n readonly processes: Map<number, number | null>;\n /** This map gives the HTTP status of each address. An address that it does not have gives no answer. */\n readonly http: Map<string, number | null>;\n /** This map gives the text of the answer of each address, for `httpText`. The default text is empty. */\n readonly bodies: Map<string, string>;\n /** This set holds each `host:port` that takes a TCP connection. */\n readonly tcp: Set<string>;\n /** True makes each child end with the code 0 at a staged stop: SIGTERM, SIGINT, an IPC message or `taskkill` without `/F`. */\n polite: boolean;\n /** This function makes a file operation fail. It gets the name of the operation and the path. */\n failFiles: ((operation: string, path: string) => boolean) | null;\n /** This function makes a spawn fail, thus the child has no ID and ends. */\n failSpawn: ((request: SpawnRequest) => boolean) | null;\n /** This method gives the text of a file, or null. */\n text(path: string): string | null;\n /** This method lets the promises that are ready continue, without a move of the clock. */\n settle(): Promise<void>;\n}\n\nconst encoder = new TextEncoder();\nconst decoder = new TextDecoder();\nconst bytesOf = (data: string | Uint8Array): Uint8Array => (typeof data === \"string\" ? encoder.encode(data) : data);\n\n/** This function makes a fake host. Give its `ports` to the host functions in a test. */\nexport const fakeHost = (options: FakeHostOptions = {}): FakeHost => {\n const clock = fakeClock();\n const wallStart = options.wallStart ?? 1_000_000_000_000;\n const wall = (): number => wallStart + clock.now();\n const self = options.self ?? { pid: 100, startedAt: wallStart };\n const files = new Map<string, string | Uint8Array>(Object.entries(options.files ?? {}));\n const commands: RecordedCommand[] = [];\n const signals: RecordedSignal[] = [];\n const children: FakeChild[] = [];\n const processes = new Map<number, number | null>([[self.pid, self.startedAt]]);\n const live = new Map<number, FakeChild>();\n let nextPid = 1000;\n\n const check = (operation: string, path: string): void => {\n if (host.failFiles?.(operation, path) === true) throw new Error(`the fake ${operation} of ${path} failed`);\n };\n\n const politeStop = (pid: number): void => {\n const child = live.get(pid);\n if (host.polite && child !== undefined) queueMicrotask(() => child.exit(0));\n };\n const hardStop = (pid: number, signal: string | null): void => {\n const child = live.get(pid);\n if (child !== undefined) queueMicrotask(() => child.exit(null, signal));\n else processes.delete(pid);\n };\n\n const spawn = (request: SpawnRequest): FakeChild => {\n const failed = host.failSpawn?.(request) === true;\n const pid = failed ? null : nextPid++;\n const outputs: Array<(stream: \"stdout\" | \"stderr\", chunk: string | Uint8Array) => void> = [];\n const exits: Array<(code: number | null, signal: string | null) => void> = [];\n const sent: unknown[] = [];\n let end: { code: number | null; signal: string | null } | null = null;\n const child: FakeChild = {\n request,\n pid,\n ipc: request.ipc,\n sent,\n get ended() {\n return end !== null;\n },\n onOutput(handler) {\n outputs.push(handler);\n },\n onExit(handler) {\n exits.push(handler);\n if (end !== null) handler(end.code, end.signal);\n },\n send(message) {\n if (!request.ipc || end !== null) return false;\n sent.push(message);\n if (pid !== null) politeStop(pid);\n return true;\n },\n exit(code, signal = null) {\n if (end !== null) return;\n end = { code, signal };\n if (pid !== null) {\n processes.delete(pid);\n live.delete(pid);\n }\n for (const handler of exits) handler(code, signal);\n },\n output(stream, chunk) {\n for (const handler of outputs) handler(stream, chunk);\n },\n };\n children.push(child);\n if (pid === null) queueMicrotask(() => child.exit(null));\n else {\n processes.set(pid, wall());\n live.set(pid, child);\n }\n return child;\n };\n\n const signal = (pid: number, sig: StopSignal, group: boolean): boolean => {\n if (!processes.has(pid)) return false;\n signals.push({ pid, signal: sig, group });\n if (sig === \"SIGKILL\") hardStop(pid, \"SIGKILL\");\n else politeStop(pid);\n return true;\n };\n\n const defaultAnswer = (command: RecordedCommand): CommandResult => {\n if (command.file !== \"taskkill\") return { code: 0, out: \"\" };\n const at = command.args.indexOf(\"/PID\");\n const pid = Number(command.args[at + 1]);\n if (at < 0 || !processes.has(pid)) return { code: 128, out: `ERROR: The process \"${command.args[at + 1] ?? \"\"}\" not found.` };\n if (command.args.includes(\"/F\")) {\n hardStop(pid, null);\n return { code: 0, out: `SUCCESS: The process with PID ${pid} has been terminated.` };\n }\n if (host.polite && live.has(pid)) {\n politeStop(pid);\n return { code: 0, out: `SUCCESS: Sent termination signal to the process with PID ${pid}.` };\n }\n return { code: 1, out: `ERROR: The process with PID ${pid} could not be terminated.` };\n };\n\n const ports: HostPorts = {\n platform: options.platform ?? \"linux\",\n commands: {\n async run(file, args, commandOptions) {\n const command: RecordedCommand = { file, args: [...args], options: commandOptions };\n commands.push(command);\n return options.answer?.(command) ?? defaultAnswer(command);\n },\n },\n files: {\n async read(path) {\n check(\"read\", path);\n const data = files.get(path);\n return data === undefined ? null : typeof data === \"string\" ? data : decoder.decode(data);\n },\n async write(path, data) {\n check(\"write\", path);\n files.set(path, data);\n },\n async create(path, data) {\n check(\"create\", path);\n if (files.has(path)) return false;\n files.set(path, data);\n return true;\n },\n async append(path, data) {\n check(\"append\", path);\n const before = files.get(path);\n if (before === undefined) files.set(path, data);\n else if (typeof before === \"string\" && typeof data === \"string\") files.set(path, before + data);\n else {\n const head = bytesOf(before);\n const tail = bytesOf(data);\n const joined = new Uint8Array(head.length + tail.length);\n joined.set(head, 0);\n joined.set(tail, head.length);\n files.set(path, joined);\n }\n },\n async size(path) {\n check(\"size\", path);\n const data = files.get(path);\n return data === undefined ? null : bytesOf(data).length;\n },\n async rename(from, to) {\n check(\"rename\", from);\n const data = files.get(from);\n if (data === undefined) throw new Error(`the fake file ${from} does not exist`);\n files.delete(from);\n files.set(to, data);\n },\n async remove(path) {\n check(\"remove\", path);\n files.delete(path);\n },\n },\n processes: {\n alive: (pid) => processes.has(pid),\n startOf: async (pid) => processes.get(pid) ?? null,\n signal: (pid, sig) => signal(pid, sig, false),\n signalGroup: (pid, sig) => ports.platform !== \"win32\" && live.has(pid) && signal(pid, sig, true),\n spawn,\n },\n net: {\n httpStatus: async (url) => host.http.get(url) ?? null,\n httpText: async (url, _timeout, limit) => {\n const status = host.http.get(url) ?? null;\n if (status === null) return null;\n const bytes = encoder.encode(host.bodies.get(url) ?? \"\");\n return { status, body: decoder.decode(bytes.subarray(0, limit)), complete: bytes.length <= limit };\n },\n tcpConnect: async (hostName, port) => host.tcp.has(`${hostName}:${port}`),\n },\n clock,\n wall,\n self,\n env: options.env ?? {},\n homeDir: options.homeDir ?? \"/home/test\",\n user: () => options.user ?? \"test\",\n ...(options.logger === undefined ? {} : { logger: options.logger }),\n };\n\n const host: FakeHost = {\n ports,\n clock,\n files,\n commands,\n signals,\n children,\n processes,\n http: new Map(),\n bodies: new Map(),\n tcp: new Set(),\n polite: false,\n failFiles: null,\n failSpawn: null,\n text(path) {\n const data = files.get(path);\n return data === undefined ? null : typeof data === \"string\" ? data : decoder.decode(data);\n },\n async settle() {\n for (let turn = 0; turn < 2000; turn += 1) await Promise.resolve();\n },\n };\n return host;\n};\n"],"mappings":";;AAuFA,MAAM,UAAU,IAAI,YAAY;AAChC,MAAM,UAAU,IAAI,YAAY;AAChC,MAAM,WAAW,SAA2C,OAAO,SAAS,WAAW,QAAQ,OAAO,IAAI,IAAI;;AAG9G,MAAa,YAAY,UAA2B,CAAC,MAAgB;CACnE,MAAM,QAAQ,UAAU;CACxB,MAAM,YAAY,QAAQ,aAAa;CACvC,MAAM,aAAqB,YAAY,MAAM,IAAI;CACjD,MAAM,OAAO,QAAQ,QAAQ;EAAE,KAAK;EAAK,WAAW;CAAU;CAC9D,MAAM,QAAQ,IAAI,IAAiC,OAAO,QAAQ,QAAQ,SAAS,CAAC,CAAC,CAAC;CACtF,MAAM,WAA8B,CAAC;CACrC,MAAM,UAA4B,CAAC;CACnC,MAAM,WAAwB,CAAC;CAC/B,MAAM,4BAAY,IAAI,IAA2B,CAAC,CAAC,KAAK,KAAK,KAAK,SAAS,CAAC,CAAC;CAC7E,MAAM,uBAAO,IAAI,IAAuB;CACxC,IAAI,UAAU;CAEd,MAAM,SAAS,WAAmB,SAAuB;EACvD,IAAI,KAAK,YAAY,WAAW,IAAI,MAAM,MAAM,MAAM,IAAI,MAAM,YAAY,UAAU,MAAM,KAAK,QAAQ;CAC3G;CAEA,MAAM,cAAc,QAAsB;EACxC,MAAM,QAAQ,KAAK,IAAI,GAAG;EAC1B,IAAI,KAAK,UAAU,UAAU,KAAA,GAAW,qBAAqB,MAAM,KAAK,CAAC,CAAC;CAC5E;CACA,MA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,OAAO;AACT"}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { Backoff } from "../backoff.js";
|
|
2
|
+
import { MessageShape } from "../process.js";
|
|
3
|
+
import { LogSpec } from "./logs.js";
|
|
4
|
+
import { HostPorts } from "./ports.js";
|
|
5
|
+
import { ReadyProbe } from "./probes.js";
|
|
6
|
+
import { Quantity } from "@mark1russell7/quantity";
|
|
7
|
+
/** A ready spec tells how the supervisor finds that a program is ready. */
|
|
8
|
+
export interface ReadySpec {
|
|
9
|
+
readonly probe: ReadyProbe;
|
|
10
|
+
/** This time is the pause between two uses of the probe. */
|
|
11
|
+
readonly interval: Quantity;
|
|
12
|
+
/** This time is the most that one use of the probe can take. */
|
|
13
|
+
readonly timeout: Quantity;
|
|
14
|
+
/** This time is the most that the program can take to be ready. After it, the supervisor stops the program and starts it again. */
|
|
15
|
+
readonly within: Quantity;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* An alive spec tells how the supervisor finds that a ready program still works. A program can hang and not exit:
|
|
19
|
+
* its process stays, and only a probe finds it. After `failures` failures in sequence, the supervisor stops the
|
|
20
|
+
* program in stages and starts it again after its backoff.
|
|
21
|
+
*/
|
|
22
|
+
export interface AliveSpec {
|
|
23
|
+
readonly probe: ReadyProbe;
|
|
24
|
+
/** This time is the pause between two uses of the probe. The first use is one interval after the program is ready. */
|
|
25
|
+
readonly interval: Quantity;
|
|
26
|
+
/** This time is the most that one use of the probe can take. */
|
|
27
|
+
readonly timeout: Quantity;
|
|
28
|
+
/** This count of failures in sequence stops the program: a whole number of 1 or more. A pass sets the count to 0. */
|
|
29
|
+
readonly failures: number;
|
|
30
|
+
}
|
|
31
|
+
/** A program restart tells how the supervisor starts a program again after it stops. */
|
|
32
|
+
export interface ProgramRestart {
|
|
33
|
+
/** This backoff gives the wait before each start again, in milliseconds, as `retrier` uses it. */
|
|
34
|
+
readonly backoff: Backoff;
|
|
35
|
+
/** This time is how long a program is ready before its count of tries goes back to 0. */
|
|
36
|
+
readonly resetAfter: Quantity;
|
|
37
|
+
}
|
|
38
|
+
/** A stop spec tells how the supervisor stops a program. */
|
|
39
|
+
export interface StopSpec {
|
|
40
|
+
/** This time is the wait for the staged stop, before the supervisor stops the process tree. */
|
|
41
|
+
readonly grace: Quantity;
|
|
42
|
+
/** This message goes on the IPC channel of a child that has one. The default is `"shutdown"`, which `run()` can match. */
|
|
43
|
+
readonly message?: MessageShape | undefined;
|
|
44
|
+
/** This signal goes to a child without an IPC channel on Linux. The default is `SIGTERM`. */
|
|
45
|
+
readonly signal?: "SIGINT" | "SIGTERM" | undefined;
|
|
46
|
+
}
|
|
47
|
+
/** A program spec tells one program of a group, and what it needs before it starts. */
|
|
48
|
+
export interface ProgramSpec {
|
|
49
|
+
/** This name is the name of the program in the group: letters, digits, `.`, `_` and `-`. */
|
|
50
|
+
readonly name: string;
|
|
51
|
+
readonly program: string;
|
|
52
|
+
readonly args?: readonly string[] | undefined;
|
|
53
|
+
readonly cwd?: string | undefined;
|
|
54
|
+
/** These variables go to the program. They replace the variables of the env file and of the host. */
|
|
55
|
+
readonly env?: Readonly<Record<string, string>> | undefined;
|
|
56
|
+
/** This file gives variables to the program at each start (`parseEnvFile`). They replace the variables of the host. */
|
|
57
|
+
readonly envFile?: string | undefined;
|
|
58
|
+
/** These are the programs that must be ready before this program starts. */
|
|
59
|
+
readonly after?: readonly string[] | undefined;
|
|
60
|
+
/** This spec tells when the program is ready. Without it, the program is ready when it starts. */
|
|
61
|
+
readonly ready?: ReadySpec | undefined;
|
|
62
|
+
/** This spec tells how the supervisor finds that the ready program still works. Without it, only an exit starts the program again. */
|
|
63
|
+
readonly alive?: AliveSpec | undefined;
|
|
64
|
+
/** This policy starts the program again after it stops. The default is `DEFAULT_RESTART`. Null does not start it again. */
|
|
65
|
+
readonly restart?: ProgramRestart | null | undefined;
|
|
66
|
+
/** This spec tells the log file. The default is `<stateDir>/logs/<name>.log`, 10 MB, with 3 old files. Null keeps no log. */
|
|
67
|
+
readonly log?: LogSpec | null | undefined;
|
|
68
|
+
/** True gives the child an IPC channel, for the staged stop of a Node program. The default is false. */
|
|
69
|
+
readonly ipc?: boolean | undefined;
|
|
70
|
+
/** This spec tells the stop. The default is a grace of 10 s. */
|
|
71
|
+
readonly stop?: StopSpec | undefined;
|
|
72
|
+
/** These fields go in the discovery file of the program, `<stateDir>/<name>.json`. */
|
|
73
|
+
readonly listen?: {
|
|
74
|
+
readonly port?: number | null | undefined;
|
|
75
|
+
readonly url?: string | null | undefined;
|
|
76
|
+
} | undefined;
|
|
77
|
+
}
|
|
78
|
+
/** A group spec tells a group of programs that one supervisor starts in sequence and keeps alive. */
|
|
79
|
+
export interface GroupSpec {
|
|
80
|
+
readonly name: string;
|
|
81
|
+
/** This folder keeps the lock, the discovery file and the log of each program. */
|
|
82
|
+
readonly stateDir: string;
|
|
83
|
+
readonly programs: readonly ProgramSpec[];
|
|
84
|
+
}
|
|
85
|
+
/** These options go with a supervisor. */
|
|
86
|
+
export interface GroupOptions {
|
|
87
|
+
/** This function gives a number in [0, 1) for the jitter of the backoff. The default is `Math.random`. */
|
|
88
|
+
readonly random?: (() => number) | undefined;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* A program phase tells where a program of a group is.
|
|
92
|
+
* - `waiting`: a program that it needs is not ready.
|
|
93
|
+
* - `starting`: it runs, and its probe did not pass.
|
|
94
|
+
* - `ready`: it runs, and its probe passed.
|
|
95
|
+
* - `backoff`: it stopped, and it starts again after its backoff.
|
|
96
|
+
* - `held`: another instance holds its lock. The supervisor tries again after the backoff.
|
|
97
|
+
* - `exited`: it stopped, and it has no restart.
|
|
98
|
+
* - `stopping`, `stopped`: the stop of the group.
|
|
99
|
+
*/
|
|
100
|
+
export type ProgramPhase = "waiting" | "starting" | "ready" | "backoff" | "held" | "exited" | "stopping" | "stopped";
|
|
101
|
+
/** A program state is the state of one program of a group, for a status. */
|
|
102
|
+
export interface ProgramState {
|
|
103
|
+
readonly name: string;
|
|
104
|
+
readonly phase: ProgramPhase;
|
|
105
|
+
/** This number is the process ID of the child, or null. */
|
|
106
|
+
readonly pid: number | null;
|
|
107
|
+
/** This count is the number of starts again since the start of the group. */
|
|
108
|
+
readonly restarts: number;
|
|
109
|
+
/** This text tells the last problem, for example why the probe failed, or it is null. */
|
|
110
|
+
readonly detail: string | null;
|
|
111
|
+
}
|
|
112
|
+
/** A group supervisor starts the programs of a group in sequence, starts each again after it stops, and stops them in stages. */
|
|
113
|
+
export interface GroupSupervisor {
|
|
114
|
+
/** This method starts each program whose programs are ready. A second use does nothing. */
|
|
115
|
+
start(): void;
|
|
116
|
+
/** This method stops each program in the reverse sequence of the start. A second use gives the same promise. */
|
|
117
|
+
stop(): Promise<void>;
|
|
118
|
+
/** This method gives the state of each program, in the sequence of the spec. */
|
|
119
|
+
states(): readonly ProgramState[];
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* This restart is the default. The first wait is 1 s, and each wait after it is two times longer, to 1 min.
|
|
123
|
+
* The jitter subtracts up to half of each wait. The count of tries goes back to 0 after 1 min ready.
|
|
124
|
+
*/
|
|
125
|
+
export declare const DEFAULT_RESTART: ProgramRestart;
|
|
126
|
+
/**
|
|
127
|
+
* This function gives the start sequence of a group, as layers of names. Each program of a layer needs only programs of the
|
|
128
|
+
* layers before it. A program in a cycle, or after an unknown program, is in no layer. `groupProblems` names it.
|
|
129
|
+
*/
|
|
130
|
+
export declare const startOrder: (spec: GroupSpec) => string[][];
|
|
131
|
+
/** This function gives the problems of a group spec: each value that the supervisor cannot use. */
|
|
132
|
+
export declare const groupProblems: (spec: GroupSpec) => string[];
|
|
133
|
+
/**
|
|
134
|
+
* This function makes the supervisor of a group. It refuses a spec with problems. Each effect goes
|
|
135
|
+
* through the ports, thus a test gives `fakeHost()` and starts no process.
|
|
136
|
+
* - A program starts when each program of its `after` is ready. It takes its lock first, thus a second instance does not start.
|
|
137
|
+
* - A program with a probe is ready when its probe passes. A program that is not ready in `within` stops, and starts again.
|
|
138
|
+
* - A program that stops starts again after the backoff of its `retrier`, when its programs are ready.
|
|
139
|
+
* - Its output goes to its rotating log. Its environment is the host, then its env file, then its `env`.
|
|
140
|
+
* - The stop goes in the reverse sequence: the staged stop first, and after the grace, the stop of the process tree by its ID.
|
|
141
|
+
*/
|
|
142
|
+
export declare const superviseGroup: (spec: GroupSpec, ports: HostPorts, options?: GroupOptions) => GroupSupervisor;
|