Contents

hummingbird-project/swift-jobs

Job queue for processing workloads asynchronously across multiple nodes.

Register a Job

// create job queue stored in local memory, that can run four jobs concurrently
let jobService = JobService(
    .memory, 
    logger: logger
)
// Define email job parameters. Its job name has to be unique
struct SendEmailJobParameters: JobParameters {
    static let jobName = "SendEmail"
    let to: String
    let subject: String
    let body: String
}
// Register jobs with job queue
jobService.registerJob(parameters: SendEmailJobParameters.self) { parameters, context in
    try await myEmailService.sendEmail(
        to: parameters.to, 
        subject: parameters.subject, 
        body: parameters.body
    )
}
// Push instance of job onto queue
jobService.push(SendEmailJobParameters(
    to: "Ellen", 
    subject:"Hello", 
    body: "Hi there!"
))

Processing Jobs

JobService conforms to Service and can be used with ServiceGroup from ServiceLifecycle. Internally this will create a JobQueueProcessor to process jobs added to your JobService

let serviceGroup = ServiceGroup(
    configuration: .init(
        services: [jobService],
        gracefulShutdownSignals: [.sigterm, .sigint],
        logger: Logger(label: "JobQueueService")
    )
)
try await serviceGroup.run()

Or it can be added as a service attached to a Hummingbird application

let app = Application(router: router, services: [jobService])

When the JobQueueProcessor service is running it executes jobs as they appear on the queue. The numWorkers field in the options initializer indicates how many jobs it will run concurrently.

Scheduling Jobs

JobService also include a job scheduler that will run jobs at regular intervals.

jobService.addScheduledJob(CleanupDatabaseJob(), schedule: .weekly(day: .sunday, hour: 4))
// the scheduler also accepts crontab strings and will support most combinations
jobService.addScheduledJob(CleanupDatabaseJob(), schedule: .crontab("0 4 * * sun"))

Documentation

You can find documentation for Jobs here. The hummingbird-examples repository has a number of examples of different uses of the library.

Package Metadata

Repository: hummingbird-project/swift-jobs

Default branch: main

README: README.md