Skip to content

Commit 2d12fff

Browse files
committed
[image-builder] Maintain local runnig build state
1 parent cbc4da6 commit 2d12fff

File tree

2 files changed

+48
-21
lines changed

2 files changed

+48
-21
lines changed

components/image-builder-mk3/pkg/orchestrator/monitor.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@ import (
2121
func (o *Orchestrator) monitor() {
2222
ctx := context.Background()
2323
for {
24+
wss, err := o.wsman.GetWorkspaces(ctx, &wsmanapi.GetWorkspacesRequest{
25+
MustMatch: &wsmanapi.MetadataFilter{
26+
Owner: buildWorkspaceOwnerID,
27+
},
28+
})
29+
if err != nil {
30+
log.WithError(err).Info("cannot get running builds from ws-manager - retrying")
31+
time.Sleep(5 * time.Second)
32+
continue
33+
}
34+
o.runningBuildsMu.Lock()
35+
o.runningBuilds = make(map[string]*api.BuildInfo, len(wss.Status))
36+
for _, ws := range wss.Status {
37+
o.runningBuilds[ws.Id] = extractBuildStatus(ws)
38+
}
39+
o.runningBuildsMu.Unlock()
40+
2441
sub, err := o.wsman.Subscribe(ctx, &wsmanapi.SubscribeRequest{
2542
MustMatch: &wsmanapi.MetadataFilter{
2643
Owner: buildWorkspaceOwnerID,
@@ -138,6 +155,14 @@ func (o *Orchestrator) publishStatus(msg *wsmanapi.WorkspaceStatus) {
138155
}
139156

140157
resp := extractBuildResponse(msg)
158+
o.runningBuildsMu.Lock()
159+
if resp.Status != api.BuildStatus_running {
160+
delete(o.runningBuilds, msg.Id)
161+
} else {
162+
o.runningBuilds[msg.Id] = extractBuildStatus(msg)
163+
}
164+
o.runningBuildsMu.Unlock()
165+
141166
for l := range listener {
142167
select {
143168
case l <- resp:

components/image-builder-mk3/pkg/orchestrator/orchestrator.go

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ func NewOrchestratingBuilder(cfg Configuration) (res *Orchestrator, err error) {
179179
gplayerHash: gplayerHash,
180180
buildListener: make(map[string]map[buildListener]struct{}),
181181
logListener: make(map[string]map[logListener]struct{}),
182+
runningBuilds: make(map[string]*protocol.BuildInfo),
182183
censorship: make(map[string][]string),
183184
builderAuthKey: builderAuthKey,
184185
}, nil
@@ -216,11 +217,13 @@ type Orchestrator struct {
216217
gplayerHash string
217218
wsman wsmanapi.WorkspaceManagerClient
218219

219-
builderAuthKey [32]byte
220-
buildListener map[string]map[buildListener]struct{}
221-
logListener map[string]map[logListener]struct{}
222-
censorship map[string][]string
223-
mu sync.RWMutex
220+
builderAuthKey [32]byte
221+
buildListener map[string]map[buildListener]struct{}
222+
logListener map[string]map[logListener]struct{}
223+
runningBuilds map[string]*protocol.BuildInfo
224+
runningBuildsMu sync.RWMutex
225+
censorship map[string][]string
226+
mu sync.RWMutex
224227

225228
protocol.UnimplementedImageBuilderServer
226229
}
@@ -426,8 +429,16 @@ func (o *Orchestrator) Build(req *protocol.BuildRequest, resp protocol.ImageBuil
426429
})
427430
return
428431
}, 1*time.Second, 5)
429-
if err != nil && status.Code(err) != codes.AlreadyExists {
432+
if status.Code(err) == codes.AlreadyExists {
433+
// build is already running - do not add it to the list of builds
434+
} else if err != nil {
430435
return status.Errorf(codes.Internal, "cannot start build: %q", err)
436+
} else {
437+
o.runningBuilds[buildID] = &protocol.BuildInfo{
438+
Ref: wsrefstr,
439+
Status: protocol.BuildStatus_running,
440+
StartedAt: time.Now().Unix(),
441+
}
431442
}
432443

433444
updates, cancel := o.registerBuildListener(buildID)
@@ -505,7 +516,7 @@ func (o *Orchestrator) ListBuilds(ctx context.Context, req *protocol.ListBuildsR
505516
return &protocol.ListBuildsResponse{Builds: res}, nil
506517
}
507518

508-
func extractBuildStats(ws *wsmanapi.WorkspaceStatus) *protocol.BuildInfo {
519+
func extractBuildStatus(ws *wsmanapi.WorkspaceStatus) *protocol.BuildInfo {
509520
return &protocol.BuildInfo{
510521
Ref: ws.Metadata.Annotations["ref"],
511522
StartedAt: ws.Metadata.StartedAt.Seconds,
@@ -514,21 +525,12 @@ func extractBuildStats(ws *wsmanapi.WorkspaceStatus) *protocol.BuildInfo {
514525
}
515526

516527
func (o *Orchestrator) getAllRunningBuilds(ctx context.Context) (res []*protocol.BuildInfo, err error) {
517-
span, ctx := opentracing.StartSpanFromContext(ctx, "getAllRunningBuilds")
518-
defer tracing.FinishSpan(span, &err)
519-
520-
wss, err := o.wsman.GetWorkspaces(ctx, &wsmanapi.GetWorkspacesRequest{
521-
MustMatch: &wsmanapi.MetadataFilter{
522-
Owner: buildWorkspaceOwnerID,
523-
},
524-
})
525-
if err != nil {
526-
return
527-
}
528+
o.runningBuildsMu.RLock()
529+
defer o.runningBuildsMu.RUnlock()
528530

529-
res = make([]*protocol.BuildInfo, len(wss.Status))
530-
for i, ws := range wss.Status {
531-
res[i] = extractBuildStats(ws)
531+
res = make([]*protocol.BuildInfo, 0, len(o.runningBuilds))
532+
for _, ws := range o.runningBuilds {
533+
res = append(res, ws)
532534
}
533535

534536
return

0 commit comments

Comments
 (0)