From f1adaf4c925eb7074a4acf363172c1fa6ec888c8 Mon Sep 17 00:00:00 2001 From: Gwynne Raskind Date: Wed, 22 Mar 2023 02:17:49 -0500 Subject: [PATCH] Add missing platform specifiers (#121) * Add missing platform specifiers * Fix imports * Remove outdated concurrency conditionals, replace deprecated NIOAtomic with ManagedAtomic --- Package.swift | 5 ++++- Sources/Queues/Application+Queues.swift | 1 + Sources/Queues/AsyncJob.swift | 5 ----- Sources/Queues/AsyncJobEventDelegate.swift | 4 ---- Sources/Queues/AsyncScheduledJob.swift | 4 ---- Sources/Queues/Job.swift | 3 ++- Sources/Queues/Queue+Async.swift | 4 +--- Sources/Queues/QueueContext.swift | 2 ++ Sources/Queues/QueuesCommand.swift | 18 +++++++++++++----- Sources/Queues/QueuesEventLoopPreference.swift | 2 +- Sources/Queues/Request+Queues.swift | 2 +- Sources/Queues/ScheduleBuilder.swift | 2 +- Sources/Queues/ScheduledJob.swift | 2 ++ Sources/XCTQueues/TestQueueDriver.swift | 1 + Tests/QueuesTests/AsyncQueueTests.swift | 9 ++++----- Tests/QueuesTests/QueueTests.swift | 14 +++++++++----- Tests/QueuesTests/ScheduleBuilderTests.swift | 3 +-- 17 files changed, 43 insertions(+), 38 deletions(-) diff --git a/Package.swift b/Package.swift index 53bca7f..9170e57 100644 --- a/Package.swift +++ b/Package.swift @@ -4,7 +4,10 @@ import PackageDescription let package = Package( name: "queues", platforms: [ - .macOS(.v10_15) + .macOS(.v10_15), + .iOS(.v13), + .tvOS(.v13), + .watchOS(.v6), ], products: [ .library(name: "Queues", targets: ["Queues"]), diff --git a/Sources/Queues/Application+Queues.swift b/Sources/Queues/Application+Queues.swift index 9e64630..b217eae 100644 --- a/Sources/Queues/Application+Queues.swift +++ b/Sources/Queues/Application+Queues.swift @@ -1,4 +1,5 @@ import Foundation +import Logging import Vapor import NIO diff --git a/Sources/Queues/AsyncJob.swift b/Sources/Queues/AsyncJob.swift index c8651f6..197e77e 100644 --- a/Sources/Queues/AsyncJob.swift +++ b/Sources/Queues/AsyncJob.swift @@ -2,9 +2,7 @@ import Vapor import NIOCore import Foundation -#if compiler(>=5.5) && canImport(_Concurrency) /// A task that can be queued for future execution. -@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) public protocol AsyncJob: Job { /// The data associated with a job associatedtype Payload @@ -41,7 +39,6 @@ public protocol AsyncJob: Job { static func parsePayload(_ bytes: [UInt8]) throws -> Payload } -@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) extension AsyncJob where Payload: Codable { /// Serialize a payload into Data @@ -57,7 +54,6 @@ extension AsyncJob where Payload: Codable { } } -@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) extension AsyncJob { /// The jobName of the Job public static var name: String { @@ -93,4 +89,3 @@ extension AsyncJob { return } } -#endif diff --git a/Sources/Queues/AsyncJobEventDelegate.swift b/Sources/Queues/AsyncJobEventDelegate.swift index bb6dc2d..d76fad4 100644 --- a/Sources/Queues/AsyncJobEventDelegate.swift +++ b/Sources/Queues/AsyncJobEventDelegate.swift @@ -1,8 +1,6 @@ -#if compiler(>=5.5) && canImport(_Concurrency) import NIOCore /// Represents an object that can receive notifications about job statuses -@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) public protocol AsyncJobEventDelegate: JobEventDelegate { /// Called when the job is first dispatched /// - Parameters: @@ -26,7 +24,6 @@ public protocol AsyncJobEventDelegate: JobEventDelegate { func error(jobId: String, error: Error) async throws } -@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) extension AsyncJobEventDelegate { public func dispatched(job: JobEventData) async throws { } public func didDequeue(jobId: String) async throws { } @@ -57,4 +54,3 @@ extension AsyncJobEventDelegate { } } } -#endif diff --git a/Sources/Queues/AsyncScheduledJob.swift b/Sources/Queues/AsyncScheduledJob.swift index 8845c60..14af89f 100644 --- a/Sources/Queues/AsyncScheduledJob.swift +++ b/Sources/Queues/AsyncScheduledJob.swift @@ -2,9 +2,7 @@ import Vapor import NIOCore import Foundation -#if compiler(>=5.5) && canImport(_Concurrency) /// Describes a job that can be scheduled and repeated -@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) public protocol AsyncScheduledJob: ScheduledJob { var name: String { get } @@ -13,7 +11,6 @@ public protocol AsyncScheduledJob: ScheduledJob { func run(context: QueueContext) async throws } -@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) extension AsyncScheduledJob { public var name: String { "\(Self.self)" } @@ -25,4 +22,3 @@ extension AsyncScheduledJob { return promise.futureResult } } -#endif diff --git a/Sources/Queues/Job.swift b/Sources/Queues/Job.swift index 42c81ff..80dfc68 100644 --- a/Sources/Queues/Job.swift +++ b/Sources/Queues/Job.swift @@ -1,5 +1,6 @@ -import NIO +import NIOCore import Foundation +import Logging import Vapor /// A task that can be queued for future execution. diff --git a/Sources/Queues/Queue+Async.swift b/Sources/Queues/Queue+Async.swift index f600f30..71f1775 100644 --- a/Sources/Queues/Queue+Async.swift +++ b/Sources/Queues/Queue+Async.swift @@ -1,7 +1,7 @@ +import Foundation import Vapor import NIOCore -#if compiler(>=5.5) && canImport(_Concurrency) extension Queue { /// Dispatch a job into the queue for processing /// - Parameters: @@ -9,7 +9,6 @@ extension Queue { /// - payload: The payload data to be dispatched /// - maxRetryCount: Number of times to retry this job on failure /// - delayUntil: Delay the processing of this job until a certain date - @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) public func dispatch( _ job: J.Type, _ payload: J.Payload, @@ -20,4 +19,3 @@ extension Queue { try await self.dispatch(job, payload, maxRetryCount: maxRetryCount, delayUntil: delayUntil, id: id).get() } } -#endif diff --git a/Sources/Queues/QueueContext.swift b/Sources/Queues/QueueContext.swift index 5ba7bba..6ef924e 100644 --- a/Sources/Queues/QueueContext.swift +++ b/Sources/Queues/QueueContext.swift @@ -1,3 +1,5 @@ +import Logging +import NIOCore import Vapor /// The context for a queue. diff --git a/Sources/Queues/QueuesCommand.swift b/Sources/Queues/QueuesCommand.swift index ba62c21..e4af895 100644 --- a/Sources/Queues/QueuesCommand.swift +++ b/Sources/Queues/QueuesCommand.swift @@ -1,6 +1,14 @@ +import ConsoleKit +import Dispatch import Vapor import NIOConcurrencyHelpers import NIOCore +import Atomics +#if os(Linux) +import Glibc +#else +import Darwin.C +#endif /// The command to start the Queue job public final class QueuesCommand: Command { @@ -30,7 +38,7 @@ public final class QueuesCommand: Command { private var signalSources: [DispatchSourceSignal] private var didShutdown: Bool - private let isShuttingDown: NIOAtomic + private let isShuttingDown: ManagedAtomic private var eventLoopGroup: EventLoopGroup { self.application.eventLoopGroup @@ -41,7 +49,7 @@ public final class QueuesCommand: Command { self.application = application self.jobTasks = [] self.scheduledTasks = [:] - self.isShuttingDown = .makeAtomic(value: false) + self.isShuttingDown = .init(false) self.signalSources = [] self.didShutdown = false self.lock = .init() @@ -114,7 +122,7 @@ public final class QueuesCommand: Command { return worker.run().map { self.application.logger.trace("Worker ran the task successfully") //Check if shutting down - if self.isShuttingDown.load() { + if self.isShuttingDown.load(ordering: .relaxed) { self.application.logger.trace("Shutting down, cancelling the task") task.cancel() } @@ -145,7 +153,7 @@ public final class QueuesCommand: Command { } private func schedule(_ job: AnyScheduledJob) { - if self.isShuttingDown.load() { + if self.isShuttingDown.load(ordering: .relaxed) { self.application.logger.trace("Application is shutting down, cancelling scheduling \(job.job.name)") return } @@ -180,7 +188,7 @@ public final class QueuesCommand: Command { self.lock.lock() defer { self.lock.unlock() } - self.isShuttingDown.store(true) + self.isShuttingDown.store(true, ordering: .relaxed) self.didShutdown = true // stop running in case shutting downf rom signal diff --git a/Sources/Queues/QueuesEventLoopPreference.swift b/Sources/Queues/QueuesEventLoopPreference.swift index e5ea97e..aa9e7fe 100644 --- a/Sources/Queues/QueuesEventLoopPreference.swift +++ b/Sources/Queues/QueuesEventLoopPreference.swift @@ -6,7 +6,7 @@ // import Foundation -import NIO +import NIOCore /// Determines which event loop the jobs worker uses while executing jobs. public enum QueuesEventLoopPreference { diff --git a/Sources/Queues/Request+Queues.swift b/Sources/Queues/Request+Queues.swift index 2bad0d2..6a1e6f4 100644 --- a/Sources/Queues/Request+Queues.swift +++ b/Sources/Queues/Request+Queues.swift @@ -1,6 +1,6 @@ import Foundation import Vapor -import NIO +import NIOCore extension Request { /// Returns the default job `Queue` diff --git a/Sources/Queues/ScheduleBuilder.swift b/Sources/Queues/ScheduleBuilder.swift index 7435ff4..9a6a2b4 100644 --- a/Sources/Queues/ScheduleBuilder.swift +++ b/Sources/Queues/ScheduleBuilder.swift @@ -24,7 +24,7 @@ public final class ScheduleBuilder { case first case last case exact(Int) - + public init(integerLiteral value: Int) { self = .exact(value) } diff --git a/Sources/Queues/ScheduledJob.swift b/Sources/Queues/ScheduledJob.swift index 707dc30..b1be2ce 100644 --- a/Sources/Queues/ScheduledJob.swift +++ b/Sources/Queues/ScheduledJob.swift @@ -1,4 +1,6 @@ import NIOCore +import Foundation +import Logging /// Describes a job that can be scheduled and repeated public protocol ScheduledJob { diff --git a/Sources/XCTQueues/TestQueueDriver.swift b/Sources/XCTQueues/TestQueueDriver.swift index 8d6135a..a4ed842 100644 --- a/Sources/XCTQueues/TestQueueDriver.swift +++ b/Sources/XCTQueues/TestQueueDriver.swift @@ -1,5 +1,6 @@ import Queues import Vapor +import NIOCore import NIOConcurrencyHelpers extension Application.Queues.Provider { diff --git a/Tests/QueuesTests/AsyncQueueTests.swift b/Tests/QueuesTests/AsyncQueueTests.swift index 3104c70..7c6c732 100644 --- a/Tests/QueuesTests/AsyncQueueTests.swift +++ b/Tests/QueuesTests/AsyncQueueTests.swift @@ -1,19 +1,20 @@ -#if compiler(>=5.5) && canImport(_Concurrency) import Queues +import Foundation import Vapor +import XCTest import XCTVapor import XCTQueues @testable import Vapor +import NIOCore import NIOConcurrencyHelpers -@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) final class AsyncQueueTests: XCTestCase { func testAsyncJob() throws { let app = Application(.testing) defer { app.shutdown() } app.queues.use(.test) - let promise = app.eventLoopGroup.next().makePromise(of: Void.self) + let promise = app.eventLoopGroup.any().makePromise(of: Void.self) app.queues.add(MyAsyncJob(promise: promise)) app.get("foo") { req in @@ -41,7 +42,6 @@ final class AsyncQueueTests: XCTestCase { } } -@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) struct MyAsyncJob: AsyncJob { let promise: EventLoopPromise @@ -54,4 +54,3 @@ struct MyAsyncJob: AsyncJob { return } } -#endif diff --git a/Tests/QueuesTests/QueueTests.swift b/Tests/QueuesTests/QueueTests.swift index a15c39a..562287b 100644 --- a/Tests/QueuesTests/QueueTests.swift +++ b/Tests/QueuesTests/QueueTests.swift @@ -1,9 +1,13 @@ +import Atomics import Queues import Vapor +import Foundation +import XCTest import XCTVapor import XCTQueues -@testable import Vapor +import NIOCore import NIOConcurrencyHelpers +@testable import Vapor final class QueueTests: XCTestCase { func testVaporIntegrationWithInProcessJob() throws { @@ -137,13 +141,13 @@ final class QueueTests: XCTestCase { let app = Application(.testing) defer { app.shutdown() } - XCTAssertEqual(TestingScheduledJob.count.load(), 0) + XCTAssertEqual(TestingScheduledJob.count.load(ordering: .relaxed), 0) app.queues.schedule(TestingScheduledJob()).everySecond() try app.queues.startScheduledJobs() let promise = app.eventLoopGroup.next().makePromise(of: Void.self) app.eventLoopGroup.next().scheduleTask(in: .seconds(5)) { () -> Void in - XCTAssert(TestingScheduledJob.count.load() > 4) + XCTAssert(TestingScheduledJob.count.load(ordering: .relaxed) > 4) promise.succeed(()) } @@ -408,10 +412,10 @@ struct FailingScheduledJob: ScheduledJob { } struct TestingScheduledJob: ScheduledJob { - static var count = NIOAtomic.makeAtomic(value: 0) + static var count = ManagedAtomic(0) func run(context: QueueContext) -> EventLoopFuture { - TestingScheduledJob.count.add(1) + TestingScheduledJob.count.wrappingIncrement(ordering: .relaxed) return context.eventLoop.future() } } diff --git a/Tests/QueuesTests/ScheduleBuilderTests.swift b/Tests/QueuesTests/ScheduleBuilderTests.swift index ab5cac8..acd0f2f 100644 --- a/Tests/QueuesTests/ScheduleBuilderTests.swift +++ b/Tests/QueuesTests/ScheduleBuilderTests.swift @@ -1,3 +1,4 @@ +import Foundation import Queues import XCTest import NIOCore @@ -157,8 +158,6 @@ final class ScheduleBuilderTests: XCTestCase { } - - final class Cleanup: ScheduledJob { func run(context: QueueContext) -> EventLoopFuture { return context.eventLoop.makeSucceededFuture(())