You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Manages the clean startup and shutdown of a process. Register lifecycle components in the order they will be initialized, and then call `start()` on the lifecycle. When the lifecycle manager receives a SIGTERM or if the `close()` method is called, it will begin the shutdown process. The lifecycle manager closes each component in the reverse order of their registration
3
+
Manages the clean startup and shutdown of a process. Register lifecycle components in the order they will be initialized, and then call `start()` on the lifecycle. When the lifecycle manager receives a SIGTERM or if the `close()` method is called, it will begin the shutdown process. The lifecycle manager closes each component in the reverse order of their registration
4
+
5
+
## Usage
6
+
7
+
```ts
8
+
import { Lifecycle } from'@antman/lifecycle';
9
+
10
+
if (import.meta.main) {
11
+
const lifecycle =newLifecycle();
12
+
lifecycle.all(console.log);
13
+
lifecycle.register(db);
14
+
lifecycle.register(webserver);
15
+
lifecycle.register(outbox);
16
+
awaitlifecycle.start();
17
+
}
18
+
```
19
+
20
+
Where each component is defined as a lifecycle component, in one of two ways:
* Define the frequency of component health check cycles. Note: This is the interval in which the lifecycle manager will begin polling each component's status -- the interval begins once each component has returned its status. Components returning their status in a promise can delay subsequent health checks.
8
12
*/
9
-
healthCheckIntervalMs: number;
13
+
healthCheckIntervalMs?: number;
10
14
};
11
-
typeOptions=Partial<Opt>;
12
15
conststatuses=[
13
16
'pending',
14
17
'starting',
@@ -27,7 +30,7 @@ export type LifecycleComponent = {
27
30
/**
28
31
* Provide a name to identify the component in events emitted by LifecycleManager -- useful for logging
29
32
*/
30
-
name: string;
33
+
readonlyname: string;
31
34
/**
32
35
* The status property will be used as a healthcheck; if the component status becomes 'crashed', the lifecycle manager will call `restart` if it exists, or `start` if it doesn't
33
36
*/
@@ -45,7 +48,6 @@ export type LifecycleComponent = {
* Manages the clean startup and shutdown of a process
70
+
* Manages the clean startup and shutdown of a process and its components.
66
71
*
67
72
* Register lifecycle components in the order they will be initialized, and then call `start()` on the lifecycle. When the lifecycle manager receives a SIGTERM or if the `close()` method is called, it will begin the shutdown process. The lifecycle manager closes each component in the reverse order of their registration
68
73
*/
@@ -77,7 +82,9 @@ export class Lifecycle {
77
82
this.#emit(v);
78
83
}
79
84
#emit(event: keyofEventMap,name?: string): void{
80
-
this.#emitter.emit(event,name);
85
+
(isComponentEvent(event)&&isDefined(name))
86
+
? this.#emitter.emit(event,name)
87
+
: this.#emitter.emit(event);
81
88
}
82
89
/**
83
90
* Provide a callback for events emitted by lifecycle manager
0 commit comments