-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
JobParameters.swift
55 lines (51 loc) · 2.04 KB
/
JobParameters.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//===----------------------------------------------------------------------===//
//
// This source file is part of the Hummingbird server framework project
//
// Copyright (c) 2021-2024 the Hummingbird authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
/// Defines job parameters and identifier
public protocol JobParameters: Codable, Sendable {
/// Job type identifier
static var jobID: String { get }
}
extension JobQueue {
/// Push Job onto queue
/// - Parameters:
/// - parameters: parameters for the job
/// - Returns: Identifier of queued job
@discardableResult public func push<Parameters: JobParameters>(_ parameters: Parameters) async throws -> Queue.JobID {
return try await self.push(id: .init(Parameters.jobID), parameters: parameters)
}
/// Register job type
/// - Parameters:
/// - parameters: Job parameter type
/// - maxRetryCount: Maximum number of times job is retried before being flagged as failed
/// - execute: Job code
public func registerJob<Parameters: JobParameters>(
parameters: Parameters.Type = Parameters.self,
maxRetryCount: Int = 0,
execute: @escaping @Sendable (
Parameters,
JobContext
) async throws -> Void
) {
self.registerJob(id: .init(Parameters.jobID), maxRetryCount: maxRetryCount, execute: execute)
}
}
extension JobDefinition where Parameters: JobParameters {
/// Initialize JobDefinition
/// - Parameters:
/// - maxRetryCount: Maxiumum times this job will be retried if it fails
/// - execute: Closure that executes job
public init(maxRetryCount: Int = 0, execute: @escaping @Sendable (Parameters, JobContext) async throws -> Void) {
self.init(id: .init(Parameters.jobID), maxRetryCount: maxRetryCount, execute: execute)
}
}