| 1 | import Foundation |
| 2 | |
| 3 | public struct StepLog: Codable, Sendable { |
| 4 | public var stepId: String |
| 5 | public var attempt: Int64 |
| 6 | public var tail: String |
| 7 | |
| 8 | /// The server bounds stored logs; also bound unexpected older responses. |
| 9 | public var boundedTail: String { |
| 10 | let bytes = tail.utf8.suffix(32 * 1024).drop(while: { $0 & 0xc0 == 0x80 }) |
| 11 | return String(decoding: bytes, as: UTF8.self).split(separator: "\n", omittingEmptySubsequences: false).suffix(200).joined(separator: "\n") |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | struct TaskAccepted: Decodable, Sendable { let attempt: Int64 } |
| 16 | |
| 17 | struct ResumeConnection: Codable, Sendable { |
| 18 | let id: String |
| 19 | let runnerId: String |
| 20 | let localId: String |
| 21 | let provider: String |
| 22 | let spend: Spend? |
| 23 | struct Spend: Codable, Sendable { |
| 24 | let unknown: Bool |
| 25 | let pauseRevision: Int64 |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | extension EquipoClient { |
| 30 | public func stepLog(_ id: String) async throws -> StepLog { try await get("/steps/\(escape(id))/log") } |
| 31 | func retryTask(_ id: String) async throws -> TaskAccepted { try await post("/tasks/\(escape(id))/retry", Self.empty()) } |
| 32 | func recoverTask(_ id: String) async throws -> TaskAccepted { try await post("/tasks/\(escape(id))/recover", Self.empty()) } |
| 33 | func resumeConnections() async throws -> ThinList<ResumeConnection> { try await get("/connections") } |
| 34 | func resumeConnection(_ connection: ResumeConnection) async throws { |
| 35 | guard let spend = connection.spend, spend.unknown else { return } |
| 36 | let _: EmptyResponse = try await post("/connections/\(escape(connection.id))/resume", try .json(["pause_revision": spend.pauseRevision])) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | enum StoppedWorkActions { |
| 41 | static func retry(client: EquipoClient, task: String) async -> String? { |
| 42 | do { _ = try await client.retryTask(task); return nil } |
| 43 | catch let error as EquipoAPIError where error.status == 409 && error.code == "not_retryable" { |
| 44 | return EquipoStrings.text("api.not_retryable") |
| 45 | } catch { return EquipoStrings.text("card.failed") } |
| 46 | } |
| 47 | |
| 48 | static func recover(client: EquipoClient, task: String) async -> String? { |
| 49 | do { _ = try await client.recoverTask(task); return nil } |
| 50 | catch { return EquipoStrings.text("card.failed") } |
| 51 | } |
| 52 | |
| 53 | static func runNow(client: EquipoClient, task: String, run: TaskPause.RunNow) async -> String? { |
| 54 | do { _ = try await client.runNow(task: task, step: run.step, revision: run.revision, offer: run.offer); return nil } |
| 55 | catch let error as EquipoAPIError where error.status == 409 { |
| 56 | switch error.code { |
| 57 | case "offer_changed": return EquipoStrings.text("api.offer_changed") |
| 58 | case "already_running": return EquipoStrings.text("api.already_running") |
| 59 | case "hold_bar": return EquipoStrings.text("api.hold_bar") |
| 60 | default: return EquipoStrings.text("card.failed") |
| 61 | } |
| 62 | } |
| 63 | catch { return EquipoStrings.text("card.failed") } |
| 64 | } |
| 65 | |
| 66 | static func logFailure(_ error: Error) -> String { |
| 67 | if let error = error as? EquipoAPIError { |
| 68 | if error == .undecodable { return EquipoStrings.text("read.failed") } |
| 69 | if error == .offline { return EquipoStrings.text("card.offline") } |
| 70 | } |
| 71 | if error is URLError { return EquipoStrings.text("card.offline") } |
| 72 | return EquipoStrings.text("read.failed_title") |
| 73 | } |
| 74 | |
| 75 | static func connection(for step: ThinStep, in connections: [ResumeConnection]) -> ResumeConnection? { |
| 76 | guard let runner = step.runnerId, let hold = step.hold else { return nil } |
| 77 | return connections.first { $0.runnerId == runner && $0.localId == hold.connection && $0.provider == hold.provider && $0.spend?.unknown == true } |
| 78 | } |
| 79 | } |
| 80 | |