Skip to content

Commit 6aacfcb

Browse files
Merge pull request #4027 from SwiftPackageIndex/mitigate-gitlab-500s-part-2
Mitigate gitlab 500s part 2
2 parents 32040ec + 78504dc commit 6aacfcb

4 files changed

Lines changed: 41 additions & 12 deletions

File tree

Sources/App/Commands/TriggerBuilds.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,9 @@ func triggerBuilds(on database: Database,
197197
async let pendingJobsTask = getStatusCount(.pending)
198198
async let runningJobsTask = getStatusCount(.running)
199199
let pendingJobs = try await pendingJobsTask
200-
// 2026-04-10 sas: default running job count to (current) maximum to mitigate 500s from Gitlab API.
200+
// 2026-04-10 sas: default running job count to 0 to mitigate 500s from Gitlab API.
201201
// See https://github.com/SwiftPackageIndex/SwiftPackageIndex-Server/issues/4024 for details.
202-
let runningJobs = (try? await runningJobsTask) ?? 20
202+
let runningJobs = (try? await runningJobsTask) ?? 0
203203

204204
AppMetrics.buildPendingJobsCount?.set(pendingJobs)
205205
AppMetrics.buildRunningJobsCount?.set(runningJobs)

Sources/App/Core/Gitlab.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,19 +152,18 @@ extension Gitlab.Builder {
152152
}
153153

154154
// periphery:ignore
155-
struct Pipeline: Decodable {
155+
struct Job: Decodable {
156156
var id: Int
157-
var status: Status
158157
}
159158

160-
// https://docs.gitlab.com/ee/api/pipelines.html
161-
static func fetchPipelines(status: Status,
162-
page: Int,
163-
pageSize: Int = 20) async throws -> [Pipeline] {
159+
// https://docs.gitlab.com/ee/api/jobs.html
160+
static func fetchJobs(status: Status,
161+
page: Int,
162+
pageSize: Int = 20) async throws -> [Job] {
164163
@Dependency(\.environment) var environment
165164
@Dependency(\.httpClient) var httpClient
166165
guard let apiToken = environment.gitlabApiToken() else { throw Gitlab.Error.missingToken }
167-
let url = "\(projectURL)/pipelines?status=\(status)&page=\(page)&per_page=\(pageSize)"
166+
let url = "\(projectURL)/jobs?scope=\(status)&page=\(page)&per_page=\(pageSize)"
168167

169168
let response = try await httpClient.get(url: url, headers: .bearer(apiToken))
170169

@@ -173,14 +172,14 @@ extension Gitlab.Builder {
173172
}
174173
guard let body = response.body else { throw Gitlab.Error.noBody }
175174

176-
return try Gitlab.decoder.decode([Pipeline].self, from: body)
175+
return try Gitlab.decoder.decode([Job].self, from: body)
177176
}
178177

179178
static func getStatusCount(status: Status,
180179
page: Int = 1,
181180
pageSize: Int = 20,
182181
maxPageCount: Int = 5) async throws -> Int {
183-
let count = try await fetchPipelines(status: status, page: page, pageSize: pageSize).count
182+
let count = try await fetchJobs(status: status, page: page, pageSize: pageSize).count
184183
if count == pageSize && page < maxPageCount {
185184
let statusCount = try await getStatusCount(status: status,
186185
page: page + 1,

Tests/AppTests/GitlabBuilderTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ extension AllTests.GitlabBuilderTests {
133133
$0.environment.gitlabApiToken = { "api token" }
134134
$0.httpClient.get = { @Sendable url, _ in
135135
#expect(
136-
url == "https://gitlab.com/api/v4/projects/19564054/pipelines?status=pending&page=\(page.value)&per_page=20"
136+
url == "https://gitlab.com/api/v4/projects/19564054/jobs?scope=pending&page=\(page.value)&per_page=20"
137137
)
138138
let pending = #"{"id": 1, "status": "pending"}"#
139139
defer { page.increment() }

restfiles/gitlab-jobs.restfile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright Dave Verwer, Sven A. Schmidt, and other contributors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
variables:
16+
api_url: https://gitlab.com/api/v4
17+
project_id: 19564054
18+
19+
requests:
20+
jobs:
21+
url: ${api_url}/projects/${project_id}/jobs
22+
query:
23+
scope: running
24+
page: 1
25+
per_page: 20
26+
headers:
27+
Authorization: Bearer ${API_TOKEN}
28+
validation:
29+
status: 200
30+
log: json

0 commit comments

Comments
 (0)