NIOCronScheduler

2.1.0

⌚️Swift cron scheduler based on Swift NIO (both v1 and v2)
MihaelIsaev/NIOCronScheduler

What's New

Seconds support

2022-08-06T13:39:30Z

Seconds are optional, just use 6 symbols mask to launch in seconds mode

let job = try? NIOCronScheduler.schedule("* * * * * *", on: eventLoop) { // every second
    print("Closure fired")
}

The rest is the same as before

Mihael Isaev

MIT License Swift 5.1 Swift.Stream


Don't forget to support the lib by giving a ⭐️

Built for NIO2

💡NIO1 version is available in nio1 branch and from 1.0.0 tag

How to install

Swift Package Manager

.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"]),

Usage

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

For Vapor users

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.

Limitations

Cron expression parsing works through SwifCron lib, please read it limitations and feel free to contribute into this lib as well

Contributing

Please feel free to contribute!

Description

  • Swift Tools 5.1.0
View More Packages from this Author

Dependencies

Last updated: Sat Oct 19 2024 01:34:54 GMT-0900 (Hawaii-Aleutian Daylight Time)