@decohere/core 0.0.1-dev.20261110.318 → 0.0.1-dev.20261120.333
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/es6/src/hosting/app.js +43 -1
- package/es6/src/hosting/hosted-service-host.js +38 -4
- package/es6/src/hosting/hosted-service.js +2 -2
- package/package.json +2 -2
- package/types/src/hosting/app.d.ts +21 -1
- package/types/src/hosting/hosted-service-host.d.ts +14 -1
- package/types/src/hosting/hosted-service.d.ts +4 -3
package/es6/src/hosting/app.js
CHANGED
|
@@ -48,6 +48,7 @@ export class App {
|
|
|
48
48
|
disposingContainer = false;
|
|
49
49
|
signalHandler;
|
|
50
50
|
shutdownSignals = [];
|
|
51
|
+
hostedStarting;
|
|
51
52
|
constructor(container, configurationService, server, hostedServices, startSteps){
|
|
52
53
|
this.container = container;
|
|
53
54
|
this.configurationService = configurationService;
|
|
@@ -70,10 +71,27 @@ export class App {
|
|
|
70
71
|
await step(this.container);
|
|
71
72
|
if (this.disposing != null) return undefined;
|
|
72
73
|
}
|
|
74
|
+
if (options?.hostedServices === "after-listen") {
|
|
75
|
+
const startResponse = await this.listen(options);
|
|
76
|
+
if (this.disposing == null) this.hostedStarting = this.startHostedServicesAfterListen();
|
|
77
|
+
return startResponse;
|
|
78
|
+
}
|
|
73
79
|
// Before the server, so a request never reaches a service that has not started.
|
|
74
|
-
|
|
80
|
+
this.hostedStarting = this.startHostedServices();
|
|
81
|
+
await this.hostedStarting;
|
|
75
82
|
// The app was stopped while a hosted service was still starting.
|
|
76
83
|
if (this.disposing != null) return undefined;
|
|
84
|
+
return this.listen(options);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Resolves once every hosted service has started, with how long each one took, in start order.
|
|
88
|
+
* With `hostedServices: "after-listen"`, await it in `main.ts` after `start()`: a failure
|
|
89
|
+
* rejects it, after the app has stopped. Left unawaited, that rejection is unhandled and ends
|
|
90
|
+
* the process, as a failing `await app.start()` would.
|
|
91
|
+
*/ hostedServicesStarted() {
|
|
92
|
+
return this.hostedStarting ?? Promise.reject(new Error("hostedServicesStarted() before start()"));
|
|
93
|
+
}
|
|
94
|
+
async listen(options) {
|
|
77
95
|
const hasHostedServices = this.hostedServices != null && !this.hostedServices.isEmpty;
|
|
78
96
|
if (!this.server) {
|
|
79
97
|
if (hasHostedServices) this.stopOnSignals(options);
|
|
@@ -94,6 +112,30 @@ export class App {
|
|
|
94
112
|
this.stopOnSignals(options);
|
|
95
113
|
return startResponse;
|
|
96
114
|
}
|
|
115
|
+
async startHostedServices() {
|
|
116
|
+
const host = this.hostedServices;
|
|
117
|
+
if (host == null || host.isEmpty) return [];
|
|
118
|
+
const begun = performance.now();
|
|
119
|
+
await host.start(this.container);
|
|
120
|
+
const timings = host.timings;
|
|
121
|
+
this.logger.info(`Started ${timings.length} hosted services in ${Math.round(performance.now() - begun)} ms`);
|
|
122
|
+
return timings;
|
|
123
|
+
}
|
|
124
|
+
/** The server is already up, so a service that fails to start takes the app down with it. */ async startHostedServicesAfterListen() {
|
|
125
|
+
try {
|
|
126
|
+
return await this.startHostedServices();
|
|
127
|
+
} catch (startError) {
|
|
128
|
+
try {
|
|
129
|
+
await this.dispose();
|
|
130
|
+
} catch (stopError) {
|
|
131
|
+
throw new AggregateError([
|
|
132
|
+
startError,
|
|
133
|
+
stopError
|
|
134
|
+
], "A hosted service failed to start, and stopping the app failed too");
|
|
135
|
+
}
|
|
136
|
+
throw startError;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
97
139
|
[Symbol.asyncDispose]() {
|
|
98
140
|
// The container holds this App too; when stop() disposes the container it lands
|
|
99
141
|
// here, and awaiting our own shutdown would deadlock.
|
|
@@ -7,7 +7,9 @@ import { scribe } from "@decohere/scribe";
|
|
|
7
7
|
types = [];
|
|
8
8
|
controller = new AbortController();
|
|
9
9
|
/** In start order; stopped in reverse. */ started = [];
|
|
10
|
+
_timings = [];
|
|
10
11
|
starting;
|
|
12
|
+
startSettled = false;
|
|
11
13
|
stopping;
|
|
12
14
|
add(...types) {
|
|
13
15
|
this.types.push(...types);
|
|
@@ -32,16 +34,43 @@ import { scribe } from "@decohere/scribe";
|
|
|
32
34
|
this.stopping ??= this.stopAll();
|
|
33
35
|
return this.stopping;
|
|
34
36
|
}
|
|
35
|
-
/**
|
|
36
|
-
return
|
|
37
|
+
/** Each started service's start time, in start order. */ get timings() {
|
|
38
|
+
return [
|
|
39
|
+
...this._timings
|
|
40
|
+
];
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* For `GET /ready`: while a start is under way, one check that fails (the app listened before
|
|
44
|
+
* its services started); then the started services that report readiness.
|
|
45
|
+
*/ readinessChecks() {
|
|
46
|
+
const checks = [];
|
|
47
|
+
if (this.starting != null && !this.startSettled) {
|
|
48
|
+
checks.push({
|
|
49
|
+
name: "hosted services starting",
|
|
50
|
+
check: ()=>Promise.reject(new Error("hosted services are still starting"))
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
for (const service of this.started){
|
|
54
|
+
if (typeof service.check !== "function") continue;
|
|
55
|
+
checks.push({
|
|
37
56
|
name: service.constructor.name,
|
|
38
57
|
check: ()=>service.check?.() ?? Promise.resolve()
|
|
39
|
-
})
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
return checks;
|
|
40
61
|
}
|
|
41
62
|
async startAll(container) {
|
|
63
|
+
try {
|
|
64
|
+
await this.startEach(container);
|
|
65
|
+
} finally{
|
|
66
|
+
this.startSettled = true;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
async startEach(container) {
|
|
42
70
|
const { signal } = this.controller;
|
|
43
71
|
for (const type of this.types){
|
|
44
72
|
if (signal.aborted) return;
|
|
73
|
+
const begun = performance.now();
|
|
45
74
|
const service = container.resolve(type);
|
|
46
75
|
try {
|
|
47
76
|
await service.start(signal);
|
|
@@ -53,7 +82,12 @@ import { scribe } from "@decohere/scribe";
|
|
|
53
82
|
throw error;
|
|
54
83
|
}
|
|
55
84
|
this.started.push(service);
|
|
56
|
-
|
|
85
|
+
const ms = Math.round(performance.now() - begun);
|
|
86
|
+
this._timings.push({
|
|
87
|
+
name: type.name,
|
|
88
|
+
ms
|
|
89
|
+
});
|
|
90
|
+
this.logger.debug(`Hosted service ${type.name} started in ${ms} ms`);
|
|
57
91
|
}
|
|
58
92
|
}
|
|
59
93
|
async stopAll() {
|
|
@@ -3,6 +3,6 @@
|
|
|
3
3
|
* Register it with `builder.withHostedService(Type)`; it is a singleton, so other services can
|
|
4
4
|
* inject it by its class.
|
|
5
5
|
*
|
|
6
|
-
* The app starts hosted services in registration order before the HTTP server listens
|
|
7
|
-
* them in reverse order when it stops (`app.dispose()`, SIGTERM or SIGINT, a Flux reboot).
|
|
6
|
+
* The app starts hosted services in registration order before the HTTP server listens (or after,
|
|
7
|
+
* with `app.start({ hostedServices: "after-listen" })`), and stops them in reverse order when it stops (`app.dispose()`, SIGTERM or SIGINT, a Flux reboot).
|
|
8
8
|
*/ export { };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@decohere/core",
|
|
3
|
-
"version": "0.0.1-dev.
|
|
3
|
+
"version": "0.0.1-dev.20261120.333",
|
|
4
4
|
"description": "The runtime base every Decohere package builds on: reflection, composition, configuration types, extensibility, collections, objects, caching on a Map, and basic file IO. No side effects on import.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
69
69
|
},
|
|
70
70
|
"dependencies": {
|
|
71
|
-
"@decohere/scribe": "0.0.1-dev.
|
|
71
|
+
"@decohere/scribe": "0.0.1-dev.20261120.333",
|
|
72
72
|
"reflect-metadata": "^0.2.2"
|
|
73
73
|
},
|
|
74
74
|
"devDependencies": {}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type IContainer, type LifetimeScope } from "../composition/index.js";
|
|
2
2
|
import { type IConfigurationService } from "../configuration/index.js";
|
|
3
3
|
import { type StartSteps } from "./const.js";
|
|
4
|
-
import type { HostedServiceHost } from "./hosted-service-host.js";
|
|
4
|
+
import type { HostedServiceHost, HostedServiceStartTiming } from "./hosted-service-host.js";
|
|
5
5
|
export declare class ApiConfiguration {
|
|
6
6
|
static schema: {
|
|
7
7
|
type: string;
|
|
@@ -39,6 +39,14 @@ export type StartOptions = {
|
|
|
39
39
|
shutdownSignals?: readonly NodeJS.Signals[] | false;
|
|
40
40
|
/** How long a signalled shutdown may take before the process exits with code 1. Default 10s. */
|
|
41
41
|
shutdownTimeoutMs?: number;
|
|
42
|
+
/**
|
|
43
|
+
* When hosted services start. `"before-listen"` (default): `start()` resolves once every one
|
|
44
|
+
* has started, and a failure rejects it. `"after-listen"`: the server listens first and they
|
|
45
|
+
* start in the background, in the same order, while `GET /ready` answers 503; a failure stops
|
|
46
|
+
* the app and rejects {@link App.hostedServicesStarted}. For a host with a start budget, such
|
|
47
|
+
* as Cloud Run.
|
|
48
|
+
*/
|
|
49
|
+
hostedServices?: "before-listen" | "after-listen";
|
|
42
50
|
};
|
|
43
51
|
export declare class App {
|
|
44
52
|
private readonly container;
|
|
@@ -52,6 +60,7 @@ export declare class App {
|
|
|
52
60
|
private disposingContainer;
|
|
53
61
|
private signalHandler;
|
|
54
62
|
private shutdownSignals;
|
|
63
|
+
private hostedStarting;
|
|
55
64
|
constructor(container: LifetimeScope, configurationService?: IConfigurationService, server?: AppServer, hostedServices?: HostedServiceHost, startSteps?: StartSteps);
|
|
56
65
|
/**
|
|
57
66
|
* The app's root container, for a tool or a test that builds the app to use its services
|
|
@@ -60,6 +69,17 @@ export declare class App {
|
|
|
60
69
|
get services(): IContainer;
|
|
61
70
|
get addresses(): readonly string[];
|
|
62
71
|
start(options?: StartOptions): Promise<AppServerStartResponse | undefined>;
|
|
72
|
+
/**
|
|
73
|
+
* Resolves once every hosted service has started, with how long each one took, in start order.
|
|
74
|
+
* With `hostedServices: "after-listen"`, await it in `main.ts` after `start()`: a failure
|
|
75
|
+
* rejects it, after the app has stopped. Left unawaited, that rejection is unhandled and ends
|
|
76
|
+
* the process, as a failing `await app.start()` would.
|
|
77
|
+
*/
|
|
78
|
+
hostedServicesStarted(): Promise<readonly HostedServiceStartTiming[]>;
|
|
79
|
+
private listen;
|
|
80
|
+
private startHostedServices;
|
|
81
|
+
/** The server is already up, so a service that fails to start takes the app down with it. */
|
|
82
|
+
private startHostedServicesAfterListen;
|
|
63
83
|
[Symbol.asyncDispose](): Promise<void>;
|
|
64
84
|
/** Stop the app. Calling it again (or from a signal) returns the same shutdown. */
|
|
65
85
|
dispose(): Promise<void>;
|
|
@@ -2,6 +2,11 @@ import type { IContainer } from "../composition/index.js";
|
|
|
2
2
|
import type { Constructable } from "../reflection/index.js";
|
|
3
3
|
import type { IReadinessCheck } from "./readiness.types.js";
|
|
4
4
|
import type { HostedService } from "./hosted-service.js";
|
|
5
|
+
/** How long one hosted service took to start: resolving it from the container plus its `start()`. */
|
|
6
|
+
export type HostedServiceStartTiming = {
|
|
7
|
+
name: string;
|
|
8
|
+
ms: number;
|
|
9
|
+
};
|
|
5
10
|
/**
|
|
6
11
|
* Starts and stops the app's {@link HostedService}s. One per app; `Builder.withHostedService`
|
|
7
12
|
* adds the types and `App` drives it.
|
|
@@ -12,7 +17,9 @@ export declare class HostedServiceHost {
|
|
|
12
17
|
private readonly controller;
|
|
13
18
|
/** In start order; stopped in reverse. */
|
|
14
19
|
private readonly started;
|
|
20
|
+
private readonly _timings;
|
|
15
21
|
private starting;
|
|
22
|
+
private startSettled;
|
|
16
23
|
private stopping;
|
|
17
24
|
add(...types: Constructable<HostedService>[]): void;
|
|
18
25
|
get isEmpty(): boolean;
|
|
@@ -28,9 +35,15 @@ export declare class HostedServiceHost {
|
|
|
28
35
|
* reverse order. Every `stop()` runs even if one throws; the failures are rethrown after.
|
|
29
36
|
*/
|
|
30
37
|
stop(): Promise<void>;
|
|
31
|
-
/**
|
|
38
|
+
/** Each started service's start time, in start order. */
|
|
39
|
+
get timings(): readonly HostedServiceStartTiming[];
|
|
40
|
+
/**
|
|
41
|
+
* For `GET /ready`: while a start is under way, one check that fails (the app listened before
|
|
42
|
+
* its services started); then the started services that report readiness.
|
|
43
|
+
*/
|
|
32
44
|
readinessChecks(): IReadinessCheck[];
|
|
33
45
|
private startAll;
|
|
46
|
+
private startEach;
|
|
34
47
|
private stopAll;
|
|
35
48
|
/** Stop what is started, newest first. Takes each service off the list, so it runs once. */
|
|
36
49
|
private stopStarted;
|
|
@@ -3,12 +3,13 @@
|
|
|
3
3
|
* Register it with `builder.withHostedService(Type)`; it is a singleton, so other services can
|
|
4
4
|
* inject it by its class.
|
|
5
5
|
*
|
|
6
|
-
* The app starts hosted services in registration order before the HTTP server listens
|
|
7
|
-
* them in reverse order when it stops (`app.dispose()`, SIGTERM or SIGINT, a Flux reboot).
|
|
6
|
+
* The app starts hosted services in registration order before the HTTP server listens (or after,
|
|
7
|
+
* with `app.start({ hostedServices: "after-listen" })`), and stops them in reverse order when it stops (`app.dispose()`, SIGTERM or SIGINT, a Flux reboot).
|
|
8
8
|
*/
|
|
9
9
|
export interface HostedService {
|
|
10
10
|
/**
|
|
11
|
-
* Called once when the app starts, and awaited: a throw fails `app.start()
|
|
11
|
+
* Called once when the app starts, and awaited: a throw fails `app.start()` (after-listen: it
|
|
12
|
+
* stops the app and rejects `app.hostedServicesStarted()`). Do what must be
|
|
12
13
|
* ready before traffic, then start long work (a first load that takes minutes, a timer) in
|
|
13
14
|
* the background and return. Waiting for it here keeps the HTTP server dark.
|
|
14
15
|
*
|