forked from sas1024/nomad_follower
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfollowedAllocation.go
74 lines (68 loc) · 1.76 KB
/
followedAllocation.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
package main
import (
"fmt"
nomadApi "github.com/hashicorp/nomad/api"
)
//FollowedAllocation a container for a followed allocations log process
type FollowedAllocation struct {
Alloc *nomadApi.Allocation
Nomad NomadConfig
OutputChan chan string
Quit chan struct{}
Tasks []*FollowedTask
log Logger
logTag string
}
//NewFollowedAllocation creates a new followed allocation
func NewFollowedAllocation(alloc *nomadApi.Allocation, nomad NomadConfig, outChan chan string, logger Logger, logTag string) *FollowedAllocation {
return &FollowedAllocation{
Alloc: alloc,
Nomad: nomad,
OutputChan: outChan,
Quit: make(chan struct{}),
Tasks: make([]*FollowedTask, 0),
log: logger,
logTag: logTag,
}
}
//Start starts following an allocation
func (f *FollowedAllocation) Start(save *SavedAlloc) {
f.log.Debugf(
"FollowedAllocation.Start",
"Following Allocation: %s ID: %s",
f.Alloc.Name,
f.Alloc.ID,
)
for _, tg := range f.Alloc.Job.TaskGroups {
for _, task := range tg.Tasks {
ft := NewFollowedTask(f.Alloc, *tg.Name, task, f.Nomad, f.Quit, f.OutputChan, f.log)
skip := true
for _, s := range ft.logTemplate.ServiceTags {
if s == f.logTag {
skip = false
}
}
if !skip {
if save != nil {
f.log.Debug("FollowedAllocation.Start", "Restoring saved allocation data")
key := fmt.Sprintf("%s:%s", *tg.Name, task.Name)
savedTask := save.SavedTasks[key]
ft.Start(&savedTask)
} else {
ft.Start(nil)
}
f.Tasks = append(f.Tasks, ft)
}
}
}
}
//Stop stops tailing all allocation tasks
func (f *FollowedAllocation) Stop() {
f.log.Debugf(
"FollowedAllocation.Stop",
"Stopping Allocation: %s ID: %s",
f.Alloc.Name,
f.Alloc.ID,
)
close(f.Quit)
}