Swiftpack.co - Package - MihaelIsaev/VaporCron

Mihael Isaev

MIT License Swift 4.2


Support this lib by giving a ⭐️

How to install

Swift Package Manager

.package(url: "https://github.com/MihaelIsaev/VaporCron.git", from:"1.0.0")

In your target's dependencies add "VaporCron" e.g. like this:

.target(name: "App", dependencies: ["VaporCron"]),

Usage

Simple job with closure

import VaporCron

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

Complex job in dedicated struct

import VaporCron

/// Your job should conform to `VaporCronSchedulable`
struct ComplexJob: VaporCronSchedulable {
    static var expression: String { return "*/2 * * * *" }

    static func task(on container: VaporCronContainer) -> Future<Void> { // Void is not a requirement, you may return any type
        return eventLoop.newSucceededFuture(result: ()).always {
            print("ComplexJob fired")
        }
    }
}
let complexJob = try? VaporCron.schedule(ComplexJob.self, on: app)

Scheduled job may be cancelled just by calling .cancel()

Where to define

On boot

You could define all cron jobs in your boot.swift cause here is app: Application which contains eventLoop

import Vapor
import VaporCron

/// Called after your application has initialized.
public func boot(_ app: Application) throws {
    let complexJob = try? VaporCron.schedule(ComplexJob.self, on: app)
    /// 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)) {
        complexJob?.cancel()
    }
}

In request handler

Some jobs you may want to schedule from some request handler like this

import Vapor
import VaporCron

func myEndpoint(_ req: Request) throws -> Future<HTTPStatus> {
    try VaporCron.schedule(ComplexJob.self, on: req)
    return .ok
}

How to do something in the database every 5 minutes?

import Vapor
import VaporCron

struct Every5MinJob: VaporCronSchedulable {
    static var expression: String { return "*/5 * * * *" } // every 5 minutes
    
    static func task(on container: VaporCronContainer) -> Future<Void> {
        // this is how you could get a connection to the database
        return container.requestPooledConnection(to: .psql).flatMap { conn in
            // here you sould do whatever you want cause you already have a connection to database
            // it's just an example below
            return User.query(on: conn).all().flatMap { users in
                return users.map { user in
                    user.updatedAt = Date()
                    return user.save(on: conn).transform(to: ())
                }.flatten(on: container)
            }
        }.always {
            // this is how to close taken pooled connection
            try? container.releasePooledConnection(conn, to: .psql)
        }
    }
}

Dependencies

Contributing

Please feel free to contribute!

Github

link
Stars: 13
Help us keep the lights on

Used By

Total: 1

Releases

1.0.0 - Mar 7, 2019

Mihael Isaev

MIT License Swift 4.2


Support this lib by giving a ⭐️

How to install

Swift Package Manager

.package(url: "https://github.com/MihaelIsaev/VaporCron.git", from:"1.0.0")

In your target's dependencies add "VaporCron" e.g. like this:

.target(name: "App", dependencies: ["VaporCron"]),

Usage

Simple job with closure

import VaporCron

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

Complex job in dedicated struct

import VaporCron

/// Your job should conform to `VaporCronSchedulable`
struct ComplexJob: VaporCronSchedulable {
    static var expression: String { return "*/2 * * * *" }

    static func task(on container: VaporCronContainer) -> Future<Void> { // Void is not a requirement, you may return any type
        return eventLoop.newSucceededFuture(result: ()).always {
            print("ComplexJob fired")
        }
    }
}
let complexJob = try? VaporCron.schedule(ComplexJob.self, on: app)

Scheduled job may be cancelled just by calling .cancel()

Where to define

On boot

You could define all cron jobs in your boot.swift cause here is app: Application which contains eventLoop

import Vapor
import VaporCron

/// Called after your application has initialized.
public func boot(_ app: Application) throws {
    let complexJob = try? VaporCron.schedule(ComplexJob.self, on: app)
    /// 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)) {
        complexJob?.cancel()
    }
}

In request handler

Some jobs you may want to schedule from some request handler like this

import Vapor
import VaporCron

func myEndpoint(_ req: Request) throws -> Future<HTTPStatus> {
    try VaporCron.schedule(ComplexJob.self, on: req)
    return .ok
}

How to do something in the database every 5 minutes?

import Vapor
import VaporCron

struct Every5MinJob: VaporCronSchedulable {
    static var expression: String { return "*/5 * * * *" } // every 5 minutes
    
    static func task(on container: VaporCronContainer) -> Future<Void> {
        // this is how you could get a connection to the database
        return container.requestPooledConnection(to: .psql).flatMap { conn in
            // here you sould do whatever you want cause you already have a connection to database
            // it's just an example below
            return User.query(on: conn).all().flatMap { users in
                return users.map { user in
                    user.updatedAt = Date()
                    return user.save(on: conn).transform(to: Void.self)
                }.flatten(on: container)
            }
        }.always {
            // this is how to close taken pooled connection
            try? container.releasePooledConnection(conn, to: .psql)
        }
    }
}

Dependencies

Contributing

Please feel free to contribute!