Skip to content

Commit

Permalink
Improve startup errors handling and reporting; update README to swift…
Browse files Browse the repository at this point in the history
…-tools-version:5.2
  • Loading branch information
m-barthelemy committed May 19, 2020
1 parent a929a1a commit 56c4cea
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 30 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Add it to the `Package.swift` of your Vapor4 project:

```swift

// swift-tools-version:5.1
// swift-tools-version:5.2
import PackageDescription

let package = Package(
Expand All @@ -40,13 +40,13 @@ let package = Package(
...
dependencies: [
...
.package(url: "https://github.com/m-barthelemy/vapor-queues-fluent-driver.git", from: "0.3.5"),
.package(url: "https://github.com/m-barthelemy/vapor-queues-fluent-driver.git", from: "0.3.6"),
...
],
targets: [
.target(name: "App", dependencies: [
...
"QueuesFluentDriver",
.product(name: "QueuesFluentDriver", package: "QueuesFluentDriver"),
...
]),
...
Expand All @@ -57,19 +57,19 @@ let package = Package(

 

This package needs a table, named `jobs`, to store the Vapor Queues jobs. Add `JobModelMigrate` to your migrations:
This package needs a table, named `_jobs` by default, to store the Vapor Queues jobs. Add `JobModelMigrate` to your migrations:
```swift
// Ensure the table for storing jobs is created
app.migrations.add(JobModelMigrate())
```

 

Finally, load the `QueuesFluentDriver` driver:
```swift
app.queues.use(.fluent())
```

Make sure you call `app.databases.use(...)` **before** calling `app.queues.use(.fluent())`!

 

Expand Down
27 changes: 9 additions & 18 deletions Sources/QueuesFluentDriver/FluentQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ import SQLKit

public struct FluentQueue {
public let context: QueueContext
let db: Database?
let dbType: QueuesFluentDbType?
let db: Database
let dbType: QueuesFluentDbType
let useSoftDeletes: Bool
}

extension FluentQueue: Queue {
public func get(_ id: JobIdentifier) -> EventLoopFuture<JobData> {
guard let db = db else {
return self.context.eventLoop.makeFailedFuture(QueuesFluentError.databaseNotFound)
}
return db.query(JobModel.self)
.filter(\.$jobId == id.string)
.filter(\.$state != .pending)
Expand All @@ -26,9 +23,6 @@ extension FluentQueue: Queue {
}

public func set(_ id: JobIdentifier, to jobStorage: JobData) -> EventLoopFuture<Void> {
guard let db = db else {
return self.context.eventLoop.makeFailedFuture(QueuesFluentError.databaseNotFound)
}
//let data = try! JSONEncoder().encode(jobStorage)
do {
let jobModel = try JobModel(jobId: id.string, queue: queueName.string, data: jobStorage)
Expand All @@ -41,9 +35,6 @@ extension FluentQueue: Queue {
}

public func clear(_ id: JobIdentifier) -> EventLoopFuture<Void> {
guard let db = db else {
return self.context.eventLoop.makeFailedFuture(QueuesFluentError.databaseNotFound)
}
// This does the equivalent of a Fluent Softdelete but sets the `state` to `completed`
return db.query(JobModel.self)
.filter(\.$jobId == id.string)
Expand All @@ -54,15 +45,15 @@ extension FluentQueue: Queue {
if self.useSoftDeletes {
job.state = .completed
job.deletedAt = Date()
return job.update(on: db)
return job.update(on: self.db)
} else {
return job.delete(force: true, on: db)
return job.delete(force: true, on: self.db)
}
}
}

public func push(_ id: JobIdentifier) -> EventLoopFuture<Void> {
guard let db = db, let sqlDb = db as? SQLDatabase else {
guard let sqlDb = db as? SQLDatabase else {
return self.context.eventLoop.makeFailedFuture(QueuesFluentError.databaseNotFound)
}
return sqlDb
Expand All @@ -74,7 +65,7 @@ extension FluentQueue: Queue {

/// Currently selects the oldest job pending execution
public func pop() -> EventLoopFuture<JobIdentifier?> {
guard let db = db, let sqlDb = db as? SQLDatabase else {
guard let sqlDb = db as? SQLDatabase else {
return self.context.eventLoop.makeFailedFuture(QueuesFluentError.databaseNotFound)
}

Expand All @@ -98,7 +89,7 @@ extension FluentQueue: Queue {
popProvider = MySQLPop()
case .sqlite:
popProvider = SqlitePop()
case .none:
default:
return db.context.eventLoop.makeFailedFuture(QueuesFluentError.databaseNotFound)
}
return popProvider.pop(db: db, select: selectQuery.query).optionalMap { id in
Expand All @@ -108,7 +99,7 @@ extension FluentQueue: Queue {

/// /!\ This is a non standard extension.
public func list(queue: String? = nil, state: QueuesFluentJobState = .pending) -> EventLoopFuture<[JobData]> {
guard let db = db, let sqlDb = db as? SQLDatabase else {
guard let sqlDb = db as? SQLDatabase else {
return self.context.eventLoop.makeFailedFuture(QueuesFluentError.databaseNotFound)
}
var query = sqlDb
Expand All @@ -132,7 +123,7 @@ extension FluentQueue: Queue {
jobs.append(jobData)
}
catch {
return db.eventLoop.makeFailedFuture(QueuesFluentError.jobDataDecodingError("\(error)"))
return self.db.eventLoop.makeFailedFuture(QueuesFluentError.jobDataDecodingError("\(error)"))
.whenSuccess {$0}
}
}
Expand Down
19 changes: 13 additions & 6 deletions Sources/QueuesFluentDriver/FluentQueuesDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ public enum QueuesFluentDbType: String {

public struct FluentQueuesDriver {
let databaseId: DatabaseID?
let dbType: QueuesFluentDbType
let useSoftDeletes: Bool
let eventLoopGroup: EventLoopGroup

init(on databaseId: DatabaseID? = nil, useSoftDeletes: Bool, on: EventLoopGroup) {
self.databaseId = databaseId
self.useSoftDeletes = useSoftDeletes
self.dbType = .postgresql
self.eventLoopGroup = on
}
}
Expand All @@ -30,13 +28,22 @@ extension FluentQueuesDriver: QueuesDriver {
.database(databaseId, logger: context.logger, on: context.eventLoop)

// How do we report that something goes wrong here? Since makeQueue cannot throw.
let dialect = (db as? SQLDatabase)?.dialect.name ?? "unknown"
let dbType = QueuesFluentDbType(rawValue: dialect) ?? .none
let dialect = (db as? SQLDatabase)?.dialect.name
if db == nil || dialect == nil {
context.logger.error(
"\(Self.self): Database misconfigured or unsupported."
)
}

let dbType = QueuesFluentDbType(rawValue: dialect!)
if dbType == nil {
context.logger.error("\(Self.self): Unsupported Database type '\(dialect!)'")
}

return FluentQueue(
context: context,
db: db,
dbType: dbType,
db: db!,
dbType: dbType!,
useSoftDeletes: self.useSoftDeletes
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import Foundation
import SQLKit
import Fluent

Expand Down

0 comments on commit 56c4cea

Please sign in to comment.