-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from BrettRToomey/docs-tests-readme
Cleanup and doc updates for minor release.
- Loading branch information
Showing
9 changed files
with
208 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,72 @@ | ||
# Jobs | ||
[![Language](https://img.shields.io/badge/Swift-3-brightgreen.svg)](http://swift.org) ![Build Status](https://travis-ci.org/BrettRToomey/Jobs.svg?branch=master)[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/BrettRToomey/Jobs/master/LICENSE.md) | ||
|
||
A job system in Swift, for Swift | ||
A minimalistic job system in Swift, for Swift | ||
|
||
## Getting started | ||
Start Jobs by calling | ||
##### Table of Contents | ||
* [Getting started](#getting-started) | ||
* [Intervals](#intervals) | ||
* [Syntax candy](#syntax-candy) | ||
* [Starting a job](#starting-a-job) | ||
* [Stopping a job](#stopping-a-job) | ||
* [Error handling](#error-handling) | ||
* [Retry on failure](#retry-on-failure) | ||
|
||
## Getting started 🚀 | ||
Creating a new `Job` is as simple as: | ||
```swift | ||
try Jobs.shared.start() | ||
Jobs.add(interval: .seconds(4)) { | ||
print("👋 I'm printed 4 times!") | ||
} | ||
``` | ||
|
||
And add a new `job` like so | ||
## Intervals ⏲ | ||
The `Duration` enumeration currently supports `.seconds`, `.days` and `.weeks`. | ||
```swift | ||
Jobs.shared.add(interval: .seconds(4)) { | ||
print("I am ran every 4 seconds.") | ||
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`. | ||
```swift | ||
10.seconds // `Duration.seconds(10)` | ||
2.days // `Duration.days(2)` | ||
3.weeks // `Duration.weeks(3)` | ||
``` | ||
|
||
## Intervals | ||
The `Duration` enumeration currently supports `.seconds`, `.days` and `.weeks` | ||
## Starting a job 🎬 | ||
By default, `Job`s are started automatically, but if you wish to start one yourself, even at a later point in time, just do the following: | ||
```swift | ||
Jobs.shared.add(interval: .days(5)) { | ||
print("I am ran every 5 days.") | ||
let job = Jobs.add(interval: 2.seconds, autoStart: false) { | ||
print("I wasn't started right away.") | ||
} | ||
//... | ||
job.start() | ||
``` | ||
|
||
## Removal | ||
You are returned a **discardable** `JobId (UInt)` when you add a `job`. You need this `id` if you want to remove it at a later time. | ||
## Stopping a job ✋ | ||
Giving up has never been so easy! | ||
```swift | ||
//keep a reference to `id` so we can use it to | ||
//remove the job later | ||
let id = Jobs.shared.add( //... { | ||
// ... | ||
} | ||
job.stop() | ||
``` | ||
|
||
Jobs.shared.remove(id) | ||
## Error handling ❌ | ||
Sometimes jobs can fail, that's okay, we have you covered. | ||
```swift | ||
Jobs.add( | ||
interval: 10.seconds, | ||
action: { | ||
throw Error.someError | ||
}, | ||
onError: { error in | ||
print("caught an error: \(error)") | ||
} | ||
) | ||
``` | ||
|
||
## Cleanup | ||
Stop Jobs by calling | ||
#### Retry on failure ⭕️ | ||
By default, jobs are ran again on error. If you wish to change this behavior then set the following attribute: | ||
```swift | ||
try Jobs.shared.stop() | ||
myJobName.retryOnFail = false | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,23 @@ | ||
extension Int { | ||
/// Converts the integer into an enum representation of seconds. | ||
var seconds: Duration { | ||
return .seconds(Double(self)) | ||
} | ||
|
||
/// Converts the integer into an enum representation of days. | ||
var days: Duration { | ||
return .days(self) | ||
} | ||
|
||
/// Converts the integer into an enum representation of weeks. | ||
var weeks: Duration { | ||
return .weeks(self) | ||
} | ||
} | ||
|
||
extension Double { | ||
/// Converts the real into an enum representation of seconds. | ||
var seconds: Duration { | ||
return .seconds(self) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.