-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_job.go
123 lines (100 loc) · 3.64 KB
/
test_job.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
package main
import (
"github.com/testributor/agent/system_command"
"strconv"
"time"
)
const (
NO_PREDICTION_WORKLOAD_SECONDS = 999999999
)
type TestJob struct {
Id int `json:"id"`
CostPredictionSeconds float64 `json:"cost_prediction_seconds"`
SentAtSecondsSinceEpoch int64 `json:"sent_at_seconds_since_epoch"`
StartedAtSecondsSinceEpoch int64 `json:"started_at_seconds_since_epoch"`
CreatedAt time.Time `json:"created_at"`
Command string `json:"command"`
Result string `json:"result"`
ResultType int `json:"status"`
TestRunId int `json:"test_run_id"`
WorkerInQueueSeconds int64 `json:"worker_in_queue_seconds"`
WorkerCommandRunSeconds int64 `json:"worker_command_run_seconds"`
QueuedAtSecondsSinceEpoch int64
CommitSha string
}
// This is a custom type based on the type return my APIClient's FetchJobs
// function. We add methods on this type to parse the various fields and return
// them in a format suitable for TestJob fields.
type TestJobBuilder map[string]interface{}
func (builder *TestJobBuilder) id() int {
return int((*builder)["id"].(float64))
}
func (builder *TestJobBuilder) testRunId() int {
testRun := (*builder)["test_run"].(map[string]interface{})
return int(testRun["id"].(float64))
}
func (builder *TestJobBuilder) commitSha() string {
testRun := (*builder)["test_run"].(map[string]interface{})
return testRun["commit_sha"].(string)
}
func (builder *TestJobBuilder) costPredictionSeconds() float64 {
switch (*builder)["cost_prediction"].(type) {
case string:
costPredictionSeconds, err :=
strconv.ParseFloat((*builder)["cost_prediction"].(string), 64)
if err != nil {
panic("Invalid format for cost prediction: " + err.Error())
}
// If no prediction is available use the default "huge" value to avoid
// fetching more jobs.
if costPredictionSeconds == 0 {
return NO_PREDICTION_WORKLOAD_SECONDS
} else {
return costPredictionSeconds
}
default:
return NO_PREDICTION_WORKLOAD_SECONDS
}
}
func (builder *TestJobBuilder) createdAt() time.Time {
createdAt, err := time.Parse(time.RFC3339, (*builder)["created_at"].(string))
if err != nil {
createdAt = *new(time.Time)
}
return createdAt
}
func (builder *TestJobBuilder) command() string {
return (*builder)["command"].(string)
}
func (builder *TestJobBuilder) sentAtSecondsSinceEpoch() int64 {
return int64((*builder)["sent_at_seconds_since_epoch"].(float64))
}
// NewTestJob is used to create a TestJob from the API response
func NewTestJob(jobData map[string]interface{}) TestJob {
builder := TestJobBuilder(jobData)
testJob := TestJob{
Id: builder.id(),
TestRunId: builder.testRunId(),
CommitSha: builder.commitSha(),
CostPredictionSeconds: builder.costPredictionSeconds(),
SentAtSecondsSinceEpoch: builder.sentAtSecondsSinceEpoch(),
CreatedAt: builder.createdAt(),
Command: builder.command(),
}
return testJob
}
func (testJob *TestJob) Run(logger Logger) {
testJob.StartedAtSecondsSinceEpoch = time.Now().Unix()
logger.Log("Running " + testJob.Command)
res, err := system_command.Run(testJob.Command, logger)
if err != nil {
testJob.Result = err.Error()
testJob.ResultType = system_command.RESULT_TYPES["error"]
} else {
testJob.Result = res.CombinedOutput
testJob.ResultType = res.ResultType
}
testJob.WorkerInQueueSeconds =
testJob.StartedAtSecondsSinceEpoch - testJob.QueuedAtSecondsSinceEpoch
testJob.WorkerCommandRunSeconds = int64(res.DurationSeconds)
}