Skip to content

Commit 2a744ec

Browse files
committed
Merge branch 'main' of github.com:go-vela/worker into feat/queue-signing
2 parents 65a4d8a + 054b67d commit 2a744ec

File tree

19 files changed

+329
-140
lines changed

19 files changed

+329
-140
lines changed

.github/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ Copyright (c) 2022 Target Brands, Inc.
3838
```
3939

4040
[Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)
41+

.gitignore

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ release/
3535
*.iws
3636
*.xml
3737

38-
# VSCode project folder
39-
.vscode/
40-
41-
# VSCode project files
42-
__debug_bin
43-
4438
# Secrets environment file
4539
secrets.env
4640

@@ -52,3 +46,28 @@ secrets.env
5246
.DS_Store
5347

5448
api-spec.json
49+
50+
# Created by https://www.toptal.com/developers/gitignore/api/visualstudiocode
51+
# Edit at https://www.toptal.com/developers/gitignore?templates=visualstudiocode
52+
53+
### VisualStudioCode ###
54+
.vscode/*
55+
!.vscode/settings.json
56+
!.vscode/tasks.json
57+
!.vscode/launch.json
58+
!.vscode/extensions.json
59+
!.vscode/*.code-snippets
60+
61+
# Local History for Visual Studio Code
62+
.history/
63+
64+
# Built Visual Studio Code Extensions
65+
*.vsix
66+
__debug_bin
67+
68+
### VisualStudioCode Patch ###
69+
# Ignore all local history of files
70+
.history
71+
.ionide
72+
73+
# End of https://www.toptal.com/developers/gitignore/api/visualstudiocode

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# Use of this source code is governed by the LICENSE file in this repository.
44

5-
FROM alpine as certs
5+
FROM alpine:3.18.2@sha256:25fad2a32ad1f6f510e528448ae1ec69a28ef81916a004d3629874104f8a7f70 as certs
66

77
RUN apk add --update --no-cache ca-certificates
88

Dockerfile-alpine

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# Use of this source code is governed by the LICENSE file in this repository.
44

5-
FROM alpine
5+
FROM alpine:3.18.2@sha256:25fad2a32ad1f6f510e528448ae1ec69a28ef81916a004d3629874104f8a7f70
66

77
RUN apk add --update --no-cache ca-certificates
88

cmd/vela-worker/exec.go

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,24 @@ package main
77
import (
88
"context"
99
"net/http"
10+
"strconv"
1011
"sync"
1112
"time"
1213

14+
"github.com/go-vela/types"
15+
"github.com/go-vela/types/constants"
16+
"github.com/go-vela/types/library"
1317
"github.com/go-vela/worker/executor"
1418
"github.com/go-vela/worker/runtime"
1519
"github.com/go-vela/worker/version"
16-
1720
"github.com/sirupsen/logrus"
1821
)
1922

2023
// exec is a helper function to poll the queue
2124
// and execute Vela pipelines for the Worker.
2225
//
2326
//nolint:nilerr,funlen // ignore returning nil - don't want to crash worker
24-
func (w *Worker) exec(index int) error {
27+
func (w *Worker) exec(index int, config *library.Worker) error {
2528
var err error
2629

2730
// setup the version
@@ -70,6 +73,48 @@ func (w *Worker) exec(index int) error {
7073
"version": v.Semantic(),
7174
})
7275

76+
// lock and append the build to the RunningBuildIDs list
77+
w.RunningBuildIDsMutex.Lock()
78+
79+
w.RunningBuildIDs = append(w.RunningBuildIDs, strconv.Itoa(item.Build.GetNumber()))
80+
81+
config.SetRunningBuildIDs(w.RunningBuildIDs)
82+
83+
w.RunningBuildIDsMutex.Unlock()
84+
85+
// set worker status
86+
updateStatus := w.getWorkerStatusFromConfig(config)
87+
config.SetStatus(updateStatus)
88+
config.SetLastStatusUpdateAt(time.Now().Unix())
89+
config.SetLastBuildStartedAt(time.Now().Unix())
90+
91+
// update worker in the database
92+
_, _, err = w.VelaClient.Worker.Update(config.GetHostname(), config)
93+
if err != nil {
94+
logger.Errorf("unable to update worker: %v", err)
95+
}
96+
97+
// handle stale item queued before a Vela upgrade or downgrade.
98+
if item.ItemVersion != types.ItemVersion {
99+
// If the ItemVersion is older or newer than what we expect, then it might
100+
// not be safe to process the build. Fail the build and loop to the next item.
101+
// TODO: Ask the server to re-compile and requeue the build instead of failing it.
102+
logrus.Errorf("Failing stale queued build due to wrong item version: want %d, got %d", types.ItemVersion, item.ItemVersion)
103+
104+
build := item.Build
105+
build.SetError("Unable to process stale build (queued before Vela upgrade/downgrade).")
106+
build.SetStatus(constants.StatusError)
107+
build.SetFinished(time.Now().UTC().Unix())
108+
109+
_, _, err := w.VelaClient.Build.Update(item.Repo.GetOrg(), item.Repo.GetName(), build)
110+
if err != nil {
111+
logrus.Errorf("Unable to set build status to %s: %s", constants.StatusFailure, err)
112+
return err
113+
}
114+
115+
return nil
116+
}
117+
73118
// setup the runtime
74119
//
75120
// https://pkg.go.dev/github.com/go-vela/worker/runtime?tab=doc#New
@@ -132,6 +177,32 @@ func (w *Worker) exec(index int) error {
132177
}
133178

134179
logger.Info("completed build")
180+
181+
// lock and remove the build from the RunningBuildIDs list
182+
w.RunningBuildIDsMutex.Lock()
183+
184+
for i, v := range w.RunningBuildIDs {
185+
if v == strconv.Itoa(item.Build.GetNumber()) {
186+
w.RunningBuildIDs = append(w.RunningBuildIDs[:i], w.RunningBuildIDs[i+1:]...)
187+
}
188+
}
189+
190+
config.SetRunningBuildIDs(w.RunningBuildIDs)
191+
192+
w.RunningBuildIDsMutex.Unlock()
193+
194+
// set worker status
195+
updateStatus := w.getWorkerStatusFromConfig(config)
196+
config.SetStatus(updateStatus)
197+
config.SetLastStatusUpdateAt(time.Now().Unix())
198+
config.SetLastBuildFinishedAt(time.Now().Unix())
199+
200+
// update worker in the database
201+
_, _, err := w.VelaClient.Worker.Update(config.GetHostname(), config)
202+
if err != nil {
203+
logger.Errorf("unable to update worker: %v", err)
204+
}
205+
135206
}()
136207

137208
// capture the configured build timeout
@@ -200,3 +271,18 @@ func (w *Worker) exec(index int) error {
200271

201272
return nil
202273
}
274+
275+
// getWorkerStatusFromConfig is a helper function
276+
// to determine the appropriate worker status
277+
func (w *Worker) getWorkerStatusFromConfig(config *library.Worker) string {
278+
switch rb := len(config.GetRunningBuildIDs()); {
279+
case rb == 0:
280+
return constants.WorkerStatusIdle
281+
case rb < w.Config.Build.Limit:
282+
return constants.WorkerStatusAvailable
283+
case rb == w.Config.Build.Limit:
284+
return constants.WorkerStatusBusy
285+
default:
286+
return constants.WorkerStatusError
287+
}
288+
}

cmd/vela-worker/operate.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/go-vela/server/queue"
12+
"github.com/go-vela/types/constants"
1213
"github.com/go-vela/types/library"
1314

1415
"github.com/sirupsen/logrus"
@@ -118,6 +119,18 @@ func (w *Worker) operate(ctx context.Context) error {
118119
//nolint:contextcheck // ignore passing context
119120
w.Queue, err = queue.New(w.Config.Queue)
120121
if err != nil {
122+
registryWorker.SetStatus(constants.WorkerStatusError)
123+
_, resp, logErr := w.VelaClient.Worker.Update(registryWorker.GetHostname(), registryWorker)
124+
if resp == nil {
125+
// log the error instead of returning so the operation doesn't block worker deployment
126+
logrus.Error("status update response is nil")
127+
}
128+
if logErr != nil {
129+
if resp != nil {
130+
// log the error instead of returning so the operation doesn't block worker deployment
131+
logrus.Errorf("status code: %v, unable to update worker %s status with the server: %v", resp.StatusCode, registryWorker.GetHostname(), logErr)
132+
}
133+
}
121134
return err
122135
}
123136

@@ -160,13 +173,24 @@ func (w *Worker) operate(ctx context.Context) error {
160173
// (do not pass the context to avoid errors in one
161174
// executor+build inadvertently canceling other builds)
162175
//nolint:contextcheck // ignore passing context
163-
err = w.exec(id)
176+
err = w.exec(id, registryWorker)
164177
if err != nil {
165178
// log the error received from the executor
166179
//
167180
// https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Errorf
168181
logrus.Errorf("failing worker executor: %v", err)
169-
182+
registryWorker.SetStatus(constants.WorkerStatusError)
183+
_, resp, logErr := w.VelaClient.Worker.Update(registryWorker.GetHostname(), registryWorker)
184+
if resp == nil {
185+
// log the error instead of returning so the operation doesn't block worker deployment
186+
logrus.Error("status update response is nil")
187+
}
188+
if logErr != nil {
189+
if resp != nil {
190+
// log the error instead of returning so the operation doesn't block worker deployment
191+
logrus.Errorf("status code: %v, unable to update worker %s status with the server: %v", resp.StatusCode, registryWorker.GetHostname(), logErr)
192+
}
193+
}
170194
return err
171195
}
172196
}

cmd/vela-worker/register.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"net/http"
1010

11+
"github.com/go-vela/types/constants"
1112
"github.com/go-vela/types/library"
1213
"github.com/sirupsen/logrus"
1314
)
@@ -46,12 +47,16 @@ func (w *Worker) checkIn(config *library.Worker) (bool, string, error) {
4647
func (w *Worker) register(config *library.Worker) (bool, string, error) {
4748
logrus.Infof("worker %s not found, registering it with the server", config.GetHostname())
4849

50+
config.SetStatus(constants.WorkerStatusIdle)
51+
4952
tkn, _, err := w.VelaClient.Worker.Add(config)
5053
if err != nil {
5154
// log the error instead of returning so the operation doesn't block worker deployment
5255
return false, "", fmt.Errorf("unable to register worker %s with the server: %w", config.GetHostname(), err)
5356
}
5457

58+
logrus.Infof("worker %q status updated successfully to %s", config.GetHostname(), config.GetStatus())
59+
5560
// successfully added the worker so return nil
5661
return true, tkn.GetToken(), nil
5762
}

cmd/vela-worker/run.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ func run(c *cli.Context) error {
139139
Executors: make(map[int]executor.Engine),
140140

141141
RegisterToken: make(chan string, 1),
142+
143+
RunningBuildIDs: make([]string, 0),
142144
}
143145

144146
// set the worker address if no flag was provided

cmd/vela-worker/worker.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package main
66

77
import (
88
"net/url"
9+
"sync"
910
"time"
1011

1112
"github.com/go-vela/sdk-go/vela"
@@ -62,12 +63,14 @@ type (
6263
// Worker represents all configuration and
6364
// system processes for the worker.
6465
Worker struct {
65-
Config *Config
66-
Executors map[int]executor.Engine
67-
Queue queue.Service
68-
Runtime runtime.Engine
69-
VelaClient *vela.Client
70-
RegisterToken chan string
71-
CheckedIn bool
66+
Config *Config
67+
Executors map[int]executor.Engine
68+
Queue queue.Service
69+
Runtime runtime.Engine
70+
VelaClient *vela.Client
71+
RegisterToken chan string
72+
CheckedIn bool
73+
RunningBuildIDs []string
74+
RunningBuildIDsMutex sync.Mutex
7275
}
7376
)

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ services:
145145
#
146146
# https://www.vaultproject.io/
147147
vault:
148-
image: vault:latest
148+
image: hashicorp/vault:latest
149149
container_name: vault
150150
command: server -dev
151151
networks:

0 commit comments

Comments
 (0)