/
client_allocs.go
182 lines (142 loc) · 4.29 KB
/
client_allocs.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package nomad
import (
"context"
"fmt"
"log"
"strings"
"time"
"github.com/hashicorp/nomad/api"
)
const (
downloadingImageMessage = "downloading image"
imagePullProgressMessage = "image pull progress"
downloadingArtifactsMessage = "downloading artifacts"
taskReceivedMessage = "received by client"
buildingTaskDirectoryMessage = "building task directory"
eventStartedType = "Started"
)
type allocationInfo struct {
taskName string
taskState string
events []*api.TaskEvent
}
func (ai allocationInfo) started() bool {
if ai.taskState != Running {
return false
}
if len(ai.events) == 0 {
return false
}
e := ai.events[len(ai.events)-1]
return e.Type == eventStartedType
}
// finishedWithoutError is required for pre-start tasks that may be
// stopped before the main task
func (ai allocationInfo) finishedWithoutError() bool {
if ai.taskState != Dead {
return false
}
if len(ai.events) == 0 {
return false
}
e := ai.events[len(ai.events)-1]
return strings.ToLower(e.Type) == Terminated &&
e.ExitCode == 0 &&
!e.FailsTask
}
// lastEventMessageContains lowercase the message and check if message contains given text.
func (ai allocationInfo) lastEventMessageContains(text string) bool {
if len(ai.events) == 0 {
return false
}
e := ai.events[len(ai.events)-1]
return strings.Contains(strings.ToLower(e.DisplayMessage), text)
}
func (ai allocationInfo) downloadingImage() bool {
return ai.lastEventMessageContains(downloadingImageMessage) || ai.lastEventMessageContains(imagePullProgressMessage)
}
func (ai allocationInfo) downloadingArtifacts() bool {
return ai.lastEventMessageContains(downloadingArtifactsMessage)
}
func (ai allocationInfo) buildingTaskDirectory() bool {
return ai.lastEventMessageContains(buildingTaskDirectoryMessage)
}
func (ai allocationInfo) taskReceived() bool {
return ai.lastEventMessageContains(taskReceivedMessage)
}
func (ai allocationInfo) String() string {
events := make([]string, 0, len(ai.events))
for _, e := range ai.events {
events = append(events, fmt.Sprintf("%s: %s", e.Type, e.DisplayMessage))
}
return fmt.Sprintf("Task: %q, State: %q, Events: %q", ai.taskName, ai.taskState, strings.Join(events, " -> "))
}
func formatPendingReason(task, reason string) string {
return fmt.Sprintf("task %q is %s", task, reason)
}
type allocations []allocationInfo
func (allocs allocations) pending() (bool, string) {
for _, a := range allocs {
if a.downloadingImage() {
return true, formatPendingReason(a.taskName, downloadingImageMessage)
}
if a.downloadingArtifacts() {
return true, formatPendingReason(a.taskName, downloadingArtifactsMessage)
}
if a.taskReceived() {
return true, formatPendingReason(a.taskName, taskReceivedMessage)
}
if a.buildingTaskDirectory() {
return true, formatPendingReason(a.taskName, buildingTaskDirectoryMessage)
}
}
return false, ""
}
func (allocs allocations) startedOrFinishedWithoutError() bool {
for _, a := range allocs {
if !a.started() && !a.finishedWithoutError() {
return false
}
}
return true
}
func (n *Client) getJobAllocsInfo(ctx context.Context, jobID string) (allocations, error) {
allocs, _, err := n.API.Jobs().Allocations(jobID, false, &api.QueryOptions{})
if err != nil {
return nil, err
}
allocsInfo := make([]allocationInfo, 0, len(allocs))
for _, alloc := range allocs {
for tsName, ts := range alloc.TaskStates {
allocsInfo = append(allocsInfo, allocationInfo{
taskName: tsName,
taskState: ts.State,
events: ts.Events,
})
}
}
return allocsInfo, nil
}
func (n *Client) jobTimedOut(ctx context.Context, t *time.Ticker, jobID string) (bool, error) {
select {
case <-t.C:
allocs, err := n.getJobAllocsInfo(ctx, jobID)
if err != nil {
return false, err
}
if allocs.startedOrFinishedWithoutError() {
log.Printf("All tasks for job %q started or finished without an error", jobID)
return false, nil
}
if pending, reason := allocs.pending(); pending {
log.Printf("Job %q has not timed out because it has pending task: %s", jobID, reason)
return false, nil
}
for _, alloc := range allocs {
log.Printf("Job %q has timed out output: %s", jobID, alloc)
}
return true, fmt.Errorf("failed to run %s job: starting deadline has been exceeded", jobID)
default:
return false, nil
}
}