Skip to content

Commit df39beb

Browse files
authored
Fix: Only run worker id action getter query when needed (#3976)
* fix: add if block to check if any ids / hashes are present * fix: add some tracing * feat: add pagination * feat: server-side status filter * fix: conditions * chore: lint * fix: backwards compat * fix: backwards compat * chore: couple more
1 parent f3fe579 commit df39beb

17 files changed

Lines changed: 695 additions & 291 deletions

File tree

api-contracts/openapi/components/schemas/_index.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ WorkerRuntimeInfo:
252252
$ref: "./worker.yaml#/WorkerRuntimeInfo"
253253
WorkerRuntimeSDKs:
254254
$ref: "./worker.yaml#/WorkerRuntimeSDKs"
255+
WorkerStatus:
256+
$ref: "./worker.yaml#/WorkerStatus"
255257
WorkerList:
256258
$ref: "./worker.yaml#/WorkerList"
257259
SemaphoreSlots:

api-contracts/openapi/components/schemas/worker.yaml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,8 @@ Worker:
143143
items:
144144
$ref: "./_index.yaml#/RecentStepRuns"
145145
status:
146-
type: string
146+
$ref: "#/WorkerStatus"
147147
description: The status of the worker.
148-
enum:
149-
- ACTIVE
150-
- INACTIVE
151-
- PAUSED
152148
slotConfig:
153149
type: object
154150
description: Slot availability and limits for this worker (slot_type -> { available, limit }).
@@ -217,3 +213,10 @@ WorkerRuntimeInfo:
217213
type: string
218214
runtimeExtra:
219215
type: string
216+
217+
WorkerStatus:
218+
type: string
219+
enum:
220+
- ACTIVE
221+
- INACTIVE
222+
- PAUSED

api-contracts/openapi/paths/worker/worker.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,28 @@ withTenant:
1313
format: uuid
1414
minLength: 36
1515
maxLength: 36
16+
- description: The number to skip
17+
in: query
18+
name: offset
19+
required: false
20+
schema:
21+
type: integer
22+
format: int64
23+
- description: The number to limit by
24+
in: query
25+
name: limit
26+
required: false
27+
schema:
28+
type: integer
29+
format: int64
30+
- description: Filter by worker status
31+
in: query
32+
name: statuses
33+
required: false
34+
schema:
35+
type: array
36+
items:
37+
$ref: "../../components/schemas/_index.yaml#/WorkerStatus"
1638
responses:
1739
"200":
1840
content:

api/v1/server/handlers/workers/list.go

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package workers
22

33
import (
44
"fmt"
5+
"math"
56
"time"
67

78
"github.com/google/uuid"
@@ -35,8 +36,31 @@ func (t *WorkerService) workerListV0(ctx echo.Context, tenant *sqlcv1.Tenant, re
3536

3637
sixSecAgo := time.Now().Add(-24 * time.Hour)
3738

39+
limit := 50
40+
offset := 0
41+
3842
opts := &v1.ListWorkersOpts{
3943
LastHeartbeatAfter: &sixSecAgo,
44+
Limit: &limit,
45+
Offset: &offset,
46+
}
47+
48+
if request.Params.Limit != nil {
49+
limit = int(*request.Params.Limit)
50+
opts.Limit = &limit
51+
}
52+
53+
if request.Params.Offset != nil {
54+
offset = int(*request.Params.Offset)
55+
opts.Offset = &offset
56+
}
57+
58+
if request.Params.Statuses != nil {
59+
statuses := make([]string, len(*request.Params.Statuses))
60+
for i, s := range *request.Params.Statuses {
61+
statuses[i] = string(s)
62+
}
63+
opts.Statuses = statuses
4064
}
4165

4266
_, listSpan := telemetry.NewSpan(reqCtx, "worker-service.v0.list-workers")
@@ -46,7 +70,7 @@ func (t *WorkerService) workerListV0(ctx echo.Context, tenant *sqlcv1.Tenant, re
4670
telemetry.AttributeKV{Key: "tenant.id", Value: tenant.ID},
4771
)
4872

49-
workers, err := t.config.V1.Workers().ListWorkers(reqCtx, tenantId, opts)
73+
workers, count, err := t.config.V1.Workers().ListWorkers(reqCtx, tenantId, opts)
5074

5175
if err != nil {
5276
listSpan.RecordError(err)
@@ -75,9 +99,22 @@ func (t *WorkerService) workerListV0(ctx echo.Context, tenant *sqlcv1.Tenant, re
7599
rows[i] = *transformers.ToWorkerSqlc(&workerCp.Worker, slotConfig, nil)
76100
}
77101

102+
totalPages := int64(math.Ceil(float64(count) / float64(limit)))
103+
currPage := 1 + int64(math.Ceil(float64(offset)/float64(limit)))
104+
nextPage := currPage + 1
105+
106+
if currPage == totalPages {
107+
nextPage = currPage
108+
}
109+
78110
return gen.WorkerList200JSONResponse(
79111
gen.WorkerList{
80112
Rows: &rows,
113+
Pagination: &gen.PaginationResponse{
114+
NumPages: &totalPages,
115+
CurrentPage: &currPage,
116+
NextPage: &nextPage,
117+
},
81118
},
82119
), nil
83120
}
@@ -88,8 +125,31 @@ func (t *WorkerService) workerListV1(ctx echo.Context, tenant *sqlcv1.Tenant, re
88125

89126
sixSecAgo := time.Now().Add(-24 * time.Hour)
90127

128+
limit := 50
129+
offset := 0
130+
91131
opts := &v1.ListWorkersOpts{
92132
LastHeartbeatAfter: &sixSecAgo,
133+
Limit: &limit,
134+
Offset: &offset,
135+
}
136+
137+
if request.Params.Limit != nil {
138+
limit = int(*request.Params.Limit)
139+
opts.Limit = &limit
140+
}
141+
142+
if request.Params.Offset != nil {
143+
offset = int(*request.Params.Offset)
144+
opts.Offset = &offset
145+
}
146+
147+
if request.Params.Statuses != nil {
148+
statuses := make([]string, len(*request.Params.Statuses))
149+
for i, s := range *request.Params.Statuses {
150+
statuses[i] = string(s)
151+
}
152+
opts.Statuses = statuses
93153
}
94154

95155
listCtx, listSpan := telemetry.NewSpan(reqCtx, "worker-service.v1.list-workers")
@@ -99,7 +159,7 @@ func (t *WorkerService) workerListV1(ctx echo.Context, tenant *sqlcv1.Tenant, re
99159
telemetry.AttributeKV{Key: "tenant.id", Value: tenant.ID},
100160
)
101161

102-
workerRows, err := t.config.V1.Workers().ListWorkers(listCtx, tenantId, opts)
162+
workerRows, count, err := t.config.V1.Workers().ListWorkers(listCtx, tenantId, opts)
103163

104164
if err != nil {
105165
listSpan.RecordError(err)
@@ -173,9 +233,22 @@ func (t *WorkerService) workerListV1(ctx echo.Context, tenant *sqlcv1.Tenant, re
173233
rows[i] = *transformersv1.ToWorkerSqlc(&workerCp, slotConfig, actions, nil, labels)
174234
}
175235

236+
totalPages := int64(math.Ceil(float64(count) / float64(limit)))
237+
currPage := 1 + int64(math.Ceil(float64(offset)/float64(limit)))
238+
nextPage := currPage + 1
239+
240+
if currPage == totalPages {
241+
nextPage = currPage
242+
}
243+
176244
return gen.WorkerList200JSONResponse(
177245
gen.WorkerList{
178246
Rows: &rows,
247+
Pagination: &gen.PaginationResponse{
248+
NumPages: &totalPages,
249+
CurrentPage: &currPage,
250+
NextPage: &nextPage,
251+
},
179252
},
180253
), nil
181254
}

0 commit comments

Comments
 (0)