Swiftpack.co - Package - MihaelIsaev/NIOCronScheduler

Mihael Isaev

MIT License Swift 4.2


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

How to install

Swift Package Manager

.package(url: "https://github.com/MihaelIsaev/NIOCronScheduler.git", from:"1.0.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) {
    print("Closure fired")
}

/// Or create a struct that conforms to NIOCronSchedulable
struct Job1: NIOCronSchedulable {
    static var expression: String { return "* * * * *" }

    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 * * * *" }

    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 boot.swift cause here in app: Application container you could get eventLoop.

So it may look like this

import Vapor
import NIOCronScheduler

/// Called after your application has initialized.
public func boot(_ app: Application) throws {
    let job = try? NIOCronScheduler.schedule("* * * * *", on: app.eventLoop) {
        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.eventLoop.scheduleTask(in: .seconds(185)) {
        job?.cancel()
    }
}

Or sure you could schedule something from req: Request cause it have eventLoop 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!

Github

link
Stars: 2
Help us keep the lights on

Releases

1.0.0 - Mar 7, 2019

Mihael Isaev

MIT License Swift 4.2


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

How to install

Swift Package Manager

.package(url: "https://github.com/MihaelIsaev/NIOCronScheduler.git", from:"1.0.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) {
    print("Closure fired")
}

/// Or create a struct that conforms to NIOCronSchedulable
struct Job1: NIOCronSchedulable {
    static var expression: String { return "* * * * *" }

    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 * * * *" }

    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 boot.swift cause here in app: Application container you could get eventLoop.

So it may look like this

import Vapor
import NIOCronScheduler

/// Called after your application has initialized.
public func boot(_ app: Application) throws {
    let job = try? NIOCronScheduler.schedule("* * * * *", on: app.eventLoop) {
        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.eventLoop.scheduleTask(in: .seconds(185)) {
        job?.cancel()
    }
}

Or sure you could schedule something from req: Request cause it have eventLoop 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!