-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworker.go
58 lines (48 loc) · 1.34 KB
/
worker.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
package main
//import "time"
import (
"os"
)
type Worker struct {
jobsChannel chan *TestJob
reportsChannel chan *TestJob
workerIdlingChannel chan bool
logger Logger
client *APIClient
lastTestRunId int
project *Project
}
// NewWorker should be used to create a Worker instances. It ensures the correct
// initialization of all fields.
func NewWorker(jobsChannel chan *TestJob, reportsChannel chan *TestJob, workerIdlingChannel chan bool, project *Project) *Worker {
logger := Logger{"Worker", os.Stdout}
return &Worker{
jobsChannel: jobsChannel,
reportsChannel: reportsChannel,
workerIdlingChannel: workerIdlingChannel,
logger: logger,
client: NewClient(logger),
project: project,
}
}
func (w *Worker) Start() {
w.logger.Log("Entering loop")
for {
w.RunJob()
}
}
// RunJobs reads a job from the jobsChannel and runs it.
func (w *Worker) RunJob() {
nextJob := <-w.jobsChannel
if w.lastTestRunId != nextJob.TestRunId {
w.project.SetupTestEnvironment(nextJob.CommitSha, w.logger)
}
nextJob.Run(w.logger)
w.lastTestRunId = nextJob.TestRunId
// Inform manager that we are done in order to set
// workerCurrentJobCostPredictionSeconds back to zero
w.workerIdlingChannel <- true
go func() {
w.reportsChannel <- nextJob
}()
}