-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
55 lines (49 loc) · 1.42 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
package main
import (
"context"
"log"
"time"
"github.com/gbeletti/service-golang/dbmongo"
"github.com/gbeletti/service-golang/httpserver"
"github.com/gbeletti/service-golang/queuerabbit"
"github.com/gbeletti/service-golang/servicemanager"
"github.com/joho/godotenv"
)
func init() {
err := godotenv.Load()
if err != nil {
log.Println("no .env file found")
}
}
func main() {
_, cancel := start()
defer shutdown(cancel)
servicemanager.WaitShutdown()
}
func start() (ctx context.Context, cancel context.CancelFunc) {
// This is the main context for the service. When it is canceled it means the service is going down.
// All the tasks must be canceled
ctx, cancel = context.WithCancel(context.Background())
if err := dbmongo.Start(); err != nil {
log.Printf("couldnt start database error [%s]\n", err)
}
queuerabbit.Start(ctx)
httpserver.Start()
return
}
func shutdown(cancel context.CancelFunc) {
cancel()
ctx, cancelTimeout := context.WithTimeout(context.Background(), time.Second*30)
defer cancelTimeout()
doneHTTP := httpserver.Shutdown(ctx)
doneRabbit := queuerabbit.Shutdown(ctx)
err := servicemanager.WaitUntilIsDoneOrCanceled(ctx, doneHTTP, doneRabbit)
if err != nil {
log.Printf("service stopped by timeout %s\n", err)
}
if err := dbmongo.Close(context.Background()); err != nil {
log.Printf("couldnt close mongo connection error [%s]\n", err)
}
time.Sleep(time.Millisecond * 200)
log.Println("bye bye")
}