-
Notifications
You must be signed in to change notification settings - Fork 13
/
worker.go
183 lines (153 loc) · 5.79 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
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
// SPDX-License-Identifier: Apache-2.0
package database
import (
"database/sql"
"errors"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/lib/pq"
)
var (
// ErrEmptyWorkerHost defines the error type when a
// Worker type has an empty Host field provided.
ErrEmptyWorkerHost = errors.New("empty worker address provided")
// ErrEmptyWorkerAddress defines the error type when a
// Worker type has an empty Address field provided.
ErrEmptyWorkerAddress = errors.New("empty worker address provided")
// ErrExceededRunningBuildIDsLimit defines the error type when a
// Worker type has RunningBuildIDs field provided that exceeds the database limit.
ErrExceededRunningBuildIDsLimit = errors.New("exceeded running build ids limit")
)
// Worker is the database representation of a worker.
type Worker struct {
ID sql.NullInt64 `sql:"id"`
Hostname sql.NullString `sql:"hostname"`
Address sql.NullString `sql:"address"`
Routes pq.StringArray `sql:"routes" gorm:"type:varchar(1000)"`
Active sql.NullBool `sql:"active"`
Status sql.NullString `sql:"status"`
LastStatusUpdateAt sql.NullInt64 `sql:"last_status_update_at"`
RunningBuildIDs pq.StringArray `sql:"running_build_ids" gorm:"type:varchar(500)"`
LastBuildStartedAt sql.NullInt64 `sql:"last_build_started_at"`
LastBuildFinishedAt sql.NullInt64 `sql:"last_build_finished_at"`
LastCheckedIn sql.NullInt64 `sql:"last_checked_in"`
BuildLimit sql.NullInt64 `sql:"build_limit"`
}
// Nullify ensures the valid flag for
// the sql.Null types are properly set.
//
// When a field within the Build type is the zero
// value for the field, the valid flag is set to
// false causing it to be NULL in the database.
func (w *Worker) Nullify() *Worker {
if w == nil {
return nil
}
// check if the ID field should be false
if w.ID.Int64 == 0 {
w.ID.Valid = false
}
// check if the Hostname field should be false
if len(w.Hostname.String) == 0 {
w.Hostname.Valid = false
}
// check if the Address field should be false
if len(w.Address.String) == 0 {
w.Address.Valid = false
}
// check if the Status field should be false
if len(w.Status.String) == 0 {
w.Status.Valid = false
}
// check if the LastStatusUpdateAt field should be false
if w.LastStatusUpdateAt.Int64 == 0 {
w.LastStatusUpdateAt.Valid = false
}
// check if the LastBuildStartedAt field should be false
if w.LastBuildStartedAt.Int64 == 0 {
w.LastBuildStartedAt.Valid = false
}
// check if the LastBuildFinishedAt field should be false
if w.LastBuildFinishedAt.Int64 == 0 {
w.LastBuildFinishedAt.Valid = false
}
// check if the LastCheckedIn field should be false
if w.LastCheckedIn.Int64 == 0 {
w.LastCheckedIn.Valid = false
}
if w.BuildLimit.Int64 == 0 {
w.BuildLimit.Valid = false
}
return w
}
// ToLibrary converts the Worker type
// to a library Worker type.
func (w *Worker) ToLibrary() *library.Worker {
worker := new(library.Worker)
worker.SetID(w.ID.Int64)
worker.SetHostname(w.Hostname.String)
worker.SetAddress(w.Address.String)
worker.SetRoutes(w.Routes)
worker.SetActive(w.Active.Bool)
worker.SetStatus(w.Status.String)
worker.SetLastStatusUpdateAt(w.LastStatusUpdateAt.Int64)
worker.SetRunningBuildIDs(w.RunningBuildIDs)
worker.SetLastBuildStartedAt(w.LastBuildStartedAt.Int64)
worker.SetLastBuildFinishedAt(w.LastBuildFinishedAt.Int64)
worker.SetLastCheckedIn(w.LastCheckedIn.Int64)
worker.SetBuildLimit(w.BuildLimit.Int64)
return worker
}
// Validate verifies the necessary fields for
// the Worker type are populated correctly.
func (w *Worker) Validate() error {
// verify the Host field is populated
if len(w.Hostname.String) == 0 {
return ErrEmptyWorkerHost
}
// verify the Address field is populated
if len(w.Address.String) == 0 {
return ErrEmptyWorkerAddress
}
// calculate total size of RunningBuildIds
total := 0
for _, f := range w.RunningBuildIDs {
total += len(f)
}
// verify the RunningBuildIds field is within the database constraints
// len is to factor in number of comma separators included in the database field,
// removing 1 due to the last item not having an appended comma
if (total + len(w.RunningBuildIDs) - 1) > constants.RunningBuildIDsMaxSize {
return ErrExceededRunningBuildIDsLimit
}
// ensure that all Worker string fields
// that can be returned as JSON are sanitized
// to avoid unsafe HTML content
w.Hostname = sql.NullString{String: sanitize(w.Hostname.String), Valid: w.Hostname.Valid}
w.Address = sql.NullString{String: sanitize(w.Address.String), Valid: w.Address.Valid}
// ensure that all Routes are sanitized
// to avoid unsafe HTML content
for i, v := range w.Routes {
w.Routes[i] = sanitize(v)
}
return nil
}
// WorkerFromLibrary converts the library worker type
// to a database worker type.
func WorkerFromLibrary(w *library.Worker) *Worker {
worker := &Worker{
ID: sql.NullInt64{Int64: w.GetID(), Valid: true},
Hostname: sql.NullString{String: w.GetHostname(), Valid: true},
Address: sql.NullString{String: w.GetAddress(), Valid: true},
Routes: pq.StringArray(w.GetRoutes()),
Active: sql.NullBool{Bool: w.GetActive(), Valid: true},
Status: sql.NullString{String: w.GetStatus(), Valid: true},
LastStatusUpdateAt: sql.NullInt64{Int64: w.GetLastStatusUpdateAt(), Valid: true},
RunningBuildIDs: pq.StringArray(w.GetRunningBuildIDs()),
LastBuildStartedAt: sql.NullInt64{Int64: w.GetLastBuildStartedAt(), Valid: true},
LastBuildFinishedAt: sql.NullInt64{Int64: w.GetLastBuildFinishedAt(), Valid: true},
LastCheckedIn: sql.NullInt64{Int64: w.GetLastCheckedIn(), Valid: true},
BuildLimit: sql.NullInt64{Int64: w.GetBuildLimit(), Valid: true},
}
return worker.Nullify()
}