I'm trying to schedule the query of a random element every minute in my database.
The schedule part works but I don't find a way to pick an item from the database...
I'm a swift beginner. Maybe I missed something important !
Let me know if you need more informations
This is my code from boot.swift
import Fluent
import FluentSQLite
func foo(on container: Container) {
let future = container.withPooledConnection(to: .sqlite) { db in
return Future.map(on: container){ }
}
future.do{ msg in
let allQuotes = QuoteOfTheDay.query(on: ).all().wait()
quoteOfTheDay = allQuotes.randomElement()
}.catch{ error in
print("\(error.localizedDescription)")
}
}
/// Called after your application has initialized.
public func boot(_ app: Application) throws {
// your code here
func runRepeatTimer() {
app.eventLoop.scheduleTask(in: TimeAmount.minutes(1), runRepeatTimer)
foo(on: app)
}
runRepeatTimer()
}
Application conforms to Container so you can call it in boot(_:) and just provider the application
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With