Skip to content
Permalink
Browse files

update to store data in individual keys

  • Loading branch information...
mcdappdev committed Feb 12, 2019
1 parent 8a230e5 commit a319c7a40eb6fc1dd826c9964512f9b3181ff5a5
@@ -33,26 +33,26 @@
"repositoryURL": "https://github.com/vapor/database-kit.git",
"state": {
"branch": null,
"revision": "3557894af50914e134803a684b42a9ea6eefaea2",
"version": "1.3.2"
"revision": "8f352c8e66dab301ab9bfef912a01ce1361ba1e4",
"version": "1.3.3"
}
},
{
"package": "HTTP",
"repositoryURL": "https://github.com/vapor/http.git",
"state": {
"branch": null,
"revision": "4c8c655932fdb409c11549eadabf27d3d61bc24c",
"version": "3.1.7"
"revision": "b57005e0de30ba36372ac41bfce1ac12b2bc3272",
"version": "3.1.8"
}
},
{
"package": "Jobs",
"package": "jobs",
"repositoryURL": "https://github.com/vapor-community/jobs.git",
"state": {
"branch": null,
"revision": "25bfceca47fdb745a0e804c47a29c6e63b4c636f",
"version": "0.2.2"
"revision": "fbe1778181387e7d9fd05263b2e5b4f936e5c23c",
"version": "0.2.4"
}
},
{
@@ -69,8 +69,8 @@
"repositoryURL": "https://github.com/vapor/redis.git",
"state": {
"branch": null,
"revision": "cec3ee1eb226f2323947016be9db626fe6d3fe37",
"version": "3.0.3"
"revision": "b14ae4174ee4c8d2bb9196c96144b591c5a6592a",
"version": "3.1.0"
}
},
{
@@ -87,8 +87,8 @@
"repositoryURL": "https://github.com/vapor/service.git",
"state": {
"branch": null,
"revision": "281a70b69783891900be31a9e70051b6fe19e146",
"version": "1.0.0"
"revision": "4907311d7d7f609365982fa302b8b17ffdeb46da",
"version": "1.0.1"
}
},
{
@@ -159,8 +159,8 @@
"repositoryURL": "https://github.com/vapor/vapor.git",
"state": {
"branch": null,
"revision": "54cbf396147aba77ecb32e7fc75a3d13251551b1",
"version": "3.1.1"
"revision": "2caf16238b6613b4fffeb8a14ad1cd20cb377379",
"version": "3.2.0"
}
},
{
@@ -32,32 +32,43 @@ extension JobsRedisDriver: JobsPersistenceLayer {

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
guard let id = redisData.string else { throw Abort(.internalServerError) }
return conn.rawGet(id).and(result: conn)
}.map { redisData, conn in
conn.close()

guard let data = redisData.data else { return nil }
let decoder = try JSONDecoder().decode(DecoderUnwrapper.self, from: data)
return try JobStorage(from: decoder.decoder)
}.catchMap { _ in
return nil
}
}

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

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

guard let value = try jobStorage.stringValue()?.convertToRedisData() else { throw Abort(.internalServerError, reason: "Cannot get string value") }
return conn.command("LREM", [processing, count, value]).transform(to: ())

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()
}
}

@@ -41,12 +41,19 @@ final class JobsRedisDriverTests: XCTestCase {

try jobsDriver.set(key: "key", jobStorage: jobStorage).wait()

guard let savedJobString = try redisConn.rPop("key").wait().data else {
XCTAssertNotNil(try redisConn.rawGet(jobStorage.id).wait().string)

guard let jobId = try redisConn.rPop("key").wait().string else {
XCTFail()
return
}

guard let retrievedJobData = try redisConn.rawGet(jobId).wait().data else {
XCTFail()
return
}

let decoder = try JSONDecoder().decode(DecoderUnwrapper.self, from: savedJobString)
let decoder = try JSONDecoder().decode(DecoderUnwrapper.self, from: retrievedJobData)
let retrievedJobStorage = try JobStorage(from: decoder.decoder)
let retrievedJob = try JSONDecoder().decode(Email.self, from: retrievedJobStorage.data)

0 comments on commit a319c7a

Please sign in to comment.
You can’t perform that action at this time.