Step 1. Install Redis on your Mac

Follow the official guide

https://redis.io/docs/latest/operate/oss_and_stack/install/archive/install-redis/install-redis-on-mac-os/

brew install redis

brew services start redis

brew services info redis

redis-cli

Step 2. Setup Redis Configuration in Vapor App

Add Redis Swift Package

.package(url: "https://github.com/vapor/queues-redis-driver.git", from: "1.0.0")

// swift-tools-version:6.0
import PackageDescription

let package = Package(
    name: "TestServer",
    platforms: [
        .macOS(.v13)
    ],
    dependencies: [
        // 💧 A server-side Swift web framework.
        .package(url: "https://github.com/vapor/vapor.git", from: "4.111.0"),
        .package(url: "https://github.com/vapor/fluent", from: "4.12.0"),
        .package(url: "https://github.com/vapor/fluent-sqlite-driver", from: "4.8.0"),
        .package(url: "https://github.com/vapor/sql-kit", from: "3.33.2"),
        .package(url: "https://github.com/lukaskubanek/LoremSwiftum", from: "2.2.3"),
        .package(url: "https://github.com/vapor/fluent-postgres-driver", from:"2.10.0"),
        .package(url: "https://github.com/vapor/jwt", from: "5.1.2"),
        .package(url: "https://github.com/vapor/queues-redis-driver.git", from: "1.0.0"),
    ],
    targets: [
        .target(
            name: "App",
            dependencies: [
                .product(name: "Vapor", package: "vapor"),
                .product(name: "Fluent", package: "fluent"),
                .product(name: "FluentSQLiteDriver", package: "fluent-sqlite-driver"),
                .product(name: "SQLKit", package: "sql-kit"),
                .product(name: "LoremSwiftum", package: "LoremSwiftum"),
                .product(name: "JWT", package: "jwt"),
                .product(name: "FluentPostgresDriver", package: "fluent-postgres-driver"),
                .product(name: "QueuesRedisDriver", package: "queues-redis-driver")
            ],
            swiftSettings: [
                // Enable better optimizations when building in Release configuration. Despite the use of
                // the `.unsafeFlags` construct required by SwiftPM, this flag is recommended for Release
                // builds. See <https://github.com/swift-server/guides#building-for-production> for details.
                .unsafeFlags(["-cross-module-optimization"], .when(configuration: .release))
            ]
        ),
        .executableTarget(name: "Run", dependencies: [
                .target(name: "App")
            ]
        ),
        .testTarget(name: "AppTests", dependencies: [
            .target(name: "App"),
            .product(name: "XCTVapor", package: "vapor")
        ])
    ]
)


Create an AsyncScheduledJob

import Foundation
import Vapor
import Queues

struct ScheduledJobs: AsyncScheduledJob {
    // Add extra services here via dependency injection, if you need them.
    func run(context: QueueContext) async throws {
        context.logger.info("Starting ScheduledJobs")
        print("✅ It is called")
        //Call other services using context.application.client
//        context.application.client
        context.logger.info("ScheduledJobs completed")
    }
}

Register Scheduled Job in Vapor App

https://docs.vapor.codes/advanced/queues/#available-builder-methods

import Vapor
import QueuesRedisDriver

public func configure(_ app: Application) throws {
    let redisConfig = try RedisConfiguration(
        hostname: "127.0.0.1",
        port: 6379,
        pool: .init(
            maximumConnectionCount: .maximumActiveConnections(50),
            minimumConnectionCount: 10
        )
    )
    app.redis.configuration = redisConfig
    //Use Redis
    app.queues.use(.redis(redisConfig))
    
    //Register ScheduledJob - It runs every 30 seconds
    app.queues.schedule(ScheduledJobs())
        .minutely()
        .at(30)
    
    try app.queues.startScheduledJobs()

....
}

Conclusion

Hope my articles helps you who want to run scheduled job using Redis. Please like my post or leave a comment it helps me continue share my knowledge for free.

Leave a comment

Quote of the week

"People ask me what I do in the winter when there's no baseball. I'll tell you what I do. I stare out the window and wait for spring."

~ Rogers Hornsby