-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartServices.go
92 lines (71 loc) · 2.12 KB
/
StartServices.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
package main
import (
"context"
"os"
"os/signal"
"syscall"
"time"
libremotebuild "github.com/RemoteBuild/LibRemotebuild"
"github.com/RemoteBuild/Remotebuild/handlers"
"github.com/RemoteBuild/Remotebuild/services"
"github.com/gorilla/mux"
"gorm.io/gorm"
log "github.com/sirupsen/logrus"
)
// Services
var (
apiService *services.APIService // Handle endpoints
jobService *services.JobService // Handle Jobs
cleanupService *services.CleanupService // Cleanup db stuff
containerService *services.ContainerService // Managing containers
)
func startAPI() {
log.Info("Starting version " + version)
// Create new container service
containerService = services.NewContainerService(config)
// Create and start the jobservice
jobService = services.NewJobService(config, db, func(jobType libremotebuild.JobType) (string, error) {
return containerService.GetContainer(jobType)
})
jobService.Start()
// Create and start required services
apiService = services.NewAPIService(config, func() *mux.Router {
return handlers.NewRouter(config, db, jobService)
})
apiService.Start()
// Create cleanup service
cleanupService = services.NewClienupService(config, db)
cleanupService.Start()
// Startup done
log.Info("Startup completed")
awaitExit(apiService, db)
}
// Shutdown server gracefully
func awaitExit(httpServer *services.APIService, db *gorm.DB) {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, os.Interrupt, syscall.SIGKILL, syscall.SIGTERM)
// await os signal
<-signalChan
// Stop all jobs
jobService.Stop()
// Create a deadline for the await
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
defer cancel()
log.Info("Shutting down server")
if httpServer.HTTPServer != nil {
err := httpServer.HTTPServer.Shutdown(ctx)
if err != nil {
log.Warn(err)
}
log.Info("HTTP server shutdown complete")
}
if httpServer.HTTPTLSServer != nil {
err := httpServer.HTTPTLSServer.Shutdown(ctx)
if err != nil {
log.Warn(err)
}
log.Info("HTTPs server shutdown complete")
}
log.Info("Shutting down complete")
os.Exit(0)
}