Giter Site home page Giter Site logo

swift-task-scheduler's Introduction

SwiftTaskScheduler

MIT License Swift 5.9

SwiftTaskScheduler - Swift tool for running scheduled tasks on specific days and times, as well as those that are supposed to be executed at a certain time interval, i.e. backups, service health checks, deleting old files, etc.

Usage example

Creates a task that runs only once at 15:30.

import SwiftTaskScheduler

let scheduler = TaskScheduler()

let task = scheduler.once(.at(15, 30)) {
    // DO SOME
}

// Or you can use default singleton instance of task scheduler
let task = TaskScheduler.default.once(.at(15, 30)) {
    // DO SOME
}

For creates scheduled tasks use task scheduler functions bellow:

// Creates repeatable tasks that will run at a specified time interval (starts at 00:00:00, and repeats every 30 minutes)
scheduler.start(.midnight, repeatEvery: 30.minutes) { }
// Starts 1 hour after creates that, and repeats every 3 hours
scheduler.start(after: 1.hours, repeatEvery: 3.hours) { }

// Creates a task that runs everyday at specified time 
scheduler.everyday(time: .midnight, .noon) { }

// Creates a task that runs every sun & sat
scheduler.weekends(time: .midnight) { }

// Creates a task that runs on the specified days of the week and time
scheduler.every(.monday, .tuesday, .thursday, time: .at(10)) { }

// Creates a task that runs on the specified days of the month and time
scheduler.every(1, 15, 30, time: .at(20, 30)) { }

For ease of management, the current task is transferred in the closure parameters. For example, if certain conditions are met, you can cancel further execution of the task:

// When a task is destroyed, it will also be deleted from the task scheduler
TaskScheduler.default.start(after: 0.seconds, repeatEvery: 30.minutes) { task in
    if condition {
        task.destroy()
    }
    // DO SOME
}

// You also can suspend and resume execution of task. The new execution time will be calculated, if needs.
task.suspend()
try task.resume()

For gracefully shutdown scheduler and tasks use .invalidate() method of TaskScheduler instance before deinit.

let scheduler = TaskScheduler()
...
scheduler.invalidate()

ExecutionStrategy type conforms Codable protocol for save and load it, and use on future for cold start tasks.

let data = try JSONSerialization.data(withJSONObject: [
    "type" : 1,
    "interval" : 60,
    "time" : [["hours": 15, "minutes": 30, "seconds": 0]]
])

// For example, we create a repeatable task from data received from somewhere
let decoder = JSONDecoder()
let strategy = try decoder.decode(ExecutionStrategy.self, from: data)
let task = JobTask(scheduler: TaskScheduler.default, strategy: strategy) { task in
    // DO SOME
}

Adding SwiftTaskScheduler to your project

Swift Package Manager

Add the following line to the 'dependencies' section of the Package.swift file:

dependencies: [
  .package(url: "https://github.com/egorbos/swift-task-scheduler.git", from: "0.1.0"),
],

CocoaPods

Add the following line to the Podfile:

pod 'SwiftTaskScheduler', :git => "https://github.com/egorbos/swift-task-scheduler.git", :tag => "0.1.0"

Compatibility

Platform Minimum version
macOS 10.15 (Catalina)
iOS, iPadOS & tvOS 13
watchOS 6

License

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

swift-task-scheduler's People

Contributors

egorbos avatar

Watchers

 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.