-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoc.go
More file actions
59 lines (43 loc) · 2.27 KB
/
doc.go
File metadata and controls
59 lines (43 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
Package lifecycle provides a robust control plane for modern Go applications.
It unifies Death Management (Signals, Shutdown) with Life Management (Supervision, Events).
# Core Concepts
The library is built around three pillars:
1. Signal Context (The Foundation):
Manages the application's lifecycle, handling OS signals (SIGINT/SIGTERM) and
propagating cancellation via context.Context.
ctx := lifecycle.NewSignalContext(context.Background())
<-ctx.Done()
2. Supervisor (The Bridge):
Manages a tree of Workers (Processes, Containers, Goroutines), ensuring they
adhere to the parent's lifecycle. It supports restart strategies (OneForOne,
OneForAll), persistent identity (Handover), and state introspection via Workers().
sup := lifecycle.NewSupervisor("root", lifecycle.SupervisorStrategyOneForOne)
sup.Add(spec)
states := sup.Workers()
3. Control Plane (The Orchestrator):
Decouples "Events" (Triggers) from "Handlers" (Reactions) via a Router.
This allows the application to react to external stimuli (Input, Webhooks) dynamically.
router := lifecycle.NewRouter()
router.Handle("signal/interrupt", handler)
# Root Package Aliases
For convenience, this package exposes the most commonly used types and constructors
from the internal `pkg/` structure, grouped by functionality:
- Runtime: lifecycle.Run (supports WithLogger, WithMetrics), lifecycle.Go (returns Task, supports WithErrorHandler), lifecycle.Do
- Signals: lifecycle.NewSignalContext, lifecycle.WithForceExit
- Supervision: lifecycle.NewSupervisor, lifecycle.NewProcessWorker
- Control: lifecycle.NewRouter, lifecycle.Handle
- Interactive: lifecycle.NewInteractiveRouter, lifecycle.WithShutdown
# Interactive Applications
For CLI tools, use the Interactive Router preset to wire up signals and input automatically:
router := lifecycle.NewInteractiveRouter(
lifecycle.WithSuspendOnInterrupt(suspendHandler),
lifecycle.WithShutdown(quitFunc),
)
lifecycle.Run(router)
# Configuration
Configuration is done via Functional Options (SignalOption) and Struct Specs (SupervisorSpec).
We adopt the "Stdlib Pattern": providing a `DefaultRouter` for convenience ("Managed Global State")
while allowing explicit `NewRouter` for strict isolation.
*/
package lifecycle