Mihael Isaev

MIT License Swift 5.1 Swift.Stream


给这个库一个 ⭐️ 来支持它

为 Vapor4 构建

💡Vapor3 版本可在 vapor3 分支和 1.0.0 tag 中找到

如何安装

Swift Package Manager

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

在你的 target 的 dependencies 中添加 "VaporCron",例如像这样

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

使用方法

使用闭包的简单任务

import VaporCron

let job = try app.cron.schedule("* * * * *") { // or add one more * to launch every second
    print("Closure fired")
}

在专用结构体中的复杂任务

import Vapor
import VaporCron

/// Your job should conform to `VaporCronSchedulable` or `VaporCronInstanceSchedulable`
struct ComplexJob: VaporCronSchedulable {
    static var expression: String { "* * * * *" } // or add one more * to launch every second

    static func task(on application: Application) -> EventLoopFuture<Void> {
        return application.eventLoopGroup.future().always { _ in
            print("ComplexJob fired")
        }
    }
}
let complexJob = try app.cron.schedule(ComplexJob.self)

struct ComplexInstanceJob: VaporCronInstanceSchedulable {
    static var expression: String { "* * * * *" } // or add one more * to launch every second

    private let application: Application
    
    init(application: Application) {
        self.application = application
    }

    func task() -> EventLoopFuture<Void> {
        return application.eventLoopGroup.future().always { _ in
            print("ComplexJob fired")
        }
    }
}
let complexInstanceJob = try app.cron.schedule(ComplexInstanceJob.self)

💡你也可以调用 req.cron.schedule(...)

💡💡可以通过调用 .cancel() 来取消已计划的任务

并发 (swift>=5.5)

使用 AsyncVaporCronSchedulable 代替 VaporCronSchedulable

使用 AsyncVaporCronInstanceSchedulable 代替 VaporCronInstanceSchedulable

public struct TestCron: AsyncVaporCronSchedulable {
    public typealias T = Void
    
    public static var expression: String {
        "*/1 * * * *" // or add one more * to launch every 1st second
    }
    
    public static func task(on application: Application) async throws -> Void {
        application.logger.info("\(Self.self) is running...")
    }
}

在哪里定义

在启动时

你可以在 boot.swift 中定义所有的 cron 任务,因为这里有包含 eventLoopapp: Application

import Vapor
import VaporCron

// Called before your application initializes.
func configure(_ app: Application) throws {
    let complexJob = try app.cron.schedule(ComplexJob.self)
    /// This example code will cancel scheduled job after 120 seconds
    /// so in a console you'll see "Closure fired" three times only
    app.eventLoopGroup.next().scheduleTask(in: .seconds(120)) {
        complexJob.cancel()
    }
}

在请求处理器中

你可能想从一些请求处理器中调度一些任务,像这样

import Vapor
import VaporCron
func routes(_ app: Application) throws {
    app.get("test") { req -> HTTPStatus in
        try req.cron.schedule(ComplexJob.self).transform(to: .ok)
    }
}

如何每 5 分钟在数据库中做一些事情?

import Vapor
import VaporCron

struct Every5MinJob: VaporCronSchedulable {
    static var expression: String { "*/5 * * * *" } // every 5 minutes

    static func task(on application: Application) -> Future<Void> {
        application.db.query(Todo.self).all().map { rows in
            print("ComplexJob fired, found \(rows.count) todos")
        }
    }
}

依赖项

贡献

请随时贡献!