Skip to content

Commit

Permalink
Use hummingbird-jobs repo (#17)
Browse files Browse the repository at this point in the history
* Use hummingbird-jobs repo

* Fix tests

* Update PostgresQueue docs

* swift-jobs

* HummingbirdJobsPostgres -> JobsPostgres

* Use HB 2.0.0 RC, Jobs 1.0.0 beta
  • Loading branch information
adam-fowler committed Jul 1, 2024
1 parent 34ffe8d commit 658b21b
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 23 deletions.
11 changes: 6 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ let package = Package(
platforms: [.macOS(.v14), .iOS(.v17), .tvOS(.v17)],
products: [
.library(name: "HummingbirdPostgres", targets: ["HummingbirdPostgres"]),
.library(name: "HummingbirdJobsPostgres", targets: ["HummingbirdJobsPostgres"]),
.library(name: "JobsPostgres", targets: ["JobsPostgres"]),
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-beta.7"),
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0-rc.1"),
.package(url: "https://github.com/hummingbird-project/swift-jobs.git", from: "1.0.0-beta.1"),
.package(url: "https://github.com/vapor/postgres-nio", from: "1.21.0"),
],
targets: [
Expand All @@ -23,18 +24,18 @@ let package = Package(
]
),
.target(
name: "HummingbirdJobsPostgres",
name: "JobsPostgres",
dependencies: [
"HummingbirdPostgres",
.product(name: "HummingbirdJobs", package: "hummingbird"),
.product(name: "Jobs", package: "swift-jobs"),
.product(name: "PostgresNIO", package: "postgres-nio"),
]
),
.testTarget(
name: "HummingbirdPostgresTests",
dependencies: [
"HummingbirdPostgres",
"HummingbirdJobsPostgres",
"JobsPostgres",
.product(name: "HummingbirdTesting", package: "hummingbird"),
]
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
//===----------------------------------------------------------------------===//

import Foundation
import HummingbirdJobs
import HummingbirdPostgres
import Jobs
import Logging
import NIOConcurrencyHelpers
import NIOCore
Expand All @@ -38,11 +38,11 @@ import PostgresNIO
/// logger: logger
/// )
/// var app = Application(...)
/// app.runBeforeServerStart {
/// app.beforeServerStarts {
/// try await migrations.apply(client: postgresClient, logger: logger, dryRun: applyMigrations)
/// }
/// ```
public final class PostgresQueue: JobQueueDriver {
public final class PostgresJobQueue: JobQueueDriver {
public typealias JobID = UUID

/// what to do with failed/processing jobs from last time queue was handled
Expand Down Expand Up @@ -279,11 +279,11 @@ public final class PostgresQueue: JobQueueDriver {
}

/// extend PostgresJobQueue to conform to AsyncSequence
extension PostgresQueue {
extension PostgresJobQueue {
public struct AsyncIterator: AsyncIteratorProtocol {
public typealias Element = QueuedJob<JobID>

let queue: PostgresQueue
let queue: PostgresJobQueue

public func next() async throws -> Element? {
while true {
Expand All @@ -304,13 +304,13 @@ extension PostgresQueue {
}
}

extension JobQueueDriver where Self == PostgresQueue {
extension JobQueueDriver where Self == PostgresJobQueue {
/// Return Postgres driver for Job Queue
/// - Parameters:
/// - client: Postgres client
/// - configuration: Queue configuration
/// - logger: Logger used by queue
public static func postgres(client: PostgresClient, migrations: PostgresMigrations, configuration: PostgresQueue.Configuration = .init(), logger: Logger) async -> Self {
public static func postgres(client: PostgresClient, migrations: PostgresMigrations, configuration: PostgresJobQueue.Configuration = .init(), logger: Logger) async -> Self {
await Self(client: client, migrations: migrations, configuration: configuration, logger: logger)
}
}
18 changes: 9 additions & 9 deletions Tests/HummingbirdPostgresTests/JobsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

import Atomics
import Hummingbird
import HummingbirdJobs
@testable import HummingbirdJobsPostgres
@testable import HummingbirdPostgres
import HummingbirdTesting
import Jobs
@testable import JobsPostgres
import NIOConcurrencyHelpers
import PostgresNIO
import ServiceLifecycle
Expand All @@ -41,7 +41,7 @@ final class JobsTests: XCTestCase {

static let env = Environment()

func createJobQueue(numWorkers: Int, configuration: PostgresQueue.Configuration, function: String = #function) async throws -> JobQueue<PostgresQueue> {
func createJobQueue(numWorkers: Int, configuration: PostgresJobQueue.Configuration, function: String = #function) async throws -> JobQueue<PostgresJobQueue> {
let logger = {
var logger = Logger(label: function)
logger.logLevel = .debug
Expand All @@ -53,7 +53,7 @@ final class JobsTests: XCTestCase {
)
let postgresMigrations = PostgresMigrations()
return await JobQueue(
PostgresQueue(
.postgres(
client: postgresClient,
migrations: postgresMigrations,
configuration: configuration,
Expand All @@ -69,9 +69,9 @@ final class JobsTests: XCTestCase {
/// Creates test client, runs test function abd ensures everything is
/// shutdown correctly
@discardableResult public func testJobQueue<T>(
jobQueue: JobQueue<PostgresQueue>,
jobQueue: JobQueue<PostgresJobQueue>,
revertMigrations: Bool = false,
test: (JobQueue<PostgresQueue>) async throws -> T
test: (JobQueue<PostgresJobQueue>) async throws -> T
) async throws -> T {
do {
return try await withThrowingTaskGroup(of: Void.self) { group in
Expand Down Expand Up @@ -117,10 +117,10 @@ final class JobsTests: XCTestCase {
/// shutdown correctly
@discardableResult public func testJobQueue<T>(
numWorkers: Int,
configuration: PostgresQueue.Configuration = .init(failedJobsInitialization: .remove, processingJobsInitialization: .remove),
configuration: PostgresJobQueue.Configuration = .init(failedJobsInitialization: .remove, processingJobsInitialization: .remove),
revertMigrations: Bool = true,
function: String = #function,
test: (JobQueue<PostgresQueue>) async throws -> T
test: (JobQueue<PostgresJobQueue>) async throws -> T
) async throws -> T {
let jobQueue = try await self.createJobQueue(numWorkers: numWorkers, configuration: configuration, function: function)
return try await self.testJobQueue(jobQueue: jobQueue, revertMigrations: revertMigrations, test: test)
Expand Down Expand Up @@ -337,7 +337,7 @@ final class JobsTests: XCTestCase {
)
let postgresMigrations2 = PostgresMigrations()
let jobQueue2 = await JobQueue(
PostgresQueue(
.postgres(
client: postgresClient,
migrations: postgresMigrations2,
configuration: .init(failedJobsInitialization: .remove, processingJobsInitialization: .remove),
Expand Down
2 changes: 1 addition & 1 deletion Tests/HummingbirdPostgresTests/PersistTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ final class PersistTests: XCTestCase {
updateRouter(router, persist)
var app = Application(responder: router.buildResponder())
app.addServices(postgresClient, persist)
app.runBeforeServerStart {
app.beforeServerStarts {
try await postgresMigrations.apply(client: postgresClient, groups: [.persist], logger: logger, dryRun: false)
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/HummingbirdPostgresTests/TestUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PostgresNIO
import ServiceLifecycle

func getPostgresConfiguration() async throws -> PostgresClient.Configuration {
let env = try await Environment.shared.merging(with: .dotEnv())
let env = try await Environment().merging(with: .dotEnv())
return .init(
host: env.get("POSTGRES_HOSTNAME") ?? "localhost",
port: env.get("POSTGRES_PORT", as: Int.self) ?? 5432,
Expand Down

0 comments on commit 658b21b

Please sign in to comment.