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

Add JobParameters that collates job parameters and id into one #398

Merged
merged 2 commits into from Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions Sources/HummingbirdJobs/JobDefinition.swift
Expand Up @@ -18,6 +18,11 @@ public struct JobDefinition<Parameters: Codable & Sendable>: Sendable {
let maxRetryCount: Int
let _execute: @Sendable (Parameters, JobContext) async throws -> Void

/// Initialize JobDefinition
/// - Parameters:
/// - id: Job identifier
/// - maxRetryCount: Maxiumum times this job will be retried if it fails
/// - execute: Closure that executes job
public init(id: JobIdentifier<Parameters>, maxRetryCount: Int = 0, execute: @escaping @Sendable (Parameters, JobContext) async throws -> Void) {
self.id = id
self.maxRetryCount = maxRetryCount
Expand Down
55 changes: 55 additions & 0 deletions Sources/HummingbirdJobs/JobParameters.swift
@@ -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)
}
}
20 changes: 20 additions & 0 deletions Tests/HummingbirdJobsTests/HummingbirdJobsTests.swift
Expand Up @@ -164,6 +164,26 @@ final class HummingbirdJobsTests: XCTestCase {
}
}

func testJobParameters() async throws {
struct TestJobParameters: JobParameters {
static let jobID: String = "TestJobParameters"
let id: Int
let message: String
}
let expectation = XCTestExpectation(description: "TestJob.execute was called")
let jobQueue = JobQueue(.memory, numWorkers: 1, logger: Logger(label: "HummingbirdJobsTests"))
jobQueue.registerJob(parameters: TestJobParameters.self) { parameters, _ in
XCTAssertEqual(parameters.id, 23)
XCTAssertEqual(parameters.message, "Hello!")
expectation.fulfill()
}
try await self.testJobQueue(jobQueue) {
try await jobQueue.push(TestJobParameters(id: 23, message: "Hello!"))

await self.wait(for: [expectation], timeout: 5)
}
}

/// Verify test job is cancelled when service group is cancelled
func testShutdownJob() async throws {
let jobIdentifer = JobIdentifier<Int>(#function)
Expand Down