/
main.go
106 lines (87 loc) · 2.31 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
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"runtime/pprof"
"flag"
"runtime"
"github.com/TIBCOSoftware/flogo-lib/app"
"github.com/TIBCOSoftware/flogo-lib/engine"
"github.com/TIBCOSoftware/flogo-lib/logger"
)
var log = logger.GetLogger("main-engine")
var cpuprofile = flag.String("cpuprofile", "", "Writes CPU profiling for the current process to the specified file")
var memprofile = flag.String("memprofile", "", "Writes memory profiling for the current process to the specified file")
var (
cp app.ConfigProvider
)
func main() {
if cp == nil {
// Use default config provider
cp = app.DefaultConfigProvider()
}
app, err := cp.GetApp()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
fmt.Println(fmt.Sprintf("Failed to create CPU profiling file due to error - %s", err.Error()))
os.Exit(1)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
e, err := engine.New(app)
if err != nil {
log.Errorf("Failed to create engine instance due to error: %s", err.Error())
os.Exit(1)
}
err = e.Start()
if err != nil {
log.Errorf("Failed to start engine due to error: %s", err.Error())
os.Exit(1)
}
exitChan := setupSignalHandling()
code := <-exitChan
e.Stop()
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
fmt.Println(fmt.Sprintf("Failed to create memory profiling file due to error - %s", err.Error()))
os.Exit(1)
}
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
fmt.Println(fmt.Sprintf("Failed to write memory profiling data to file due to error - %s", err.Error()))
os.Exit(1)
}
f.Close()
}
os.Exit(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:
logger.Debug("Unknown signal.")
exitChan <- 1
}
}
return exitChan
}