Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions Sources/Jobs/JobInstance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,15 @@ struct JobInstance<Parameters: Sendable & Codable>: JobInstanceProtocol {
/// Data attach to a job
public struct JobInstanceData<Parameters: Sendable & Codable>: Codable, Sendable {
/// Job parameters
@usableFromInline
let parameters: Parameters
public let parameters: Parameters
/// Time job was queued
let queuedAt: Date
public let queuedAt: Date
/// Current attempt
@usableFromInline
let attempt: Int
public let attempt: Int
/// trace context
let traceContext: [String: String]?
public let traceContext: [String: String]?
/// Next time job is scheduled to run
let nextScheduledAt: Date?
public let nextScheduledAt: Date?

init(
parameters: Parameters,
Expand Down
64 changes: 64 additions & 0 deletions Sources/Jobs/QueryableJobQueue.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// This source file is part of the Hummingbird server framework project
// Copyright (c) the Hummingbird authors
//
// See LICENSE.txt for license information
// SPDX-License-Identifier: Apache-2.0
//

/// A protocol that extends `JobQueueDriver` by providing the ability to
/// query and iterate over jobs in the queue matching specific criteria.
///
/// Conformers must implement a method to perform an async operation for
/// each job that matches a given `JobQueueQuery`.
public protocol QueryableJobQueueDriver: JobQueueDriver {
/// Iterates over each job of the given type matching the provided query,
/// performing the specified operation for each.
///
/// - Parameters:
/// - type: The type of the parameters for the job. This is used for type safety and decoding.
/// - query: A `JobQueueQuery` that specifies which jobs to include.
/// - perform: A closure that is called with the job's ID and data for each matched job.
func withEachJob<Parameters: Sendable>(
ofType type: Parameters.Type,
matching query: JobQueueQuery<Parameters>,
perform: @Sendable @escaping (JobID, JobInstanceData<Parameters>) async throws -> Void
) async throws
}

/// Default implementation for `withEachJob` that includes all jobs of the given type,
/// using the default query options.
extension QueryableJobQueueDriver {
/// Iterates over each job of the specified type using the default `JobQueueQuery` options,
/// performing the operation for each.
///
/// - Parameters:
/// - type: The type of the parameters for the job.
/// - perform: A closure called for each job.
public func withEachJob<Parameters: Sendable>(
ofType type: Parameters.Type,
perform: @Sendable @escaping (JobID, JobInstanceData<Parameters>) async throws -> Void
) async throws {
try await withEachJob(
ofType: Parameters.self,
matching: JobQueueQuery<Parameters>()
) { id, job in
try await perform(id, job)
}
}
}

/// Represents a set of options for filtering and querying jobs in the job queue.
public struct JobQueueQuery<Parameters: Sendable>: Sendable {
/// Whether to include cancelled jobs in the query result. Defaults to `true`.
public var includeCancelledJobs = true
/// Whether to include failed jobs in the query result. Defaults to `true`.
public var includeFailedJobs = true
/// Whether to include completed jobs in the query result. Defaults to `true`.
public var includeCompletedJobs = true
/// Whether to include delayed jobs in the query result. Defaults to `true`.
public var includeDelayedJobs = true

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot to include pending jobs (ie queued but not executed). I'd probably replace delayed with pending.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also filter on queuedAt and a couple other properties. Not sure what we all should support

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of these filters could be quite hard for the valkey solution. It is not a bunch of database tables. The Postgres solution will be happy to supply anything I'm sure, given it two tables (the queue and the jobs)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Can Valkey emit a historic record at all? And does it make sense for Valkey to implement/support this new feature?
  • If so; What alternations can we make to accomodate valkey(-like) usecases?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does have a historic record. The problem with valkey is that some of the more complex queries you can do with postgres are not so easily available. I don't think we even include queuedAt in the valkey data. We could do that, I just added a hash map for each job which can hold arbitrary data.

If the valkey installation include Valkey search you can search hash maps

/// Creates a new `JobQueueQuery` with the default options, including all jobs.
public init() {}
}
Loading