Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Continue on error in the main task group #65

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions Sources/ValidatorCore/Commands/CheckRedirects.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,36 +139,41 @@ extension Validator {

let semaphore = Semaphore(maximum: concurrency ?? 1)

let updated = try await withThrowingTaskGroup(of: PackageURL?.self) { group in
let updated = await withTaskGroup(of: PackageURL?.self) { group in
for (index, packageURL) in inputURLs[offset...]
.prefix(prefix)
.chunk(index: chunk, of: numberOfChunks)
.enumerated() {
await semaphore.increment()
try? await semaphore.waitForAvailability()
group.addTask {
let index = index + offset
let redirect = try await resolvePackageRedirects(client: httpClient, for: packageURL)

if index % 100 == 0, let token = Current.githubToken() {
let rateLimit = try await Github.getRateLimit(client: httpClient, token: token).get()
if rateLimit.remaining < 200 {
print("Rate limit remaining: \(rateLimit.remaining)")
print("Sleeping until reset at \(rateLimit.resetDate) ...")
sleep(UInt32(rateLimit.secondsUntilReset + 0.5))
do {
let index = index + offset
let redirect = try await resolvePackageRedirects(client: httpClient, for: packageURL)

if index % 100 == 0, let token = Current.githubToken() {
let rateLimit = try await Github.getRateLimit(client: httpClient, token: token).get()
if rateLimit.remaining < 200 {
print("Rate limit remaining: \(rateLimit.remaining)")
print("Sleeping until reset at \(rateLimit.resetDate) ...")
sleep(UInt32(rateLimit.secondsUntilReset + 0.5))
}
}

let res = try await Self.process(redirect: redirect,
verbose: verbose,
index: index,
packageURL: packageURL)

await semaphore.decrement()
return res
} catch {
print("Error in main task group: \(error)")
return nil
}

let res = try await Self.process(redirect: redirect,
verbose: verbose,
index: index,
packageURL: packageURL)

await semaphore.decrement()
return res
}
}
return try await group
return await group
.compactMap { $0 }
.reduce(into: [], { res, next in res.append(next) })
.sorted(by: { $0.lowercased() < $1.lowercased() })
Expand Down
Loading