-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added AsyncScheduledJob and cleaned up AsyncJob. (#104)
* Added AsyncScheduledJob and cleaned up AsyncJob. * Implement Job protocol. * Add error stub Co-authored-by: jdmcd <jimmy@jdmcd.io>
- Loading branch information
1 parent
09f39d5
commit bdb4171
Showing
3 changed files
with
93 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import Vapor | ||
import NIOCore | ||
import Foundation | ||
|
||
#if compiler(>=5.5) && canImport(_Concurrency) | ||
/// Describes a job that can be scheduled and repeated | ||
@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *) | ||
protocol AsyncScheduledJob: ScheduledJob { | ||
var name: String { get } | ||
|
||
/// The method called when the job is run | ||
/// - Parameter context: A `JobContext` that can be used | ||
func run(context: QueueContext) async throws | ||
} | ||
|
||
@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *) | ||
extension AsyncScheduledJob { | ||
public var name: String { "\(Self.self)" } | ||
|
||
func run(context: QueueContext) -> EventLoopFuture<Void> { | ||
let promise = context.eventLoop.makePromise(of: Void.self) | ||
promise.completeWithTask { | ||
try await self.run(context: context) | ||
} | ||
return promise.futureResult | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#if compiler(>=5.5) && canImport(_Concurrency) | ||
import Queues | ||
import Vapor | ||
import XCTVapor | ||
import XCTQueues | ||
@testable import Vapor | ||
import NIOConcurrencyHelpers | ||
|
||
@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *) | ||
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) | ||
app.queues.add(MyAsyncJob(promise: promise)) | ||
|
||
app.get("foo") { req in | ||
req.queue.dispatch(MyAsyncJob.self, .init(foo: "bar")) | ||
.map { _ in "done" } | ||
} | ||
|
||
try app.testable().test(.GET, "foo") { res in | ||
XCTAssertEqual(res.status, .ok) | ||
XCTAssertEqual(res.body.string, "done") | ||
} | ||
|
||
XCTAssertEqual(app.queues.test.queue.count, 1) | ||
XCTAssertEqual(app.queues.test.jobs.count, 1) | ||
let job = app.queues.test.first(MyAsyncJob.self) | ||
XCTAssert(app.queues.test.contains(MyAsyncJob.self)) | ||
XCTAssertNotNil(job) | ||
XCTAssertEqual(job!.foo, "bar") | ||
|
||
try app.queues.queue.worker.run().wait() | ||
XCTAssertEqual(app.queues.test.queue.count, 0) | ||
XCTAssertEqual(app.queues.test.jobs.count, 0) | ||
|
||
try XCTAssertNoThrow(promise.futureResult.wait()) | ||
} | ||
} | ||
|
||
@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *) | ||
struct MyAsyncJob: AsyncJob { | ||
let promise: EventLoopPromise<Void> | ||
|
||
struct Data: Codable { | ||
var foo: String | ||
} | ||
|
||
func dequeue(_ context: QueueContext, _ payload: Data) async throws { | ||
promise.succeed(()) | ||
return | ||
} | ||
} | ||
#endif |