-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
70 lines (54 loc) · 1.29 KB
/
main.go
File metadata and controls
70 lines (54 loc) · 1.29 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
64
65
66
67
68
69
70
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/blazity/enterprise-cli/pkg/enterprise"
"github.com/blazity/enterprise-cli/pkg/logging"
)
var GlobalCtx context.Context
var GlobalCancel context.CancelFunc
func main() {
GlobalCtx, GlobalCancel = context.WithCancel(context.Background())
defer GlobalCancel()
logger := enterprise.InitializeLogger(false)
setupSignalHandling()
cmd := enterprise.NewEnterpriseCommand(GlobalCtx, GlobalCancel)
select {
case <-GlobalCtx.Done():
logger.Info("Operation was cancelled before execution")
os.Exit(1)
default:
if err := cmd.ExecuteContext(GlobalCtx); err != nil {
logger.Error(err.Error())
os.Exit(1)
}
}
}
func setupSignalHandling() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
sig := <-c
fmt.Print("\r\033[K")
logger := logging.GetLogger()
logger.Info("Received signal: " + sig.String())
logger.Info("Cancelling operations...")
GlobalCancel()
cleanup := make(chan struct{})
go func() {
time.Sleep(100 * time.Millisecond)
close(cleanup)
}()
select {
case <-cleanup:
logger.Info("Graceful shutdown completed")
case <-time.After(2 * time.Second):
logger.Warning("Cleanup timed out, forcing exit")
}
os.Exit(1)
}()
}