-
Notifications
You must be signed in to change notification settings - Fork 54
/
executor.go
72 lines (56 loc) · 1.51 KB
/
executor.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
package forest
import (
"fmt"
"github.com/labstack/gommon/log"
)
const (
JobSnapshotPath = "/forest/client/snapshot/"
JobSnapshotGroupPath = "/forest/client/snapshot/%s/"
JobClientSnapshotPath = "/forest/client/snapshot/%s/%s/"
)
type JobExecutor struct {
node *JobNode
snapshots chan *JobSnapshot
}
func NewJobExecutor(node *JobNode) (exec *JobExecutor) {
exec = &JobExecutor{
node: node,
snapshots: make(chan *JobSnapshot, 500),
}
go exec.lookup()
return
}
func (exec *JobExecutor) lookup() {
for snapshot := range exec.snapshots {
exec.handleJobSnapshot(snapshot)
}
}
// handle the job snapshot
func (exec *JobExecutor) handleJobSnapshot(snapshot *JobSnapshot) {
var (
err error
client *Client
)
group := snapshot.Group
if client, err = exec.node.groupManager.selectClient(group); err != nil {
log.Warnf("the group:%s,select a client error:%#v", group, err)
return
}
clientName := client.name
snapshot.Ip = clientName
log.Printf("clientName:%#v", clientName)
snapshotPath := fmt.Sprintf(JobClientSnapshotPath, group, clientName)
log.Printf("snapshotPath:%#v", snapshotPath)
value, err := ParkJobSnapshot(snapshot)
if err != nil {
log.Warnf("uPark the snapshot error:%#v", group, err)
return
}
if err = exec.node.etcd.Put(snapshotPath+snapshot.Id, string(value)); err != nil {
log.Warnf("put the snapshot error:%#v", group, err)
}
}
// push a new job snapshot
func (exec *JobExecutor) pushSnapshot(snapshot *JobSnapshot) {
exec.snapshots <- snapshot
}