Swiftpack.co - clutter/Pelican as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by clutter.
clutter/Pelican 2.1.0
Batch processing library written in Swift
⭐️ 6
🕓 1 year ago
.package(url: "https://github.com/clutter/Pelican.git", from: "2.1.0")

Pelican

Build Status Version License Platform

Pelican is a persisted batching library useful for log rolling, event logging or doing other periodic background processing.

Example

The following is an example of setting up custom event logging, for example to your own API using Pelican to batch upload tasks.

protocol AppEvents: PelicanGroupable {
    var eventName: String { get }
    var timeStamp: Date { get }
}

// Batch all app events together into the same group
extension AppEvents {
    // Used to group events so they can batched
    var group: String {
        return "Example App Events"
    }

    /// Pelican will call `processGroup` on a background thread. You should implement it to do whatever processing is 
    /// relevant to your application. Below is a sample implementation that sends application events as a batch.
    static func processGroup(tasks: [PelicanBatchableTask], didComplete: @escaping ((PelicanProcessResult) -> Void)) {
        // Construct JSON to pass to your API.
        let postData = Data()
        API.shared.postEvents(json: postData) { error in
            if error == nil {
                didComplete(PelicanProcessResult.done)
            } else {
                // Retry will call process group again until succesful
                didComplete(PelicanProcessResult.retry(delay: 0))
            }
        }
    }
}

// Using compiler generated conformance since PelicanBatchableTask refines Codable
struct LogInEvent: PelicanBatchableTask, AppEvents {
    let eventName: String = "Log In"
    let timeStamp: Date
    let userName: String

    init(userName: String) {
        self.timeStamp = Date()
        self.userName = userName
    }

    // PelicanBatchableTask conformance, used to read and store task to storage
    static let taskType: String = String(describing: LogInEvent.self)
}

Register your batchable tasks and initialize Pelican as soon as possible, for example in application(_ application: UIApplication, didFinishLaunchingWithOptions...

var tasks = Pelican.RegisteredTasks()
tasks.register(for: LogInEvent.self)

Pelican.initialize(tasks: tasks)

When the user performs an event we log it with Pelican, after 5 seconds events are grouped and processGroup is called to send them.

Pelican.shared.gulp(task: LogInEvent(userName: "Ender Wiggin"))

Requirements

Pelican requires Swift 5 or higher.

Installation

Pelican is available through CocoaPods or Swift Package Manager.

Author

[email protected]

License

Pelican is available under the MIT license. See the LICENSE file for more info.

GitHub

link
Stars: 6
Last commit: 1 year ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

2.1.0
1 year ago
  • Support for SPM

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics