-
Notifications
You must be signed in to change notification settings - Fork 53
/
engine.go
78 lines (62 loc) · 1.35 KB
/
engine.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
package engine
import (
"fmt"
"os"
"os/signal"
"sync"
"syscall"
"github.com/project-flogo/core/app"
"github.com/project-flogo/core/support/log"
"github.com/project-flogo/core/support/managed"
)
var managedServices []managed.Managed
var lock = &sync.Mutex{}
// Interface for the engine behaviour
type Engine interface {
//App get the application associated with this engine
App() *app.App
// Start starts the engine
Start() error
// Stop stop the engine
Stop() error
}
func LifeCycle(managedEntity managed.Managed) {
if managedEntity == nil {
return
}
defer lock.Unlock()
lock.Lock()
managedServices = append(managedServices, managedEntity)
}
func RunEngine(e Engine) int {
err := e.Start()
if err != nil {
log.RootLogger().Error(err)
fmt.Printf("Failed to start engine: %s\n", err.Error())
os.Exit(1)
}
exitChan := setupSignalHandling()
code := <-exitChan
_ = e.Stop()
return code
}
func setupSignalHandling() chan int {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
exitChan := make(chan int, 1)
select {
case s := <-signalChan:
switch s {
case syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT:
exitChan <- 0
default:
log.RootLogger().Debugf("Unknown signal.")
exitChan <- 1
}
}
return exitChan
}