Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Register jobs with an id and a closure * Add precondition to check job isn't registered twice * Move job registry inside queue handler * HBJobQueue is now a struct containing a driver and handler * comments * Add DecodableWithUserInfoConfiguration * Init HBJobIdentifier from string literal * Make HBJobDefinition.id public, Rename HBJobRegister -> HBJobRegistry * Remove Hummingbird dependency from HummingbirdJobs * Add HBJobQueueDriver.memory * Update preprocessor check in HummingbirdJobsTests.wait * Use ByteBuffer instead of Data * Add dependent modules to HummingbirdJobs
- Loading branch information
1 parent
5928003
commit b5fd7d4
Showing
14 changed files
with
619 additions
and
335 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
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,30 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Hummingbird server framework project | ||
// | ||
// Copyright (c) 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
/// Job definition type | ||
public struct HBJobDefinition<Parameters: Codable & Sendable>: Sendable { | ||
public let id: HBJobIdentifier<Parameters> | ||
let maxRetryCount: Int | ||
let _execute: @Sendable (Parameters, HBJobContext) async throws -> Void | ||
|
||
public init(id: HBJobIdentifier<Parameters>, maxRetryCount: Int = 0, execute: @escaping @Sendable (Parameters, HBJobContext) async throws -> Void) { | ||
self.id = id | ||
self.maxRetryCount = maxRetryCount | ||
self._execute = execute | ||
} | ||
|
||
func execute(_ parameters: Parameters, context: HBJobContext) async throws { | ||
try await self._execute(parameters, context) | ||
} | ||
} |
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,42 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Hummingbird server framework project | ||
// | ||
// Copyright (c) 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
/// Identifier for a Job type | ||
/// | ||
/// The identifier includes the type of the parameters required by the job to ensure | ||
/// the wrong parameters are not passed to this job | ||
/// | ||
/// Extend this type to include your own job identifiers | ||
/// ``` | ||
/// extension HBJobIdentifier<String> { | ||
/// static var myJob: Self { .init("my-job") } | ||
/// } | ||
/// ``` | ||
public struct HBJobIdentifier<Parameters>: Sendable, Hashable, ExpressibleByStringLiteral { | ||
let name: String | ||
/// Initialize a HBJobIdentifier | ||
/// | ||
/// - Parameters: | ||
/// - name: Unique name for identifier | ||
/// - parameters: Parameter type associated with Job | ||
public init(_ name: String, parameters: Parameters.Type = Parameters.self) { self.name = name } | ||
|
||
/// Initialize a HBJobIdentifier from a string literal | ||
/// | ||
/// This can only be used in a situation where the Parameter type is defined elsewhere | ||
/// - Parameter string: | ||
public init(stringLiteral string: String) { | ||
self.name = string | ||
} | ||
} |
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,43 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Foundation | ||
import Logging | ||
import NIOCore | ||
|
||
/// Job queue protocol. | ||
/// | ||
/// Defines how to push and pop job data off a queue | ||
public protocol HBJobQueueDriver: AsyncSequence, Sendable where Element == HBQueuedJob<JobID> { | ||
associatedtype JobID: CustomStringConvertible & Sendable | ||
|
||
/// Called when JobQueueHandler is initialised with this queue | ||
func onInit() async throws | ||
/// Push Job onto queue | ||
/// - Returns: Identifier of queued job | ||
func push(_ buffer: ByteBuffer) async throws -> JobID | ||
/// This is called to say job has finished processing and it can be deleted | ||
func finished(jobId: JobID) async throws | ||
/// This is called to say job has failed to run and should be put aside | ||
func failed(jobId: JobID, error: any Error) async throws | ||
/// stop serving jobs | ||
func stop() async | ||
/// shutdown queue | ||
func shutdownGracefully() async | ||
} | ||
|
||
extension HBJobQueueDriver { | ||
// default version of onInit doing nothing | ||
public func onInit() async throws {} | ||
} |
Oops, something went wrong.