-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
63 lines (53 loc) · 1.83 KB
/
main.go
File metadata and controls
63 lines (53 loc) · 1.83 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
60
61
62
63
package main
import (
"context"
"fmt"
"time"
"github.com/aretw0/lifecycle"
)
func main() {
// Use lifecycle.Run to manage the SignalContext automatically.
// It handles:
// 1. Context Creation (SIGINT/SIGTERM)
// 2. Monitoring Goroutine Cleanup (Stop)
// 3. Waiting for Hooks (Wait) on signal
err := lifecycle.Run(lifecycle.Job(runApp))
if err != nil && !lifecycle.IsInterrupted(err) {
fmt.Printf("Exit error: %v\n", err)
}
fmt.Println("All cleanups done. Goodbye!")
}
func runApp(ctx context.Context) error {
fmt.Println("Application started. Press Ctrl+C to shutdown.")
// Register a simple hook (LIFO execution)
// Use the facade helper to avoid manual casting.
lifecycle.OnShutdown(ctx, func() {
fmt.Println("[Hook 3] Fastest cleanup (runs last)")
})
// Register a slow hook to demonstrate Wait()
lifecycle.OnShutdown(ctx, func() {
fmt.Println("[Hook 2] Slow cleanup starting... (runs 2nd)")
time.Sleep(1 * time.Second)
fmt.Println("[Hook 2] Slow cleanup finished.")
})
// Dynamic Hook Registration
lifecycle.OnShutdown(ctx, func() {
fmt.Println("[Hook 1] Main cleanup (runs 1st)")
// You can register more hooks from INSIDE a hook!
// They will be executed immediately after this hook returns (LIFO-ish).
lifecycle.OnShutdown(ctx, func() {
fmt.Println("[Hook 1.1] Dynamic sub-task")
})
})
// Simulate application work
// Using the helper Sleep instead of select!
if err := lifecycle.Sleep(ctx, 10*time.Second); err != nil {
// To check the specific reasons (like IsInterrupted), we might generally just return err.
// If we want the Reason string, we'd need to cast, but usually err is enough.
// fmt.Printf("\nShutdown signal received! (Reason: %s)\n", sCtx.Reason())
fmt.Printf("\nShutdown signal received! (%v)\n", err)
return err
}
fmt.Println("\nTimeout! Exiting normally.")
return nil
}