Giter Club home page Giter Club logo

scheduler's Introduction

Go Task Scheduler

https://img.shields.io/travis/rakanalh/scheduler/master.svg?style=flat-square http://codecov.io/github/rakanalh/scheduler/coverage.svg?branch=master https://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square https://img.shields.io/badge/License-MIT-orange.svg?style=flat-square

Go Task scheduler is a small library that you can use within your application that enables you to execute callbacks (goroutines) after a pre-defined amount of time. GTS also provides task storage which is used to invoke callbacks for tasks which couldn’t be executed during down-time as well as maintaining a history of the callbacks that got executed.

Features

  • Execute tasks based after a specific duration or at a specific point in time
  • Job stores for history & recovery, provided stores out of the box:
    • Sqlite3
    • Redis (Coming soon)

Installation

go get github.com/rakanalh/scheduler

How To Use

Instantiate a scheduler as follows:

s := scheduler.New(storage)

GTS currently supports 2 kinds of storages:

  1. NoOpStorage: Does nothing
noOpStorage := storage.NewNoOpStorage()
  1. MemoryStorage: Does nothing
memStorage := storage.NewMemoryStorage()
  1. SqliteStorage: Persists tasks into a SQLite3 database.
sqliteStorage := storage.NewSqlite3Storage()

Example:

storage := storage.NewSqlite3Storage(
	storage.Sqlite3Config{
		DbName: "db.store",
	},
)
if err := storage.Connect(); err != nil {
	log.Fatal("Could not connect to db", err)
}

if err := storage.Initialize(); err != nil {
	log.Fatal("Could not intialize database", err)
}

and then pass it to the scheduler.

Scheduling tasks can be done in 3 ways:

Execute a task which after 5 seconds.

func MyFunc(arg1 string, arg2 string)
taskID := s.RunAfter(5*time.Second, MyFunc, "Hello", "World")

Execute a task at a specific time.

func MyFunc(arg1 string, arg2 string)
taskID := s.RunAt(Time.Now.Add(24 * time.Hour, MyFunc, "Hello", "World")

Execute a task every 1 minute.

func MyFunc(arg1 string, arg2 string)
taskID := s.RunEvery(1 * time.Minute, MyFunc, "Hello", "World")

Examples

The Examples folder contains a bunch of code samples you can look into.

Custom Storage

GTS supports the ability to provide a custom storage, the newly created storage has to implement the TaskStore interface

type TaskStore interface {
	Store(task *TaskAttributes) error
        Remove(task *TaskAttributes) error
	Fetch() ([]TaskAttributes, error)
}

TaskAttributes looks as follows:

type TaskAttributes struct {
	Hash        string
	Name        string
	LastRun     string
	NextRun     string
	Duration    string
	IsRecurring string
	Params      string
}

TODOs

  • [ ] Design a cron-like task schedule for RunEvery method

Credit

This package is heavily inspired by APScheduler for Python & GoCron

License

MIT

scheduler's People

Contributors

rakanalh avatar hotid avatar

Watchers

Eko B. Karuniawan 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.