forked from bitmark-inc/bitmarkd
/
setup.go
113 lines (84 loc) · 2.5 KB
/
setup.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
// Copyright (c) 2014-2018 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package proof
import (
"sync"
"github.com/bitmark-inc/bitmarkd/background"
"github.com/bitmark-inc/bitmarkd/fault"
"github.com/bitmark-inc/logger"
)
// server identification in Z85 (ZeroMQ Base-85 Encoding) see: http://rfc.zeromq.org/spec:32
// a block of configuration data
// this is read from a libucl configuration file
type Configuration struct {
Publish []string `libucl:"publish" json:"publish"`
Submit []string `libucl:"submit" json:"submit"`
PrivateKey string `libucl:"private_key" json:"private_key"`
PublicKey string `libucl:"public_key" json:"public_key"`
SigningKey string `libucl:"signing_key" json:"signing_key"`
PaymentAddr map[string]string `libucl:"payment_address" json:"payment_address"`
}
// globals for background proccess
type proofData struct {
sync.RWMutex // to allow locking
// logger
log *logger.L
// for publisher
pub publisher
// for submission
sub submission
// for background
background *background.T
// set once during initialise
initialised bool
}
// global data
var globalData proofData
// initialise proofer backgrouds processes
func Initialise(configuration *Configuration) error {
globalData.Lock()
defer globalData.Unlock()
// no need to start if already started
if globalData.initialised {
return fault.ErrAlreadyInitialised
}
globalData.log = logger.New("proof")
globalData.log.Info("starting…")
if err := globalData.pub.initialise(configuration); nil != err {
return err
}
if err := globalData.sub.initialise(configuration); nil != err {
return err
}
// create the job queue
initialiseJobQueue()
// all data initialised
globalData.initialised = true
// start background processes
globalData.log.Info("start background…")
// list of background processes to start
processes := background.Processes{
&globalData.pub,
&globalData.sub,
}
globalData.background = background.Start(processes, globalData.log)
return nil
}
// finialise - stop all background tasks
func Finalise() error {
globalData.Lock()
defer globalData.Unlock()
if !globalData.initialised {
return fault.ErrNotInitialised
}
globalData.log.Info("shutting down…")
globalData.log.Flush()
// stop background
globalData.background.Stop()
// finally...
globalData.initialised = false
globalData.log.Info("finished")
globalData.log.Flush()
return nil
}