Skip to content

Commit

Permalink
Merge pull request #2 from vapor-community/4
Browse files Browse the repository at this point in the history
Vapor 4
  • Loading branch information
jdmcd committed Jun 11, 2019
2 parents cf36f85 + 104ea89 commit 4d69716
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 150 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
/.build
/Packages
/*.xcodeproj

.swiftpm
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5.1
151 changes: 62 additions & 89 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,174 +2,147 @@
"object": {
"pins": [
{
"package": "Console",
"repositoryURL": "https://github.com/vapor/console.git",
"package": "async-kit",
"repositoryURL": "https://github.com/vapor/async-kit.git",
"state": {
"branch": null,
"revision": "d6cf07af59ae63cd95c4b5f98cf1f25627750fd1",
"version": "3.1.0"
"revision": "b5742bfbbe2d60f3b77465a0907777261e418f23",
"version": "1.0.0-alpha.1"
}
},
{
"package": "Core",
"repositoryURL": "https://github.com/vapor/core.git",
"package": "console-kit",
"repositoryURL": "https://github.com/vapor/console-kit.git",
"state": {
"branch": null,
"revision": "439d6dcd6c520451ae30d39b2ca9f2aba96c22f4",
"version": "3.7.0"
"revision": "252a8b8e22cc70ebe8a073b241b7275a3b3880d9",
"version": "4.0.0-alpha.1"
}
},
{
"package": "Crypto",
"repositoryURL": "https://github.com/vapor/crypto.git",
"package": "crypto-kit",
"repositoryURL": "https://github.com/vapor/crypto-kit.git",
"state": {
"branch": null,
"revision": "45bb12d13cdec80dbd1cc0685ea002e51ab83439",
"version": "3.3.2"
}
},
{
"package": "DatabaseKit",
"repositoryURL": "https://github.com/vapor/database-kit.git",
"state": {
"branch": null,
"revision": "8f352c8e66dab301ab9bfef912a01ce1361ba1e4",
"version": "1.3.3"
}
},
{
"package": "HTTP",
"repositoryURL": "https://github.com/vapor/http.git",
"state": {
"branch": null,
"revision": "b57005e0de30ba36372ac41bfce1ac12b2bc3272",
"version": "3.1.8"
"revision": "9d5e780ad8f621e14226f0c2824648a9c062d37c",
"version": "4.0.0-alpha.1"
}
},
{
"package": "jobs",
"repositoryURL": "https://github.com/vapor-community/jobs.git",
"state": {
"branch": null,
"revision": "fbe1778181387e7d9fd05263b2e5b4f936e5c23c",
"version": "0.2.4"
"revision": "ae7094c5c835fa236b8449f7a944887571c77dfa",
"version": "1.0.0-alpha.1.0"
}
},
{
"package": "Multipart",
"repositoryURL": "https://github.com/vapor/multipart.git",
"package": "nio-websocket-client",
"repositoryURL": "https://github.com/vapor/nio-websocket-client.git",
"state": {
"branch": null,
"revision": "bd7736c5f28e48ed8b683dcc9df3dcd346064c2b",
"version": "3.0.3"
"revision": "3e515024c4c9eab6adb1ab516be15777a28a233e",
"version": "1.0.0-alpha.1"
}
},
{
"package": "Redis",
"repositoryURL": "https://github.com/vapor/redis.git",
"package": "redis-kit",
"repositoryURL": "https://github.com/vapor/redis-kit.git",
"state": {
"branch": null,
"revision": "ab229d186694e6ea64d9e92e8bab28304f92df3c",
"version": "3.3.0"
"branch": "master",
"revision": "38a206aee6151ffd163bc377d4453b5c41128438",
"version": null
}
},
{
"package": "Routing",
"repositoryURL": "https://github.com/vapor/routing.git",
"package": "routing-kit",
"repositoryURL": "https://github.com/vapor/routing-kit.git",
"state": {
"branch": null,
"revision": "626190ddd2bd9f967743b60ba6adaf90bbd2651c",
"version": "3.0.2"
"revision": "6c7f4b471f9662d05045d82e64e22d5572a16a82",
"version": "4.0.0-alpha.1"
}
},
{
"package": "Service",
"repositoryURL": "https://github.com/vapor/service.git",
"package": "swift-log",
"repositoryURL": "https://github.com/apple/swift-log.git",
"state": {
"branch": null,
"revision": "4907311d7d7f609365982fa302b8b17ffdeb46da",
"version": "1.0.1"
"revision": "f4240bf022a69815241a883c03645444b58ac553",
"version": "1.1.0"
}
},
{
"package": "swift-nio",
"repositoryURL": "https://github.com/apple/swift-nio.git",
"package": "swift-metrics",
"repositoryURL": "https://github.com/apple/swift-metrics.git",
"state": {
"branch": null,
"revision": "87dbd0216c47ea2e7ddb1b545271b716e03b943e",
"version": "1.13.1"
"revision": "8e5110dcd6584ff5acfec40e31263a8d07cba783",
"version": "1.1.0"
}
},
{
"package": "swift-nio-ssl",
"repositoryURL": "https://github.com/apple/swift-nio-ssl.git",
"package": "swift-nio",
"repositoryURL": "https://github.com/apple/swift-nio.git",
"state": {
"branch": null,
"revision": "0f3999f3e3c359cc74480c292644c3419e44a12f",
"version": "1.4.0"
"revision": "ed50f8a41ece8db20afa8d1188fc0960f95263df",
"version": "2.2.0"
}
},
{
"package": "swift-nio-ssl-support",
"repositoryURL": "https://github.com/apple/swift-nio-ssl-support.git",
"package": "swift-nio-extras",
"repositoryURL": "https://github.com/apple/swift-nio-extras.git",
"state": {
"branch": null,
"revision": "c02eec4e0e6d351cd092938cf44195a8e669f555",
"version": "1.0.0"
"revision": "96e8335180ca61c7187efe47058a1c99eadaf265",
"version": "1.1.0"
}
},
{
"package": "swift-nio-zlib-support",
"repositoryURL": "https://github.com/apple/swift-nio-zlib-support.git",
"package": "swift-nio-http-client",
"repositoryURL": "https://github.com/vapor/swift-nio-http-client.git",
"state": {
"branch": null,
"revision": "37760e9a52030bb9011972c5213c3350fa9d41fd",
"version": "1.0.0"
"revision": "93682abeb568b3df47f0744558c4436bb325f860",
"version": "0.0.0"
}
},
{
"package": "TemplateKit",
"repositoryURL": "https://github.com/vapor/template-kit.git",
"package": "swift-nio-http2",
"repositoryURL": "https://github.com/apple/swift-nio-http2.git",
"state": {
"branch": null,
"revision": "aff2d6fc65bfd04579b0201b31a8d6720239c1cf",
"version": "1.1.1"
"revision": "c2638ff60910a0d468ff9a882c8586a827da1a0d",
"version": "1.2.1"
}
},
{
"package": "URLEncodedForm",
"repositoryURL": "https://github.com/vapor/url-encoded-form.git",
"package": "swift-nio-ssl",
"repositoryURL": "https://github.com/apple/swift-nio-ssl.git",
"state": {
"branch": null,
"revision": "932024f363ee5ff59059cf7d67194a1c271d3d0c",
"version": "1.0.5"
"revision": "dc23a24fbe5d59429eacf45a9add82fdf6ab90e6",
"version": "2.1.0"
}
},
{
"package": "Validation",
"repositoryURL": "https://github.com/vapor/validation.git",
"package": "swift-redis-nio-client",
"repositoryURL": "https://gitlab.com/mordil/swift-redis-nio-client.git",
"state": {
"branch": null,
"revision": "4de213cf319b694e4ce19e5339592601d4dd3ff6",
"version": "2.1.1"
"revision": "30733dea02830129064210f32ba41738b9878bbf",
"version": "1.0.0-alpha.2"
}
},
{
"package": "Vapor",
"package": "vapor",
"repositoryURL": "https://github.com/vapor/vapor.git",
"state": {
"branch": null,
"revision": "c86ada59b31c69f08a6abd4f776537cba48d5df6",
"version": "3.3.0"
}
},
{
"package": "WebSocket",
"repositoryURL": "https://github.com/vapor/websocket.git",
"state": {
"branch": null,
"revision": "21eb4773e25a8ff96fe347a31fe106900a69fa6a",
"version": "1.1.1"
"revision": "22bd569b47fbbaa8bb97ce4a5129bba221f9f544",
"version": "4.0.0-alpha.1.1"
}
}
]
Expand Down
8 changes: 4 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:4.2
// swift-tools-version:5.1

import PackageDescription

Expand All @@ -10,13 +10,13 @@ let package = Package(
targets: ["JobsRedisDriver"]),
],
dependencies: [
.package(url: "https://github.com/vapor-community/jobs.git", from: "0.2.0"),
.package(url: "https://github.com/vapor/redis.git", from: "3.0.0")
.package(url: "https://github.com/vapor-community/jobs.git", from: "1.0.0-alpha.1.0"),
.package(url: "https://github.com/vapor/redis-kit.git", .branch("master"))
],
targets: [
.target(
name: "JobsRedisDriver",
dependencies: ["Jobs", "Redis"]),
dependencies: ["Jobs", "RedisKit"]),
.testTarget(
name: "JobsRedisDriverTests",
dependencies: ["JobsRedisDriver"]),
Expand Down
56 changes: 23 additions & 33 deletions Sources/JobsRedisDriver/JobsRedisDriver.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Jobs
import Redis
import RedisKit
import NIO
import Foundation
import Vapor

/// A wrapper that conforms to `JobsPersistenceLayer`
public struct JobsRedisDriver {

/// The `RedisDatabase` to run commands on
let database: RedisDatabase
/// The `RedisClient` to run commands on
let client: RedisClient

/// The `EventLoop` to run jobs on
public let eventLoop: EventLoop
Expand All @@ -18,8 +18,8 @@ public struct JobsRedisDriver {
/// - Parameters:
/// - database: The `RedisDatabase` to run commands on
/// - eventLoop: The `EventLoop` to run jobs on
public init(database: RedisDatabase, eventLoop: EventLoop) {
self.database = database
public init(client: RedisClient, eventLoop: EventLoop) {
self.client = client
self.eventLoop = eventLoop
}
}
Expand All @@ -30,53 +30,43 @@ extension JobsRedisDriver: JobsPersistenceLayer {
public func get(key: String) -> EventLoopFuture<JobStorage?> {
let processing = processingKey(key: key)

return database.newConnection(on: eventLoop).flatMap { conn in
return conn.rpoplpush(source: key, destination: processing).and(result: conn)
}.flatMap(to: (RedisData, RedisClient).self) { redisData, conn in
return client.rpoplpush(from: key, to: processing).flatMap { redisData -> EventLoopFuture<String?> in
guard let id = redisData.string else {
conn.close()
throw Abort(.internalServerError)
return self.eventLoop.makeFailedFuture(Abort(.internalServerError))
}

return conn.rawGet(id).and(result: conn)
}.map { redisData, conn in
conn.close()

guard let data = redisData.data else {
return self.client.get(id)
}.flatMapThrowing { redisData in
guard let data = redisData?.data(using: .utf8) else {
print("Could not convert redis data to Data")
return nil
}

let decoder = try JSONDecoder().decode(DecoderUnwrapper.self, from: data)
return try JobStorage(from: decoder.decoder)
}.catchMap { error in
return nil
}.flatMapError { _ in
return self.eventLoop.makeSucceededFuture(nil)
}
}

/// See `JobsPersistenceLayer.set`
public func set(key: String, jobStorage: JobStorage) -> EventLoopFuture<Void> {
return database.newConnection(on: eventLoop).flatMap(to: (RedisData, RedisClient).self) { conn in
let data = try JSONEncoder().encode(jobStorage).convertToRedisData()
return conn.set(jobStorage.id, to: data).transform(to: (data, conn))
}.flatMap { data, conn in
return conn.lpush([try jobStorage.id.convertToRedisData()], into: key).transform(to: conn)
}.map { conn in
return conn.close()
guard let data = try? JSONEncoder().encode(jobStorage).convertedToRESPValue() else {
return self.eventLoop.makeFailedFuture(JobsRedisDriverError.couldNotConvertData)
}

return client.set(jobStorage.id, to: data).flatMap { data in
return self.client.lpush([jobStorage.id.convertedToRESPValue()], into: key).transform(to: ())
}
}

/// See `JobsPersistenceLayer.completed`
public func completed(key: String, jobStorage: JobStorage) -> EventLoopFuture<Void> {
return database.newConnection(on: eventLoop).flatMap(to: RedisClient.self) { conn in
let processing = try self.processingKey(key: key).convertToRedisData()
let count = try 0.convertToRedisData()

return conn.command("LREM", [processing, count, try jobStorage.id.convertToRedisData()]).transform(to: conn)
}.flatMap(to: RedisClient.self) { conn in
return conn.delete(jobStorage.id).transform(to: conn)
}.map { conn in
conn.close()
let processing = self.processingKey(key: key)
let jobData = jobStorage.id.convertedToRESPValue()

return client.lrem(jobData, from: processing, count: 0).flatMap { _ in
return self.client.delete([jobStorage.id]).transform(to: ())
}
}

Expand Down
15 changes: 15 additions & 0 deletions Sources/JobsRedisDriver/JobsRedisDriverError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// JobsRedisDriverError.swift
//
//
// Created by Jimmy McDermott on 6/11/19.
//

import Foundation

/// Describes an error from the JobsRedisDriver
public enum JobsRedisDriverError: Error {

/// Could not convert data to RESPValue
case couldNotConvertData
}
Loading

0 comments on commit 4d69716

Please sign in to comment.