-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathobserver.go
More file actions
96 lines (79 loc) · 2.42 KB
/
observer.go
File metadata and controls
96 lines (79 loc) · 2.42 KB
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
/*
* Copyright 2018 The CovenantSQL Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package observer
import (
"net/http"
"github.com/CovenantSQL/CovenantSQL/conf"
"github.com/CovenantSQL/CovenantSQL/crypto/kms"
"github.com/CovenantSQL/CovenantSQL/proto"
rpc "github.com/CovenantSQL/CovenantSQL/rpc/mux"
"github.com/CovenantSQL/CovenantSQL/utils/log"
)
func registerNode() (err error) {
var nodeID proto.NodeID
if nodeID, err = kms.GetLocalNodeID(); err != nil {
return
}
var nodeInfo *proto.Node
if nodeInfo, err = kms.GetNodeInfo(nodeID); err != nil {
return
}
err = rpc.PingBP(nodeInfo, conf.GConf.BP.NodeID)
return
}
func startService() (service *Service, err error) {
// register observer service to rpc server
service, err = NewService()
if err != nil {
return
}
// start observer service
service.start()
return
}
func stopService(service *Service) (err error) {
// stop subscription
return service.stop()
}
// StartObserver starts the observer service and http API server.
func StartObserver(listenAddr string, version string) (service *Service, httpServer *http.Server, err error) {
// start service
if service, err = startService(); err != nil {
log.WithError(err).Fatal("start observation failed")
}
// start explorer api
httpServer, err = startAPI(service, listenAddr, version)
if err != nil {
log.WithError(err).Fatal("start explorer api failed")
}
// register node
if err = registerNode(); err != nil {
log.WithError(err).Fatal("register node failed")
}
return
}
// StopObserver stops the service and http API server returned by StartObserver.
func StopObserver(service *Service, httpServer *http.Server) (err error) {
// stop explorer api
if err = stopAPI(httpServer); err != nil {
log.WithError(err).Fatal("stop explorer api failed")
}
// stop subscriptions
if err = stopService(service); err != nil {
log.WithError(err).Fatal("stop service failed")
}
return
}