forked from docker-archive/classicswarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.go
79 lines (67 loc) · 1.37 KB
/
agent.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
package mesos
import (
"sync"
"github.com/docker/swarm/cluster"
"github.com/docker/swarm/cluster/mesos/task"
"github.com/mesos/mesos-go/mesosproto"
)
type agent struct {
sync.RWMutex
id string
offers map[string]*mesosproto.Offer
tasks map[string]*task.Task
engine *cluster.Engine
}
func newAgent(sid string, e *cluster.Engine) *agent {
return &agent{
id: sid,
offers: make(map[string]*mesosproto.Offer),
tasks: make(map[string]*task.Task),
engine: e,
}
}
func (s *agent) addOffer(offer *mesosproto.Offer) {
s.Lock()
s.offers[offer.Id.GetValue()] = offer
s.Unlock()
}
func (s *agent) addTask(task *task.Task) {
s.Lock()
s.tasks[task.TaskInfo.TaskId.GetValue()] = task
s.Unlock()
}
func (s *agent) removeOffer(offerID string) bool {
s.Lock()
defer s.Unlock()
found := false
_, found = s.offers[offerID]
if found {
delete(s.offers, offerID)
}
return found
}
func (s *agent) removeTask(taskID string) bool {
s.Lock()
defer s.Unlock()
found := false
_, found = s.tasks[taskID]
if found {
delete(s.tasks, taskID)
}
return found
}
func (s *agent) empty() bool {
s.RLock()
defer s.RUnlock()
return len(s.offers) == 0 && len(s.tasks) == 0
}
func (s *agent) getOffers() map[string]*mesosproto.Offer {
s.RLock()
defer s.RUnlock()
return s.offers
}
func (s *agent) getTasks() map[string]*task.Task {
s.RLock()
defer s.RUnlock()
return s.tasks
}