Skip to content

Commit

Permalink
Try workaround for Sqlite issue with concurrent requests
Browse files Browse the repository at this point in the history
  • Loading branch information
m-barthelemy committed Apr 6, 2020
1 parent 5470158 commit 6141281
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 45 deletions.
4 changes: 1 addition & 3 deletions Sources/QueuesFluentDriver/PopQueries/MySQLPopQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import SQLKit
import Fluent
import Queues

class MySQLPop : PopQueryProtocol {
final class MySQLPop : PopQueryProtocol {
func pop(db: Database, select: SQLExpression) -> EventLoopFuture<UUID?> {
db.transaction { transaction in
let database = transaction as! SQLDatabase
Expand All @@ -12,7 +12,6 @@ class MySQLPop : PopQueryProtocol {
return database.execute(sql: select) { (row) -> Void in
print("••• columns: \(row.allColumns)")
id = try? row.decode(column: "\(FluentQueue.model.$id.key)", as: UUID.self)
print("••• returned id \(id)")
}
.flatMap {
if (id != nil) {
Expand All @@ -27,7 +26,6 @@ class MySQLPop : PopQueryProtocol {
}
return database.eventLoop.makeSucceededFuture(nil)
}


}
}
Expand Down
30 changes: 28 additions & 2 deletions Sources/QueuesFluentDriver/PopQueries/SqlitePopQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,32 @@ import SQLKit
import Fluent
import Queues

final class SqlitePop : MySQLPop {

final class SqlitePop : PopQueryProtocol {
func pop(db: Database, select: SQLExpression) -> EventLoopFuture<UUID?> {
let database = db as! SQLDatabase
return database.raw(SQLQueryString("BEGIN IMMEDIATE")).run().flatMap { void in
var id: UUID?
return database.execute(sql: select) { (row) -> Void in
print("••• columns: \(row.allColumns)")
id = try? row.decode(column: "\(FluentQueue.model.$id.key)", as: UUID.self)
}
.flatMap {
if (id != nil) {
let updateQuery = database
.update(JobModel.schema)
.set(SQLColumn.init("\(FluentQueue.model.$state.key)"), to: SQLBind.init(JobState.processing))
.set(SQLColumn.init("\(FluentQueue.model.$updatedAt.path.first!)"), to: SQLBind.init(Date()))
.where(SQLColumn.init("\(FluentQueue.model.$id.key)"), .equal, SQLBind.init(id!))
.query
return database.execute(sql: updateQuery) { (row) in }
.flatMap {
return database.raw(SQLQueryString("COMMIT")).run().map {
return id
}
}
}
return database.eventLoop.makeSucceededFuture(nil)
}
}
}
}
40 changes: 0 additions & 40 deletions Sources/QueuesFluentDriver/SQLDatabase+query.swift

This file was deleted.

0 comments on commit 6141281

Please sign in to comment.