-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JobParameters that collates job parameters and id into one (#398)
- Loading branch information
1 parent
0d5bf5b
commit e80b318
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters