Skip to content

Commit

Permalink
Better logging, Swift 5.8
Browse files Browse the repository at this point in the history
  • Loading branch information
Joannis committed Aug 25, 2023
1 parent 4697657 commit 977ee39
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
dbimage:
- mongo
runner:
- swift:5.6-focal
- swift:5.8-focal
- swiftlang/swift:nightly-main-focal
container: ${{ matrix.runner }}
runs-on: ubuntu-latest
Expand Down
34 changes: 26 additions & 8 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,40 @@
"version": "8.0.9"
}
},
{
"package": "DNSClient",
"repositoryURL": "https://github.com/orlandos-nl/DNSClient.git",
"state": {
"branch": null,
"revision": "770249dcb7259c486f2d68c164091b115ccb765f",
"version": "2.2.1"
}
},
{
"package": "MongoKitten",
"repositoryURL": "https://github.com/orlandos-nl/MongoKitten.git",
"state": {
"branch": null,
"revision": "4850c77a604fa10fc16c8ccac0d808b6461a0f74",
"version": "7.0.0"
"revision": "34929f3126017edb1e29cfc16fb19881189cf58d",
"version": "7.7.1"
}
},
{
"package": "DNSClient",
"repositoryURL": "https://github.com/orlandos-nl/NioDNS.git",
"package": "swift-atomics",
"repositoryURL": "https://github.com/apple/swift-atomics.git",
"state": {
"branch": null,
"revision": "6c89474e62719ddcc1e9614989fff2f68208fe10",
"version": "1.1.0"
}
},
{
"package": "swift-collections",
"repositoryURL": "https://github.com/apple/swift-collections.git",
"state": {
"branch": null,
"revision": "71cfedd3310d59b7d19e290176dd2a1a605a4252",
"version": "2.0.7"
"revision": "937e904258d22af6e447a0b72c0bc67583ef64a2",
"version": "1.0.4"
}
},
{
Expand All @@ -51,8 +69,8 @@
"repositoryURL": "https://github.com/apple/swift-nio.git",
"state": {
"branch": null,
"revision": "124119f0bb12384cef35aa041d7c3a686108722d",
"version": "2.40.0"
"revision": "cf281631ff10ec6111f2761052aa81896a83a007",
"version": "2.58.0"
}
},
{
Expand Down
9 changes: 7 additions & 2 deletions Sources/MongoQueue/KnownType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ internal struct KnownType {
ofType type: T.Type,
context: T.ExecutionContext
) async throws {
logger.debug("Executing task \(task._id) of category \"\(T.category)\"")
let collection = queue.collection
var metadata: T

Expand Down Expand Up @@ -98,10 +99,10 @@ internal struct KnownType {

defer { executionUpdates.cancel() }
try await metadata.execute(withContext: context)

logger.debug("Successful execution: task \(task._id) of category \"\(T.category)\"")
_ = try await metadata._onDequeueTask(task, withContext: context, inQueue: queue)
} catch {
logger.error("Execution failure for task \(task._id) in category \"\(T.category))\": \(error.localizedDescription)")
logger.debug("Execution failure for task \(task._id) in category \"\(T.category))\": \(error.localizedDescription)")
let failureContext = QueuedTaskFailure(
executionContext: context,
error: error,
Expand All @@ -120,10 +121,12 @@ internal struct KnownType {
let update = try await queue.collection.upsertEncoded(task, where: "_id" == task._id)

guard update.updatedCount == 1 else {
logger.error("Failed to soft-delete task \(task._id) of category \"\(T.category)\"")
throw MongoQueueError.dequeueTaskFailed
}
case .dequeue:
guard try await collection.deleteOne(where: "_id" == task._id).deletes == 1 else {
logger.error("Failed to delete task \(task._id) of category \"\(T.category)\"")
throw MongoQueueError.dequeueTaskFailed
}
}
Expand All @@ -134,6 +137,7 @@ internal struct KnownType {
try await applyRemoval(removal)
case .retry(maxAttempts: let maxAttempts, let removal):
if let maxAttempts = maxAttempts, task.attempts >= maxAttempts {
logger.debug("Task Removal: task \(task._id) of category \"\(T.category)\" exceeded \(maxAttempts) attempts")
try await applyRemoval(removal)
} else {
task.status = .scheduled
Expand All @@ -145,6 +149,7 @@ internal struct KnownType {
}
case .retryAfter(let nextInterval, maxAttempts: let maxAttempts, let removal):
if let maxAttempts = maxAttempts, task.attempts >= maxAttempts {
logger.debug("Task Removal: task \(task._id) of category \"\(T.category)\" exceeded \(maxAttempts) attempts")
try await applyRemoval(removal)
} else {
task.status = .scheduled
Expand Down
8 changes: 4 additions & 4 deletions Sources/MongoQueue/MongoQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public final class MongoQueue {
}

_ = try? await queue.findAndRequeueStaleTasks()
_ = try? await Task.sleep(nanoseconds: UInt64(stalledTaskPollingFrequency.nanoseconds))
_ = try? await Task.sleep(nanoseconds: UInt64(queue.stalledTaskPollingFrequency.nanoseconds))
} while !Task.isCancelled
}

Expand Down Expand Up @@ -283,7 +283,7 @@ public final class MongoQueue {
do {
switch try await self.runNextTask() {
case .taskFailure(let error):
logger.error("\(error)")
logger.debug("Failed to run task: \(error)")
fallthrough
case .taskSuccessful:
Task {
Expand All @@ -295,7 +295,7 @@ public final class MongoQueue {
} catch {
// Task execution failed due to a MongoDB error
// Otherwise the return type would specify the task status
logger.error("\(error)")
logger.error("Failed to run next task: \(error)")
serverHasData = false
}
}
Expand Down Expand Up @@ -361,7 +361,7 @@ public final class MongoQueue {
}

private func requeueStaleTask(_ task: TaskModel) async {
self.logger.info("Dequeueing stale task id \(task._id) of type \(task.category)")
self.logger.debug("Dequeueing stale task id \(task._id) of type \(task.category)")
do {
_ = try await self.collection.findOneAndUpdate(where: "_id" == task._id, to: [
"$set": [
Expand Down

0 comments on commit 977ee39

Please sign in to comment.