master
go 35 lines 1020 Bytes
Raw
1 // Package shutdown tracks daemon-wide graceful shutdown state. The daemon
2 // command marks shutdown started when SIGTERM/SIGINT is received; the
3 // "ipfs diag healthy" subcommand checks this state for Dockerfile
4 // HEALTHCHECK and other monitoring.
5 package shutdown
6
7 import (
8 "sync/atomic"
9 "time"
10 )
11
12 // startedAt holds the unix-nano timestamp when shutdown began.
13 // Zero means shutdown has not started.
14 var startedAt atomic.Int64
15
16 // MarkStarted records that graceful shutdown has begun. Safe to call
17 // multiple times concurrently; only the first call wins. Returns true on
18 // the first call, false on subsequent calls.
19 func MarkStarted() bool {
20 return startedAt.CompareAndSwap(0, time.Now().UnixNano())
21 }
22
23 // StartedAt returns when shutdown began, or the zero time if not started.
24 func StartedAt() time.Time {
25 n := startedAt.Load()
26 if n == 0 {
27 return time.Time{}
28 }
29 return time.Unix(0, n)
30 }
31
32 // InProgress reports whether shutdown has been initiated.
33 func InProgress() bool {
34 return startedAt.Load() != 0
35 }