Giter Club home page Giter Club logo

jobs's Introduction

Jobs

Language Build Status codecov codebeat badge GitHub license

A minimalistic job system in Swift, for Swift

Table of Contents

Integration

Update your Package.swift file.

.Package(url: "https://github.com/BrettRToomey/Jobs.git", majorVersion: 1)

Getting started ๐Ÿš€

Creating a new Job is as simple as:

Jobs.add(interval: .seconds(4)) {
    print("๐Ÿ‘‹ I'm printed every 4 seconds!")
}

Intervals โฒ

The Duration enumeration currently supports .seconds, hours, .days and .weeks.

Jobs.add(interval: .days(5)) {
    print("See you every 5 days.")
}

Syntax candy ๐Ÿญ

It's possible to create a Duration from an Int and a Double.

10.seconds // `Duration.seconds(10)`
4.hours // `Duration.hours(4)`
2.days // `Duration.days(2)`
3.weeks // `Duration.weeks(3)`

Starting a job ๐ŸŽฌ

By default, Jobs are started automatically, but if you wish to start one yourself, even at a later point in time, just do the following:

let job = Jobs.add(interval: 2.seconds, autoStart: false) {
    print("I wasn't started right away.")
}
//...
job.start()

Stopping a job โœ‹

Giving up has never been so easy!

job.stop()

One-off jobs

If you just want to asynchronously run a job, but not repeat it you can use the oneoff functions.

Jobs.oneoff {
    print("Sadly, I'm not a phoenix.")            
}

How about waiting a little?

Jobs.oneoff(delay: 10.seconds) {
    print("I was delayed by 10 seconds.")
}

Error handling โŒ

Sometimes jobs can fail, that's okay, we have you covered.

Jobs.add(
    interval: 10.seconds,
    action: {
        throw Error.someError
    }, 
    onError: { error in
        print("caught an error: \(error)")
        return RecoverStrategy.default
    }
)

Retry on failure โญ•๏ธ

By default, jobs will be attempted again after a five second delay. If you wish to override this behavior you must first implement an onError handler and return one of the following RecoveryStrategy cases.

.none //do not retry
.default //retry after 5 seconds
.retry(after: Duration) //retry after specified duration

Here's a small sample:

enum Error: Swift.Error {
  case recoverable
  case abort
}

Jobs.add(
    interval: 1.days,
    action: {
        //...
    }, 
    onError: { error in
        switch error {
        //we cannot recover from this
        case .abort:
            //do not retry
            return .none

        //we can recover from this
        case .recoverable:
            //... recovery code

            //try again in 15 seconds
            return .retry(after: 15.seconds)
        }
    }
)

jobs's People

Contributors

brettrtoomey avatar affanshahid avatar ksmandersen avatar mdab121 avatar petrpavlik avatar

Watchers

James Cloos avatar Leo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.