/
ServiceT.Stop.go
76 lines (57 loc) · 1.91 KB
/
ServiceT.Stop.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
package masterbot
import (
"fmt"
"time"
"sync"
"net/http"
"github.com/luisfurquim/stonelizard"
)
func (svc ServiceT) Stop() stonelizard.Response {
var botId string
var botCfg *BotClientT
var wg sync.WaitGroup
var botInstance int
var host Host
Goose.StartStop.Logf(2,"Stopping kairos")
Kairos.Stop()
Goose.StartStop.Logf(2,"Stopping slave bots")
for botId, botCfg = range svc.appcfg.Bot {
wg.Add(len(botCfg.Host)) // wait the stop of each instance of the slavebots
}
for botId, botCfg = range svc.appcfg.Bot {
for botInstance, host = range botCfg.Host {
go func(id string, instance int, cfg *BotClientT, h Host) {
var err error
var url string
var resp *http.Response
defer wg.Done()
if h.Status == BotStatRunning {
url = fmt.Sprintf("https://%s%s/%s/stop", h.Name, cfg.Listen, id)
Goose.StartStop.Logf(2,"Stopping bot %s@%s via %s",id,h.Name,url)
resp, err = svc.appcfg.HttpsStopClient.Get(url)
if err != nil {
Goose.StartStop.Logf(1,"Error stopping bot %s@%s (%s)",id,h.Name,err)
return
}
if resp.StatusCode != http.StatusNoContent {
Goose.StartStop.Logf(1,"Error of status code stopping bot %s@%s (%s)",id,h.Name,resp.Status)
}
}
}(botId,botInstance,botCfg,host)
}
}
wg.Wait()
wg.Add(1) // wait the stop of the masterbot listener
go (func () {
Goose.StartStop.Logf(2,"Stopping listener")
wg.Wait()
svc.onStop()
Goose.StartStop.Logf(2,"Masterbot listener finished")
})()
time.Sleep(100 * time.Millisecond)
Goose.StartStop.Logf(2,"Ending masterbot")
defer wg.Done()
return stonelizard.Response{
Status: http.StatusNoContent,
}
}