-
Notifications
You must be signed in to change notification settings - Fork 12
/
process-config-worker.go
63 lines (57 loc) · 1.48 KB
/
process-config-worker.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
package process_master
import (
"encoding/json"
"github.com/eolinker/eosc/process"
"github.com/eolinker/eosc/process-master/extender"
"github.com/eolinker/eosc/service"
"github.com/eolinker/eosc/traffic"
"os"
"sync"
"github.com/eolinker/eosc/config"
)
type WorkerController struct {
workerProcess *process.ProcessController
extends map[string]string
locker sync.Mutex
traffics []*traffic.PbTraffic
trafficFiles []*os.File
listensMsg config.ListenUrl
isRunning bool
lastVersion int64
}
func (wc *WorkerController) Stop() {
wc.workerProcess.Stop()
}
func (wc *WorkerController) Update(status []*extender.Status, success bool) {
if success {
extends := make(map[string]string)
for _, s := range status {
extends[s.Name()] = s.Version
}
wc.locker.Lock()
wc.extends = extends
wc.locker.Unlock()
args := &service.ProcessLoadArg{
Traffic: wc.traffics,
ListensMsg: wc.listensMsg,
Extends: extends,
}
data, _ := json.Marshal(args)
if wc.isRunning {
wc.workerProcess.TryRestart(data, wc.trafficFiles)
} else {
wc.isRunning = true
wc.workerProcess.Start(data, wc.trafficFiles)
}
}
}
func NewWorkerController(tfd *traffic.TrafficData, listensMsg config.ListenUrl, workerProcess *process.ProcessController) *WorkerController {
traffics, files := traffic.Export(tfd, 3)
wc := &WorkerController{
traffics: traffics,
trafficFiles: files,
listensMsg: listensMsg,
workerProcess: workerProcess,
}
return wc
}