This repository has been archived by the owner on Feb 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
taskmanager.go
221 lines (188 loc) · 5.95 KB
/
taskmanager.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package worker
import (
"fmt"
"sync"
"github.com/taskcluster/taskcluster-client-go"
tcqueue "github.com/taskcluster/taskcluster-client-go/queue"
"github.com/taskcluster/taskcluster-worker/engines"
"github.com/taskcluster/taskcluster-worker/plugins"
"github.com/taskcluster/taskcluster-worker/runtime"
"github.com/taskcluster/taskcluster-worker/runtime/gc"
)
// Manager is resonsible for managing the entire task lifecyle from claiming the
// task, creating a sandbox environment, and reporting the results fo the execution.
// The manager will also be responsible for ensuring tasks do not run past their max run
// time and are aborted if a cancellation message is received.
type Manager struct {
sync.RWMutex
doneClaimingTasks chan struct{}
doneExecutingTasks chan struct{}
config *configType
engine engines.Engine
environment *runtime.Environment
pluginManager plugins.Plugin
pluginOptions *plugins.PluginOptions
monitor runtime.Monitor
queue QueueService
provisionerID string
workerGroup string
workerID string
tasks map[string]*TaskRun
gc *gc.GarbageCollector
}
// Create a new instance of the task manager that will be responsible for claiming,
// executing, and resolving units of work (tasks).
func newTaskManager(
config *configType, engine engines.Engine, pluginManager plugins.Plugin,
environment *runtime.Environment, monitor runtime.Monitor, gc *gc.GarbageCollector,
) (*Manager, error) {
queue := tcqueue.New(
&tcclient.Credentials{
ClientID: config.Credentials.ClientID,
AccessToken: config.Credentials.AccessToken,
Certificate: config.Credentials.Certificate,
},
)
if config.QueueBaseURL != "" {
queue.BaseURL = config.QueueBaseURL
}
service := &queueService{
capacity: config.Capacity,
interval: config.PollingInterval,
client: queue,
provisionerID: config.ProvisionerID,
workerGroup: config.WorkerGroup,
workerID: config.WorkerID,
workerType: config.WorkerType,
monitor: monitor.WithPrefix("queue-service"),
expirationOffset: config.ReclaimOffset,
maxTasksToRun: config.MaxTasksToRun,
}
m := &Manager{
tasks: make(map[string]*TaskRun),
doneClaimingTasks: make(chan struct{}),
doneExecutingTasks: make(chan struct{}),
config: config,
engine: engine,
environment: environment,
monitor: monitor,
queue: service,
provisionerID: config.ProvisionerID,
workerGroup: config.WorkerGroup,
workerID: config.WorkerID,
gc: gc,
}
m.pluginManager = pluginManager
return m, nil
}
// Start will instruct the queue service to begin claiming work and will run task
// claims that are returned by the queue service.
func (m *Manager) Start() {
tc := m.queue.Start()
var wg sync.WaitGroup
go func() {
for t := range tc {
wg.Add(1)
go func(t *taskClaim) {
defer wg.Done()
m.run(t)
}(t)
}
close(m.doneClaimingTasks)
wg.Wait()
close(m.doneExecutingTasks)
}()
return
}
// ImmediateStop should be called when the worker should aggressively terminate
// all running tasks and then gracefully terminate.
func (m *Manager) ImmediateStop() {
m.queue.Stop()
<-m.doneClaimingTasks
m.Lock()
defer m.Unlock()
for _, task := range m.tasks {
task.Abort()
}
<-m.doneExecutingTasks
}
// GracefulStop should be called when the worker should stop claiming new
// tasks, but wait for existing tasks to complete naturally
func (m *Manager) GracefulStop() {
m.queue.Stop()
<-m.doneExecutingTasks
}
// RunningTasks returns the list of task names that are currently running. This could
// be useful for determining the number of tasks currently running or snapshotting
// the current running task list at a moment in time.
func (m *Manager) RunningTasks() []string {
m.RLock()
defer m.RUnlock()
tasks := []string{}
for k := range m.tasks {
tasks = append(tasks, k)
}
return tasks
}
// CancelTask will cancel a running task. Typically this will be called when a Pulse
// message is received to cancel a task. Calling this method will not resolve the task
// as it's assumed that this task was already resolved as cancelled by another system/client.
func (m *Manager) CancelTask(taskID string, runID int) {
name := fmt.Sprintf("%s/%d", taskID, runID)
m.RLock()
defer m.RUnlock()
t, exists := m.tasks[name]
if !exists {
return
}
t.Cancel()
return
}
func (m *Manager) run(claim *taskClaim) {
// Always do a best-effort GCing before we run a task
if err := m.gc.Collect(); err != nil {
m.monitor.Error("Failed to run garbage collector, error: ", err)
}
monitor := m.monitor.WithTags(map[string]string{
"taskID": claim.taskID,
"runID": fmt.Sprintf("%d", claim.runID),
})
task, err := newTaskRun(m.config, claim, m.environment, m.engine, m.pluginManager, monitor)
if err != nil {
// This is a fatal call because creating a task run should never fail.
monitor.WithTag("error", err.Error()).Panic("Could not successfully run the task")
}
err = m.registerTask(task)
if err != nil {
monitor.WithTag("error", err.Error()).Warn("Could not register task")
panic(err)
}
defer m.deregisterTask(task)
task.Run()
return
}
func (m *Manager) registerTask(task *TaskRun) error {
name := fmt.Sprintf("%s/%d", task.TaskID, task.RunID)
m.monitor.Debugf("Registered task: %s", name)
m.Lock()
defer m.Unlock()
_, exists := m.tasks[name]
if exists {
return fmt.Errorf("cannot register task %s since it already exists", name)
}
m.tasks[name] = task
return nil
}
func (m *Manager) deregisterTask(task *TaskRun) error {
name := fmt.Sprintf("%s/%d", task.TaskID, task.RunID)
m.monitor.Debugf("Deregistered task: %s", name)
m.Lock()
defer m.Unlock()
_, exists := m.tasks[name]
if !exists {
return fmt.Errorf("cannot deregister task %s since it does not exist", name)
}
delete(m.tasks, name)
m.queue.Done()
return nil
}