Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a missing index to MongoQueue #22

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
"version" : "7.9.0"
}
},
{
"identity" : "swift-async-algorithms",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-async-algorithms.git",
"state" : {
"revision" : "4c3ea81f81f0a25d0470188459c6d4bf20cf2f97",
"version" : "1.0.3"
}
},
{
"identity" : "swift-atomics",
"kind" : "remoteSourceControl",
Expand Down Expand Up @@ -99,6 +108,15 @@
"version" : "1.0.0"
}
},
{
"identity" : "swift-service-lifecycle",
"kind" : "remoteSourceControl",
"location" : "https://github.com/swift-server/swift-service-lifecycle.git",
"state" : {
"revision" : "f70b838872863396a25694d8b19fe58bcd0b7903",
"version" : "2.6.2"
}
},
{
"identity" : "swift-system",
"kind" : "remoteSourceControl",
Expand Down
4 changes: 3 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ let package = Package(
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/orlandos-nl/MongoKitten.git", from: "7.9.0"),
.package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "2.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand All @@ -26,7 +27,8 @@ let package = Package(
name: "MongoQueue",
dependencies: [
.product(name: "MongoKitten", package: "MongoKitten"),
.product(name: "Meow", package: "MongoKitten")
.product(name: "Meow", package: "MongoKitten"),
.product(name: "ServiceLifecycle", package: "swift-service-lifecycle"),
]
),
.testTarget(
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ Register the task to MongoQueue, before it starts.
queue.registerTask(RegistrationEmailTask.self, context: ())
```

Start the queue in the background - this is helpful in use inside HTTP applications - or when creating separate workers.
### Service Lifecycle

```swift
try queue.runInBackground()
```

Alternatively, run the queue in the foreground and block until the queue is stopped. Only use this if your queue worker is only running as a worker. I.E., it isn't serving users on the side.
MongoQueue can be used as a service, and will automatically start and stop when the service starts and stops.

```swift
try await queue.run()
```

Hummingbird 2 users can use the `MongoQueue` to their `Application` to automatically start and stop the queue along with the application.

### Adding Tasks

Queue the task in MongoDB:

```swift
Expand Down
18 changes: 15 additions & 3 deletions Sources/MongoQueue/MongoQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Foundation
import Meow
import Tracing
import Metrics

import ServiceLifecycle
/// A MongoQueue is a queue that uses MongoDB as a backend for storing tasks. It is designed to be used in a distributed environment.
///
/// 1. First, connect to MongoDB and create the MongoQueue.
Expand All @@ -27,7 +27,7 @@ import Metrics
/// ```swift
/// try await queue.queueTask(Reminder(username: "Joannis"))
/// ```
public final class MongoQueue: @unchecked Sendable {
public final class MongoQueue: @unchecked Sendable, Service {
public struct Option: Hashable {
internal enum _Option: Hashable {
case uniqueKeysEnabled
Expand Down Expand Up @@ -328,7 +328,19 @@ public final class MongoQueue: @unchecked Sendable {
)
uniqueKeyIndex.unique = true
uniqueKeyIndex.partialFilterExpression = ["status": ["$in": ["scheduled", "executing"]]]
try await collection.createIndexes([uniqueKeyIndex])

let findNextTaskIndex = CreateIndexes.Index(
named: "next-task-index",
keys: [
"status": 1,
"executeAfter": 1,
"priority": -1,
"executeBefore": 1,
"creationDate": 1
]
)

try await collection.createIndexes([uniqueKeyIndex, findNextTaskIndex])
}
}

Expand Down
Loading