Built for NIO2
💡NIO1 version is available in
nio1
branch and from1.0.0
tag
.package(url: "https://github.com/MihaelIsaev/NIOCronScheduler.git", from:"2.1.0")
In your target's dependencies add "NIOCronScheduler"
e.g. like this:
.target(name: "App", dependencies: ["NIOCronScheduler"]),
import NIOCronScheduler
/// Simplest way is to use closure
let job = try? NIOCronScheduler.schedule("* * * * *", on: eventLoop) { // add one more * to launch every second
print("Closure fired")
}
/// Or create a struct that conforms to NIOCronSchedulable
struct Job1: NIOCronSchedulable {
static var expression: String { return "* * * * *" } // add one more * to launch every second
static func task() {
print("Job1 fired")
}
}
let job1 = try? NIOCronScheduler.schedule(Job1.self, on: eventLoop)
/// Or create a struct that conforms to NIOCronFutureSchedulable
/// to be able to return a future
struct Job2: NIOCronFutureSchedulable {
static var expression: String { return "*/2 * * * *" } // add one more * to launch every 2nd second
static func task(on eventLoop: EventLoop) -> EventLoopFuture<Void> { //Void is not a requirement, you may return any type
return eventLoop.newSucceededFuture(result: ()).always {
print("Job2 fired")
}
}
}
let job2 = try? NIOCronScheduler.schedule(Job2.self, on: eventLoop)
Scheduled job may be cancelled just by calling .cancel()
on it
The easiest way is to define all cron jobs in configure.swift
So it may look like this
import Vapor
import NIOCronScheduler
// Called before your application initializes.
func configure(_ app: Application) throws {
// ...
let job = try? NIOCronScheduler.schedule("* * * * *", on: app.eventLoopGroup.next()) {
print("Closure fired")
}
/// This example code will cancel scheduled job after 185 seconds
/// so in a console you'll see "Closure fired" three times only
app.eventLoopGroup.next().scheduleTask(in: .seconds(185)) {
job?.cancel()
}
}
Or sure you could schedule something from req: Request
cause it have eventLoopGroup.next()
inside itself as well.
Cron expression parsing works through SwifCron lib, please read it limitations and feel free to contribute into this lib as well
Please feel free to contribute!