@@ -7,21 +7,24 @@ package main
77import (
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+ }
0 commit comments