Kyu

master

Kyu is persistent queue system in Swift.
reddavis/Kyu

Kyu

Kyu is a stupidly simple, persistant, queue system.

Requirements

  • iOS 15.0+
  • macOS 12.0+

Installation

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/reddavis/Kyu", from: "3.0.0-beta.1")
]

Usage

Kyu has two parts to it; The Kyu, which manages job state and execution and the Job which does the actual work.

The Job

A job must simply conform to the Job and Codable protocol.

A simple "Append new line to end of file" job could look something like:

final class AppendNewLineJob: Job
{
    let id: UUID
    var maximumNumberOfRetries: Int { 5 }
    var numberOfRetries = 0
    var executionDate = Date()
    
    let fileURL: URL
    let string: String
    
    // MARK: Initialization
    
    init(fileURL: URL, string: String)
    {
        self.id = UUID()
        self.fileURL = fileURL
        self.string = string
    }
    
    // MARK: Job
    
    func execute() async throws
    {
        let fileHandle = try FileHandle(forWritingTo: self.fileURL)
        try fileHandle.seekToEnd()
        fileHandle.write("\(self.string)\n".data(using: .utf8)!)
        fileHandle.closeFile()
    }
}

The Kyu

The Kyu manages and executes jobs. It take a URL parameter, which is where it will persist the jobs.

let url = URL(string: ...)!
self.kyu = try Kyu<AppendNewLineJob>(url: url)

let job = AppendNewLineJob(
    fileURL: fileURL, 
    string: "a string to append"
)
try await kyu.add(job: job)

Description

  • Swift Tools 5.3.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sat Mar 16 2024 07:26:18 GMT-0900 (Hawaii-Aleutian Daylight Time)