forked from open-horizon/anax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
153 lines (131 loc) · 4.35 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"flag"
"github.com/boltdb/bolt"
"github.com/golang/glog"
"github.com/open-horizon/anax/agreement"
"github.com/open-horizon/anax/agreementbot"
"github.com/open-horizon/anax/api"
"github.com/open-horizon/anax/config"
"github.com/open-horizon/anax/container"
"github.com/open-horizon/anax/ethblockchain"
"github.com/open-horizon/anax/exchange"
"github.com/open-horizon/anax/governance"
"github.com/open-horizon/anax/policy"
"github.com/open-horizon/anax/torrent"
"github.com/open-horizon/anax/worker"
"os"
"os/signal"
"path"
"runtime"
"runtime/pprof"
"syscall"
"time"
)
// The core of anax is an event handling system that distributes events to workers, where the workers
// process events that they are about. However, to get started, anax needs to do a bunch of initialization
// tasks. The config file has to be read in, the databases have to get created, and then the eventing system
// and the workers can be fired up.
//
func main() {
configFile := flag.String("config", "/etc/colonus/anax.config", "Config file location")
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to file")
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
panic(err)
}
pprof.StartCPUProfile(f)
glog.V(2).Infof("Started CPU profiling. Writing to: %v", f.Name())
}
cfg, err := config.Read(*configFile)
if err != nil {
panic(err)
}
glog.V(2).Infof("Using config: %v", cfg)
glog.V(2).Infof("GOMAXPROCS: %v", runtime.GOMAXPROCS(-1))
// open edge DB if necessary
var db *bolt.DB
if len(cfg.Edge.DBPath) != 0 {
if err := os.MkdirAll(cfg.Edge.DBPath, 0700); err != nil {
panic(err)
}
edgeDB, err := bolt.Open(path.Join(cfg.Edge.DBPath, "anax.db"), 0600, &bolt.Options{Timeout: 10 * time.Second})
if err != nil {
panic(err)
}
db = edgeDB
}
// open Agreement Bot DB if necessary
var agbotdb *bolt.DB
if len(cfg.AgreementBot.DBPath) != 0 {
if err := os.MkdirAll(cfg.AgreementBot.DBPath, 0700); err != nil {
panic(err)
}
agdb, err := bolt.Open(path.Join(cfg.AgreementBot.DBPath, "agreementbot.db"), 0600, &bolt.Options{Timeout: 10 * time.Second})
if err != nil {
panic(err)
}
agbotdb = agdb
}
// start control signal handler
control := make(chan os.Signal, 1)
signal.Notify(control, os.Interrupt)
signal.Notify(control, syscall.SIGTERM)
// This routine does not need to be a subworker because it has no parent worker and it will terminate on its own
// when the main anax process terminates.
go func() {
<-control
glog.Infof("Closing up shop.")
pprof.StopCPUProfile()
if db != nil {
db.Close()
}
if agbotdb != nil {
agbotdb.Close()
}
os.Exit(0)
}()
// Get the device side policy manager started early so that all the workers can use it.
// Make sure the policy directory is in place.
var pm *policy.PolicyManager
if cfg.Edge.PolicyPath == "" {
// nothing to initialize
} else if err := os.MkdirAll(cfg.Edge.PolicyPath, 0644); err != nil {
glog.Errorf("Cannot create edge policy file path %v, terminating.", cfg.Edge.PolicyPath)
panic(err)
} else if policyManager, err := policy.Initialize(cfg.Edge.PolicyPath, cfg.ArchSynonyms, nil, true); err != nil {
glog.Errorf("Unable to initialize policy manager, terminating.")
panic(err)
} else {
pm = policyManager
}
// start workers
workers := worker.NewMessageHandlerRegistry()
workers.Add(agreementbot.NewAgreementBotWorker("AgBot", cfg, agbotdb))
if cfg.AgreementBot.APIListen != "" {
workers.Add(agreementbot.NewAPIListener("AgBot API", cfg, agbotdb))
}
workers.Add(ethblockchain.NewEthBlockchainWorker("Blockchain", cfg))
if db != nil {
workers.Add(api.NewAPIListener("API", cfg, db, pm))
workers.Add(agreement.NewAgreementWorker("Agreement", cfg, db, pm))
workers.Add(governance.NewGovernanceWorker("Governance", cfg, db, pm))
workers.Add(exchange.NewExchangeMessageWorker("Exchange", cfg, db))
workers.Add(container.NewContainerWorker("Container", cfg, db))
workers.Add(torrent.NewTorrentWorker("Torrent", cfg, db))
} else {
workers.Add(container.NewContainerWorker("Container", cfg, agbotdb))
workers.Add(torrent.NewTorrentWorker("Torrent", cfg, agbotdb))
}
// Get into the event processing loop until anax shuts itself down.
workers.ProcessEventMessages()
if db != nil {
db.Close()
}
if agbotdb != nil {
agbotdb.Close()
}
glog.Info("Main process terminating")
}