-
Notifications
You must be signed in to change notification settings - Fork 46
/
app.go
96 lines (82 loc) · 2.04 KB
/
app.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
package app
import (
"context"
"errors"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/golangid/candi/codebase/factory"
)
// App service
type App struct {
service factory.ServiceFactory
}
// New service app
func New(service factory.ServiceFactory) *App {
return &App{
service: service,
}
}
// Run start app
func (a *App) Run() {
if err := a.checkRequired(); err != nil {
panic(err)
}
errServe := make(chan error)
checkExist := make(map[string]struct{})
for _, app := range a.service.GetApplications() {
if _, ok := checkExist[app.Name()]; ok {
panic("Register application: " + app.Name() + " has been registered")
}
checkExist[app.Name()] = struct{}{}
go func(srv factory.AppServerFactory) {
defer func() {
if r := recover(); r != nil {
errServe <- fmt.Errorf("%v", r)
}
}()
srv.Serve()
}(app)
}
quitSignal := make(chan os.Signal, 1)
signal.Notify(quitSignal, os.Interrupt, syscall.SIGTERM)
log.Printf("Application \x1b[32;1m%s\x1b[0m ready to run\n\n", a.service.Name())
select {
case e := <-errServe:
panic(e)
case <-quitSignal:
a.shutdown(quitSignal)
}
}
// graceful shutdown all server, panic if there is still a process running when the request exceed given timeout in context
func (a *App) shutdown(forceShutdown chan os.Signal) {
fmt.Println("\x1b[34;1mGracefully shutdown... (press Ctrl+C again to force)\x1b[0m")
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
done := make(chan struct{})
go func() {
defer close(done)
for _, server := range a.service.GetApplications() {
server.Shutdown(ctx)
}
done <- struct{}{}
}()
select {
case <-done:
log.Println("\x1b[32;1mSuccess shutdown all server & worker\x1b[0m")
case <-forceShutdown:
log.Println("\x1b[31;1mForce shutdown server & worker\x1b[0m")
cancel()
case <-ctx.Done():
log.Println("\x1b[31;1mContext timeout\x1b[0m")
}
}
func (a *App) checkRequired() (err error) {
if len(a.service.GetApplications()) == 0 {
return errors.New("No server/worker running")
}
return
}