-
Notifications
You must be signed in to change notification settings - Fork 914
/
standards.go
151 lines (119 loc) · 2.75 KB
/
standards.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
package orchestrator
import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"strconv"
"sync"
"time"
"github.com/botlabs-gg/yagpdb/v2/lib/discordgo"
)
// StdShardCountProvider is a standard implementation of RecommendedShardCountProvider
type StdShardCountProvider struct {
DiscordSession *discordgo.Session
}
func (sc *StdShardCountProvider) GetTotalShardCount() (int, error) {
gwBot, err := sc.DiscordSession.GatewayBot()
if err != nil {
return 0, err
}
return gwBot.Shards, nil
}
type IDGenerator interface {
GenerateID() (string, error)
}
type StdNodeLauncher struct {
IDGenerator IDGenerator
LaunchCmdName string
LaunchArgs []string
VersionCmdName string
VersionArgs []string
mu sync.Mutex
lastTimeLaunchedNode time.Time
}
func NewNodeLauncher(cmdName string, args []string, idGen IDGenerator) NodeLauncher {
return &StdNodeLauncher{
IDGenerator: idGen,
LaunchCmdName: cmdName,
LaunchArgs: args,
}
}
// LaunchNewNode implements NodeLauncher.LaunchNewNode
func (nl *StdNodeLauncher) LaunchNewNode() (string, error) {
// ensure were not starting nodes too fast since the id generation only does millisecond unique ids
nl.mu.Lock()
if time.Since(nl.lastTimeLaunchedNode) < time.Millisecond*100 {
time.Sleep(time.Millisecond * 100)
}
nl.lastTimeLaunchedNode = time.Now()
nl.mu.Unlock()
// generate the node id
var err error
id := ""
if nl.IDGenerator != nil {
id, err = nl.IDGenerator.GenerateID()
} else {
id = nl.GenerateID()
}
if err != nil {
return "", err
}
args := append(nl.LaunchArgs, "-nodeid", id)
// launch it
cmd := exec.Command(nl.LaunchCmdName, args...)
cmd.Env = os.Environ()
wd, err := os.Getwd()
if err != nil {
return "", err
}
cmd.Dir = wd
stdOut, err := cmd.StdoutPipe()
if err != nil {
return "", err
}
go nl.PrintOutput(stdOut)
stdErr, err := cmd.StderrPipe()
if err != nil {
return "", err
}
go nl.PrintOutput(stdErr)
err = cmd.Start()
return id, err
}
func (nl *StdNodeLauncher) GenerateID() string {
tms := time.Now().UnixNano() / 1000000
st := strconv.FormatInt(tms, 36)
host, _ := os.Hostname()
return fmt.Sprintf("%s-%s", host, st)
}
func (nl *StdNodeLauncher) PrintOutput(reader io.Reader) {
breader := bufio.NewReader(reader)
for {
s, err := breader.ReadString('\n')
if len(s) > 0 {
s = s[:len(s)-1]
}
fmt.Println("NODE: " + s)
if err != nil {
break
}
}
}
// LaunchVersion implements NodeLauncher.LaunchVersion
func (nl *StdNodeLauncher) LaunchVersion() (string, error) {
// launch it
cmd := exec.Command(nl.VersionCmdName, nl.VersionArgs...)
cmd.Env = os.Environ()
wd, err := os.Getwd()
if err != nil {
return "", err
}
cmd.Dir = wd
out, err := cmd.Output()
if err != nil {
return "", err
}
return string(out), nil
}