forked from esolveeg/new_forkee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
112 lines (92 loc) · 2.87 KB
/
main.go
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"net/http"
"context"
"errors"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/darwishdev/devkit-api/api"
"github.com/darwishdev/devkit-api/config"
"github.com/darwishdev/devkit-api/db"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// operation is a clean up function on shutting down
type operation func(ctx context.Context) error
// gracefulShutdown waits for termination syscalls and doing clean up operations after received it
func gracefulShutdown(ctx context.Context, timeout time.Duration, ops map[string]operation) <-chan struct{} {
wait := make(chan struct{})
go func() {
s := make(chan os.Signal, 1)
// add any other syscalls that you want to be notified with
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
<-s
log.Info().Msg("shutting down")
// set timeout for the ops to be done to prevent system hang
timeoutFunc := time.AfterFunc(timeout, func() {
log.Printf("timeout %d ms has been elapsed, force exit", timeout.Milliseconds())
os.Exit(0)
})
defer timeoutFunc.Stop()
var wg sync.WaitGroup
// Do the operations asynchronously to save time
for key, op := range ops {
wg.Add(1)
innerOp := op
innerKey := key
go func() {
defer wg.Done()
log.Printf("cleaning up: %s", innerKey)
if err := innerOp(ctx); err != nil {
log.Printf("%s: clean up failed: %s", innerKey, err.Error())
return
}
log.Printf("%s was shutdown gracefully", innerKey)
}()
}
wg.Wait()
close(wait)
}()
return wait
}
func main() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
ctx := context.Background()
state, err := config.LoadState("./config")
if err != nil {
log.Fatal().Err(err).Msg("cannot load the state config")
}
config, err := config.LoadConfig("./config", state.State)
store, connPool, err := db.InitDB(ctx, config.DBSource, config.State == "dev")
if err != nil {
log.Fatal().Str("DBSource", config.DBSource).Err(err).Msg("db failed to connect")
}
server, err := api.NewServer(config, store) // Start the server in a goroutine
if err != nil {
log.Fatal().Err(err).Msg("server initialization failed")
}
httpServer := server.NewGrpcHttpServer()
go func() {
log.Info().Str("server address", config.GRPCServerAddress).Msg("GRPC server start")
if err := httpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal().Err(err).Msg("HTTP listen and serve failed")
}
}()
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, syscall.SIGTERM)
wait := gracefulShutdown(ctx, 3*time.Second, map[string]operation{
"database": func(ctx context.Context) error {
connPool.Close()
return nil
},
"http-server": func(ctx context.Context) error {
return httpServer.Shutdown(ctx)
},
// Add other cleanup operations here
})
<-wait
}