-
Notifications
You must be signed in to change notification settings - Fork 0
/
daemon.go
67 lines (57 loc) · 1.31 KB
/
daemon.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
package daemon
import (
"context"
"log"
"sync"
"github.com/spf13/cobra"
"github.com/thetatoken/theta/cmd/thetacli/rpc"
)
// startDaemonCmd runs the thetacli daemon
// Example:
// thetacli daemon start --port=16889
var startDaemonCmd = &cobra.Command{
Use: "start",
Short: "Run the thatacli daemon",
Long: `Run the thatacli daemon.`,
Example: `thetacli daemon start --port=16889`,
Run: func(cmd *cobra.Command, args []string) {
cfgPath := cmd.Flag("config").Value.String()
server, err := rpc.NewThetaCliRPCServer(cfgPath, portFlag)
if err != nil {
log.Fatalf("Failed to run the ThetaCli Daemon: %v", err)
}
daemon := &ThetaCliDaemon{
RPC: server,
}
daemon.Start(context.Background())
daemon.Wait()
},
}
func init() {
startDaemonCmd.Flags().StringVar(&portFlag, "port", "16889", "Port to run the ThetaCli Daemon")
}
type ThetaCliDaemon struct {
RPC *rpc.ThetaCliRPCServer
// Life cycle
wg *sync.WaitGroup
quit chan struct{}
ctx context.Context
cancel context.CancelFunc
stopped bool
}
func (d *ThetaCliDaemon) Start(ctx context.Context) {
c, cancel := context.WithCancel(ctx)
d.ctx = c
d.cancel = cancel
if d.RPC != nil {
d.RPC.Start(d.ctx)
}
}
func (d *ThetaCliDaemon) Stop() {
d.cancel()
}
func (d *ThetaCliDaemon) Wait() {
if d.RPC != nil {
d.RPC.Wait()
}
}