-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add mongo queue for send verification email
- Loading branch information
Showing
11 changed files
with
279 additions
and
151 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import Vapor | ||
import MongoQueue | ||
|
||
private struct MongoQueueStorageKey: StorageKey { | ||
public typealias Value = MongoQueue | ||
} | ||
|
||
extension Application { | ||
public var mongoQueue: MongoQueue { | ||
get { | ||
storage[MongoQueueStorageKey.self]! | ||
} | ||
set { | ||
storage[MongoQueueStorageKey.self] = newValue | ||
} | ||
} | ||
|
||
public func initializeMongoQueue() { | ||
self.mongoQueue = MongoQueue(collection: mongoDB["queue"]) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Vapor | ||
import MongoQueue | ||
|
||
func mongoQueue(_ app: Application) throws { | ||
app.mongoQueue.registerTask(EmailJobMongoQueue.self, context: app) | ||
do { | ||
try app.mongoQueue.runInBackground() | ||
} catch { | ||
app.logger.info("\(error)") | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import Vapor | ||
import Mailgun | ||
import MongoQueue | ||
|
||
struct EmailJobMongoQueue: RecurringTask { | ||
|
||
var payload: EmailPayload | ||
var vCodeAttempt: VerificationCodeAttempt | ||
|
||
// The date this task should be executed at earliest | ||
// We default to one week from 'now' | ||
var initialTaskExecutionDate: Date { Date() } | ||
|
||
// We can use any execution context. This context is provided on registration, and the job can have access to it to do 'the work' | ||
// For example, if your job sends email, pass it a handle to the email client | ||
typealias ExecutionContext = Application | ||
|
||
// Ensures only one user engagement prompt exists per user | ||
var uniqueTaskKey: String { vCodeAttempt.id!.hexString } | ||
|
||
// The amount of time we expect this task to take if it's very slow | ||
// This is optional, and has a sensible default. Note; it will not be the task's actual deadline. | ||
// Instead, the task will be killed if worker executing this task goes offline unexpectedly | ||
// There's a mechanism in MongoQueue to detect a stale or dead task worker | ||
var maxTaskDuration: TimeInterval { 60 } | ||
|
||
// This job has a low priority. Some other jobs may be done first if there's a contest for job queue time | ||
var priority: TaskPriority { .higher } | ||
|
||
// Execute the task. In our case, we check their last login date | ||
func execute(withContext context: ExecutionContext) async throws { | ||
let mailgunMessage = MailgunTemplateMessage( | ||
from: context.config.noReplyEmail, | ||
to: payload.recipient, | ||
subject: payload.email.subject, | ||
template: payload.email.templateName, | ||
templateData: payload.email.templateData | ||
) | ||
|
||
do { | ||
_ = try await context.mailgun().send(mailgunMessage).get() | ||
} catch { | ||
print(error) | ||
} | ||
} | ||
|
||
// When to send the next reminder to the user | ||
// If `nil` is returned, this job will never run again | ||
func getNextRecurringTaskDate(_ context: ExecutionContext) async throws -> Date? { | ||
return nil | ||
} | ||
|
||
// If we failed to run the job for whatever reason (SMTP issue, database issue or otherwise) | ||
func onExecutionFailure(failureContext: QueuedTaskFailure<ExecutionContext>) async throws -> TaskExecutionFailureAction { | ||
// Retry in 1 hour, `nil` for maxAttempts means we never stop retrying | ||
print(failureContext.error) | ||
return .retryAfter(3600, maxAttempts: nil) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters