forked from hashicorp/nomad
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handle.go
165 lines (139 loc) · 3.61 KB
/
handle.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
package mock
import (
"context"
"io"
"sync"
"time"
hclog "github.com/hashicorp/go-hclog"
"github.com/hashicorp/nomad/client/lib/fifo"
bstructs "github.com/hashicorp/nomad/plugins/base/structs"
"github.com/hashicorp/nomad/plugins/drivers"
)
// taskHandle supervises a mock task
type taskHandle struct {
logger hclog.Logger
runFor time.Duration
pluginExitAfter time.Duration
killAfter time.Duration
waitCh chan struct{}
exitCode int
exitSignal int
exitErr error
signalErr error
stdoutString string
stdoutRepeat int
stdoutRepeatDur time.Duration
taskConfig *drivers.TaskConfig
// stateLock guards the procState field
stateLock sync.RWMutex
procState drivers.TaskState
startedAt time.Time
completedAt time.Time
exitResult *drivers.ExitResult
// Calling kill closes killCh if it is not already closed
kill context.CancelFunc
killCh <-chan struct{}
// Recovered is set to true if the handle was created while being recovered
Recovered bool
}
func (h *taskHandle) TaskStatus() *drivers.TaskStatus {
h.stateLock.RLock()
defer h.stateLock.RUnlock()
return &drivers.TaskStatus{
ID: h.taskConfig.ID,
Name: h.taskConfig.Name,
State: h.procState,
StartedAt: h.startedAt,
CompletedAt: h.completedAt,
ExitResult: h.exitResult,
DriverAttributes: map[string]string{},
}
}
func (h *taskHandle) IsRunning() bool {
h.stateLock.Lock()
defer h.stateLock.Unlock()
return h.procState == drivers.TaskStateRunning
}
func (h *taskHandle) run() {
defer func() {
h.stateLock.Lock()
h.procState = drivers.TaskStateExited
h.stateLock.Unlock()
h.completedAt = time.Now()
close(h.waitCh)
}()
h.stateLock.Lock()
h.procState = drivers.TaskStateRunning
h.stateLock.Unlock()
errCh := make(chan error, 1)
// Setup logging output
go h.handleLogging(errCh)
timer := time.NewTimer(h.runFor)
defer timer.Stop()
var pluginExitTimer <-chan time.Time
if h.pluginExitAfter != 0 {
timer := time.NewTimer(h.pluginExitAfter)
defer timer.Stop()
pluginExitTimer = timer.C
}
select {
case <-timer.C:
h.logger.Debug("run_for time elapsed; exiting", "run_for", h.runFor)
case <-h.killCh:
h.logger.Debug("killed; exiting")
case <-pluginExitTimer:
h.logger.Debug("exiting plugin")
h.exitResult = &drivers.ExitResult{
Err: bstructs.ErrPluginShutdown,
}
return
case err := <-errCh:
h.logger.Error("error running mock task; exiting", "error", err)
h.exitResult = &drivers.ExitResult{
Err: err,
}
return
}
h.exitResult = &drivers.ExitResult{
ExitCode: h.exitCode,
Signal: h.exitSignal,
Err: h.exitErr,
}
return
}
func (h *taskHandle) handleLogging(errCh chan<- error) {
stdout, err := fifo.OpenWriter(h.taskConfig.StdoutPath)
if err != nil {
h.logger.Error("failed to write to stdout", "error", err)
errCh <- err
return
}
stderr, err := fifo.OpenWriter(h.taskConfig.StderrPath)
if err != nil {
h.logger.Error("failed to write to stderr", "error", err)
errCh <- err
return
}
defer stderr.Close()
if h.stdoutString == "" {
return
}
if _, err := io.WriteString(stdout, h.stdoutString); err != nil {
h.logger.Error("failed to write to stdout", "error", err)
errCh <- err
return
}
for i := 0; i < h.stdoutRepeat; i++ {
select {
case <-h.waitCh:
h.logger.Warn("exiting before done writing output", "i", i, "total", h.stdoutRepeat)
return
case <-time.After(h.stdoutRepeatDur):
if _, err := io.WriteString(stdout, h.stdoutString); err != nil {
h.logger.Error("failed to write to stdout", "error", err)
errCh <- err
return
}
}
}
}