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

Remove "HB" prefix #17

Merged
merged 3 commits into from Mar 12, 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
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -7,7 +7,7 @@ This is the Hummingbird interface to [RediStack library](https://gitlab.com/mord
## Usage

```swift
let app = HBApplication()
let app = Application()
try app.addRedis(configuration: .init(hostname: "localhost", port: 6379))
app.router.get("redis") { request in
request.redis.send(command: "INFO").map {
adam-fowler marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
2 changes: 1 addition & 1 deletion Sources/HummingbirdJobsRedis/Configuration.swift
Expand Up @@ -15,7 +15,7 @@
import NIOCore
@preconcurrency import RediStack

extension HBRedisQueue {
extension RedisQueue {
/// what to do with failed/processing jobs from last time queue was handled
public enum JobInitialization: Sendable {
case doNothing
Expand Down
20 changes: 10 additions & 10 deletions Sources/HummingbirdJobsRedis/RedisJobQueue.swift
Expand Up @@ -23,7 +23,7 @@ import NIOCore
import RediStack

/// Redis implementation of job queue driver
public final class HBRedisQueue: HBJobQueueDriver {
public final class RedisQueue: JobQueueDriver {
public struct JobID: Sendable, CustomStringConvertible {
let id: String

Expand Down Expand Up @@ -67,15 +67,15 @@ public final class HBRedisQueue: HBJobQueueDriver {
}
}

let redisConnectionPool: HBRedisConnectionPoolService
let redisConnectionPool: RedisConnectionPoolService
let configuration: Configuration
let isStopped: ManagedAtomic<Bool>

/// Initialize redis job queue
/// - Parameters:
/// - redisConnectionPoolService: Redis connection pool
/// - configuration: configuration
public init(_ redisConnectionPoolService: HBRedisConnectionPoolService, configuration: Configuration = .init()) {
public init(_ redisConnectionPoolService: RedisConnectionPoolService, configuration: Configuration = .init()) {
self.redisConnectionPool = redisConnectionPoolService
self.configuration = configuration
self.isStopped = .init(false)
Expand Down Expand Up @@ -132,7 +132,7 @@ public final class HBRedisQueue: HBJobQueueDriver {
/// Pop Job off queue and add to pending queue
/// - Parameter eventLoop: eventLoop to do work on
/// - Returns: queued job
func popFirst() async throws -> HBQueuedJob<JobID>? {
func popFirst() async throws -> QueuedJob<JobID>? {
let pool = self.redisConnectionPool.pool
let key = try await pool.rpoplpush(from: self.configuration.queueKey, to: self.configuration.processingQueueKey).get()
guard !key.isNull else {
Expand Down Expand Up @@ -199,11 +199,11 @@ public final class HBRedisQueue: HBJobQueueDriver {
}
}

/// extend HBRedisJobQueue to conform to AsyncSequence
extension HBRedisQueue {
public typealias Element = HBQueuedJob<JobID>
/// extend RedisJobQueue to conform to AsyncSequence
extension RedisQueue {
public typealias Element = QueuedJob<JobID>
public struct AsyncIterator: AsyncIteratorProtocol {
let queue: HBRedisQueue
let queue: RedisQueue

public func next() async throws -> Element? {
while true {
Expand All @@ -224,12 +224,12 @@ extension HBRedisQueue {
}
}

extension HBJobQueueDriver where Self == HBRedisQueue {
extension JobQueueDriver where Self == RedisQueue {
/// Return Redis driver for Job Queue
/// - Parameters:
/// - redisConnectionPoolService: Redis connection pool
/// - configuration: configuration
public static func redis(_ redisConnectionPoolService: HBRedisConnectionPoolService, configuration: HBRedisQueue.Configuration = .init()) -> Self {
public static func redis(_ redisConnectionPoolService: RedisConnectionPoolService, configuration: RedisQueue.Configuration = .init()) -> Self {
.init(redisConnectionPoolService, configuration: configuration)
}
}
Expand Down
26 changes: 26 additions & 0 deletions Sources/HummingbirdRedis/Deprecations.swift
@@ -0,0 +1,26 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// Below is a list of deprecated symbols with the "HB" prefix. These are available
// temporarily to ease transition from the old symbols that included the "HB"
// prefix to the new ones.
//
// This file will be removed before we do a 2.0 release

@_documentation(visibility: internal) @available(*, deprecated, renamed: "RedisConfiguration")
public typealias HBRedisConfiguration = RedisConfiguration
@_documentation(visibility: internal) @available(*, deprecated, renamed: "RedisConnectionPoolService")
public typealias HBRedisConnectionPoolService = RedisConnectionPoolService
@_documentation(visibility: internal) @available(*, deprecated, renamed: "RedisPersistDriver")
public typealias HBRedisPersistDriver = RedisPersistDriver
10 changes: 5 additions & 5 deletions Sources/HummingbirdRedis/Persist+Redis.swift
Expand Up @@ -16,22 +16,22 @@ import Hummingbird
import RediStack

/// Redis driver for persist system for storing persistent cross request key/value pairs
public struct HBRedisPersistDriver: HBPersistDriver {
let redisConnectionPool: HBRedisConnectionPoolService
public struct RedisPersistDriver: PersistDriver {
let redisConnectionPool: RedisConnectionPoolService

public init(redisConnectionPoolService: HBRedisConnectionPoolService) {
public init(redisConnectionPoolService: RedisConnectionPoolService) {
self.redisConnectionPool = redisConnectionPoolService
}

/// create new key with value. If key already exist throw `HBPersistError.duplicate` error
/// create new key with value. If key already exist throw `PersistError.duplicate` error
public func create(key: String, value: some Codable, expires: Duration?) async throws {
let expiration: RedisSetCommandExpiration? = expires.map { .seconds(Int($0.components.seconds)) }
let result = try await self.redisConnectionPool.set(.init(key), toJSON: value, onCondition: .keyDoesNotExist, expiration: expiration).get()
switch result {
case .ok:
return
case .conditionNotMet:
throw HBPersistError.duplicate
throw PersistError.duplicate
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/HummingbirdRedis/RedisConfiguration.swift
Expand Up @@ -20,7 +20,7 @@ import Logging
import NIOCore
import RediStack

public struct HBRedisConfiguration {
public struct RedisConfiguration {
public typealias ValidationError = RedisConnection.Configuration.ValidationError

public var serverAddresses: [SocketAddress]
Expand Down Expand Up @@ -104,7 +104,7 @@ public struct HBRedisConfiguration {

extension RedisConnectionPool.Configuration {
init(
_ config: HBRedisConfiguration,
_ config: RedisConfiguration,
logger: Logger
) {
self.init(
Expand Down
8 changes: 4 additions & 4 deletions Sources/HummingbirdRedis/RedisConnectionPoolService.swift
Expand Up @@ -20,10 +20,10 @@ import RediStack
import ServiceLifecycle

/// Wrapper for RedisConnectionPool that conforms to ServiceLifecycle Service
public struct HBRedisConnectionPoolService: Service, @unchecked Sendable {
public struct RedisConnectionPoolService: Service, @unchecked Sendable {
/// Initialize RedisConnectionPoolService
public init(
_ config: HBRedisConfiguration,
_ config: RedisConfiguration,
eventLoopGroupProvider: EventLoopGroupProvider = .singleton,
logger: Logger
) {
Expand Down Expand Up @@ -66,7 +66,7 @@ public struct HBRedisConnectionPoolService: Service, @unchecked Sendable {
}
}

extension HBRedisConnectionPoolService {
extension RedisConnectionPoolService {
/// A unique identifer to represent this connection.
@inlinable
public var id: UUID { self.pool.id }
Expand Down Expand Up @@ -124,7 +124,7 @@ extension HBRedisConnectionPoolService {
}
}

extension HBRedisConnectionPoolService: RedisClient {
extension RedisConnectionPoolService: RedisClient {
@inlinable
public var eventLoop: NIOCore.EventLoop { self.pool.eventLoop }

Expand Down
54 changes: 27 additions & 27 deletions Tests/HummingbirdJobsRedisTests/RedisJobsTests.swift
Expand Up @@ -40,7 +40,7 @@ final class HummingbirdRedisJobsTests: XCTestCase {
#endif
}

static let env = HBEnvironment()
static let env = Environment()
static let redisHostname = env.get("REDIS_HOSTNAME") ?? "localhost"

/// Helper function for test a server
Expand All @@ -49,16 +49,16 @@ final class HummingbirdRedisJobsTests: XCTestCase {
/// shutdown correctly
@discardableResult public func testJobQueue<T>(
numWorkers: Int,
failedJobsInitialization: HBRedisQueue.JobInitialization = .remove,
test: (HBJobQueue<HBRedisQueue>) async throws -> T
failedJobsInitialization: RedisQueue.JobInitialization = .remove,
test: (JobQueue<RedisQueue>) async throws -> T
) async throws -> T {
var logger = Logger(label: "RedisJobsTests")
logger.logLevel = .debug
let redisService = try HBRedisConnectionPoolService(
let redisService = try RedisConnectionPoolService(
.init(hostname: Self.redisHostname, port: 6379),
logger: logger
)
let jobQueue = HBJobQueue(
let jobQueue = JobQueue(
.redis(
redisService,
configuration: .init(
Expand Down Expand Up @@ -90,9 +90,9 @@ final class HummingbirdRedisJobsTests: XCTestCase {

func testBasic() async throws {
let expectation = XCTestExpectation(description: "TestJob.execute was called", expectedFulfillmentCount: 10)
let jobIdentifer = HBJobIdentifier<Int>(#function)
let jobIdentifer = JobIdentifier<Int>(#function)
try await self.testJobQueue(numWorkers: 1) { jobQueue in
jobQueue.registerJob(jobIdentifer) { parameters, context in
jobQueue.registerJob(id: jobIdentifer) { parameters, context in
context.logger.info("Parameters=\(parameters)")
try await Task.sleep(for: .milliseconds(Int.random(in: 10..<50)))
expectation.fulfill()
Expand All @@ -113,13 +113,13 @@ final class HummingbirdRedisJobsTests: XCTestCase {
}

func testMultipleWorkers() async throws {
let jobIdentifer = HBJobIdentifier<Int>(#function)
let jobIdentifer = JobIdentifier<Int>(#function)
let runningJobCounter = ManagedAtomic(0)
let maxRunningJobCounter = ManagedAtomic(0)
let expectation = XCTestExpectation(description: "TestJob.execute was called", expectedFulfillmentCount: 10)

try await self.testJobQueue(numWorkers: 4) { jobQueue in
jobQueue.registerJob(jobIdentifer) { parameters, context in
jobQueue.registerJob(id: jobIdentifer) { parameters, context in
let runningJobs = runningJobCounter.wrappingIncrementThenLoad(by: 1, ordering: .relaxed)
if runningJobs > maxRunningJobCounter.load(ordering: .relaxed) {
maxRunningJobCounter.store(runningJobs, ordering: .relaxed)
Expand Down Expand Up @@ -149,11 +149,11 @@ final class HummingbirdRedisJobsTests: XCTestCase {
}

func testErrorRetryCount() async throws {
let jobIdentifer = HBJobIdentifier<Int>(#function)
let jobIdentifer = JobIdentifier<Int>(#function)
let expectation = XCTestExpectation(description: "TestJob.execute was called", expectedFulfillmentCount: 4)
struct FailedError: Error {}
try await self.testJobQueue(numWorkers: 1) { jobQueue in
jobQueue.registerJob(jobIdentifer, maxRetryCount: 3) { _, _ in
jobQueue.registerJob(id: jobIdentifer, maxRetryCount: 3) { _, _ in
expectation.fulfill()
throw FailedError()
}
Expand All @@ -176,9 +176,9 @@ final class HummingbirdRedisJobsTests: XCTestCase {
let message: String
}
let expectation = XCTestExpectation(description: "TestJob.execute was called")
let jobIdentifer = HBJobIdentifier<TestJobParameters>(#function)
let jobIdentifer = JobIdentifier<TestJobParameters>(#function)
try await self.testJobQueue(numWorkers: 1) { jobQueue in
jobQueue.registerJob(jobIdentifer) { parameters, _ in
jobQueue.registerJob(id: jobIdentifer) { parameters, _ in
XCTAssertEqual(parameters.id, 23)
XCTAssertEqual(parameters.message, "Hello!")
expectation.fulfill()
Expand All @@ -191,13 +191,13 @@ final class HummingbirdRedisJobsTests: XCTestCase {

/// Test job is cancelled on shutdown
func testShutdownJob() async throws {
let jobIdentifer = HBJobIdentifier<Int>(#function)
let jobIdentifer = JobIdentifier<Int>(#function)
let expectation = XCTestExpectation(description: "TestJob.execute was called", expectedFulfillmentCount: 1)
var logger = Logger(label: "HummingbirdJobsTests")
logger.logLevel = .trace

try await self.testJobQueue(numWorkers: 4) { jobQueue in
jobQueue.registerJob(jobIdentifer) { _, _ in
jobQueue.registerJob(id: jobIdentifer) { _, _ in
expectation.fulfill()
try await Task.sleep(for: .milliseconds(1000))
}
Expand All @@ -215,12 +215,12 @@ final class HummingbirdRedisJobsTests: XCTestCase {
/// test job fails to decode but queue continues to process
func testFailToDecode() async throws {
let string: NIOLockedValueBox<String> = .init("")
let jobIdentifer1 = HBJobIdentifier<Int>(#function)
let jobIdentifer2 = HBJobIdentifier<String>(#function)
let jobIdentifer1 = JobIdentifier<Int>(#function)
let jobIdentifer2 = JobIdentifier<String>(#function)
let expectation = XCTestExpectation(description: "job was called", expectedFulfillmentCount: 1)

try await self.testJobQueue(numWorkers: 4) { jobQueue in
jobQueue.registerJob(jobIdentifer2) { parameters, _ in
jobQueue.registerJob(id: jobIdentifer2) { parameters, _ in
string.withLockedValue { $0 = parameters }
expectation.fulfill()
}
Expand All @@ -237,12 +237,12 @@ final class HummingbirdRedisJobsTests: XCTestCase {
/// is then rerun on startup of new server
func testRerunAtStartup() async throws {
struct RetryError: Error {}
let jobIdentifer = HBJobIdentifier<Int>(#function)
let jobIdentifer = JobIdentifier<Int>(#function)
let firstTime = ManagedAtomic(true)
let finished = ManagedAtomic(false)
let failedExpectation = XCTestExpectation(description: "TestJob failed", expectedFulfillmentCount: 1)
let succeededExpectation = XCTestExpectation(description: "TestJob2 succeeded", expectedFulfillmentCount: 1)
let job = HBJobDefinition(id: jobIdentifer) { _, _ in
let job = JobDefinition(id: jobIdentifer) { _, _ in
if firstTime.compareExchange(expected: true, desired: false, ordering: .relaxed).original {
failedExpectation.fulfill()
throw RetryError()
Expand Down Expand Up @@ -272,30 +272,30 @@ final class HummingbirdRedisJobsTests: XCTestCase {
}

func testMultipleJobQueueHandlers() async throws {
let jobIdentifer = HBJobIdentifier<Int>(#function)
let jobIdentifer = JobIdentifier<Int>(#function)
let expectation = XCTestExpectation(description: "TestJob.execute was called", expectedFulfillmentCount: 200)
let logger = {
var logger = Logger(label: "HummingbirdJobsTests")
logger.logLevel = .debug
return logger
}()
let job = HBJobDefinition(id: jobIdentifer) { parameters, context in
let job = JobDefinition(id: jobIdentifer) { parameters, context in
context.logger.info("Parameters=\(parameters)")
try await Task.sleep(for: .milliseconds(Int.random(in: 10..<50)))
expectation.fulfill()
}
let redisService = try HBRedisConnectionPoolService(
let redisService = try RedisConnectionPoolService(
.init(hostname: Self.redisHostname, port: 6379),
logger: Logger(label: "Redis")
)
let jobQueue = HBJobQueue(
HBRedisQueue(redisService),
let jobQueue = JobQueue(
RedisQueue(redisService),
numWorkers: 2,
logger: logger
)
jobQueue.registerJob(job)
let jobQueue2 = HBJobQueue(
HBRedisQueue(redisService),
let jobQueue2 = JobQueue(
RedisQueue(redisService),
numWorkers: 2,
logger: logger
)
Expand Down