forked from waffle-iron/core0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (96 loc) · 2.71 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"fmt"
"os"
"github.com/g8os/core0/base"
"github.com/g8os/core0/base/logger"
"github.com/g8os/core0/base/pm"
pmcore "github.com/g8os/core0/base/pm/core"
"github.com/g8os/core0/base/settings"
"github.com/g8os/core0/coreX/bootstrap"
"github.com/g8os/core0/coreX/options"
"github.com/op/go-logging"
_ "github.com/g8os/core0/base/builtin"
_ "github.com/g8os/core0/coreX/builtin"
"os/signal"
"syscall"
)
var (
log = logging.MustGetLogger("main")
)
func init() {
formatter := logging.MustStringFormatter("%{color}%{module} %{level:.1s} > %{message} %{color:reset}")
logging.SetFormatter(formatter)
logging.SetLevel(logging.INFO, "")
}
func handleSignal(bs *bootstrap.Bootstrap) {
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGTERM)
go func(ch <-chan os.Signal, bs *bootstrap.Bootstrap) {
<-ch
log.Infof("Received SIGTERM, terminating.")
bs.UnBootstrap()
os.Exit(0)
}(ch, bs)
}
func main() {
var opt = options.Options
fmt.Println(core.Version())
if opt.Version() {
os.Exit(0)
}
if errors := options.Options.Validate(); len(errors) != 0 {
for _, err := range errors {
log.Errorf("Validation Error: %s\n", err)
}
os.Exit(1)
}
//redis logger for commands
rl := logger.NewRedisLogger(uint16(opt.CoreID()), opt.RedisSocket(), "", nil, 100000)
//set backend so coreX logs itself also get pushed to redis
logging.SetBackend(
&logBackend{
logger: rl,
cmd: pmcore.Command{
ID: "core-x",
},
},
)
pm.InitProcessManager(opt.MaxJobs())
//start process mgr.
log.Infof("Starting process manager")
mgr := pm.GetManager()
//handle process results. Forwards the result to the correct controller.
mgr.AddResultHandler(func(cmd *pmcore.Command, result *pmcore.JobResult) {
result.Container = opt.CoreID()
log.Infof("Job result for command '%s' is '%s'", cmd, result.State)
})
mgr.Run()
bs := bootstrap.NewBootstrap()
handleSignal(bs)
if err := bs.Bootstrap(opt.Hostname()); err != nil {
log.Fatalf("Failed to bootstrap corex: %s", err)
}
sinkID := fmt.Sprintf("%d", opt.CoreID())
sinkCfg := settings.SinkConfig{
URL: fmt.Sprintf("redis://%s", opt.RedisSocket()),
Password: opt.RedisPassword(),
}
cl, err := core.NewSinkClient(&sinkCfg, sinkID, opt.ReplyTo())
if err != nil {
log.Fatal("Failed to get connection to redis at %s", sinkCfg.URL)
}
sinks := map[string]core.SinkClient{
"main": cl,
}
log.Infof("Configure redis logger")
mgr.AddMessageHandler(rl.Log)
//forward stats messages to core0
mgr.AddStatsHandler(func(op, key string, value float64, tags string) {
fmt.Printf("10::core-%d.%s:%f|%s|%s\n", opt.CoreID(), key, value, op, tags)
})
//start jobs sinks.
core.StartSinks(pm.GetManager(), sinks)
//wait
select {}
}