gron

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 29, 2020 License: MIT Imports: 7 Imported by: 0

README

gron: Yet another task scheduling utility

build Go Report Card Coverage Status

Gron is an elegant and simple periodic task scheduler, for human.

Inspired by Python pacakge schedule and Go package goCron

Feature

  • Parallel scheduling
  • Isolated tasks
  • Panic-proof
  • EventBus

Example

package main

import (
	"fmt"
	"time"

	. "github.com/u2386/gron"
)

func main() {
	Gron(
		Every(10),
		Seconds(),
		Do(func() {
			fmt.Println("FireBall!", time.Now().Format("15:04:05"))
		}),
		Name("Fireball!"),
	)

	Gron(
		Every(1),
		Minutes(),
		At("::01"),
		Do(func() {
			fmt.Println("Cast Blizzard!", time.Now().Format("15:04:05"))
		}),
		Name("Blizzard!"),
	)

	Gron(
		Every(4),
		Seconds(),
		Do(func() {
			panic("Oops...") // panic-proof
		}),
		Name("Thunderclap!"),
	)

	Gron(
		Every(1),
		Saturday(),
		Do(func() {
			time.Sleep(4)
			fmt.Println("wubba lubba dub dub")
		}),
		Name("Rick"),
	)

	// subscribe task events
	for ev := range Subscribe() {
		switch ev.E {
		case Disabled:
			fmt.Println("Disabled:", ev.TaskName, ev.At.Format("15:04:05"))
		case Failed:
			fmt.Println("Failed:", ev.TaskName, ev.Msg, ev.At.Format("15:04:05"))
			Remove(ev.TaskName)
		default:
			fmt.Println(ev.TaskName, ev.E, ev.At.Format("15:04:05"))
		}
	}
}

License

MIT

Documentation

Index

Constants

View Source
const (
	// Enabled emitted when a task is enabled
	Enabled etype = iota

	// Disabled emitted when a task has been disabled
	Disabled

	// Running emitted when a task is starting
	Running

	// Finished emitted when a task has finished successful
	Finished

	// Failed emitted when a task has failed
	Failed
)

Variables

This section is empty.

Functions

func Disable

func Disable(name TaskName)

Disable puts the task inactive

func Enable

func Enable(name TaskName) error

Enable puts the task active

func Gron

func Gron(ops ...Option) error

Gron is an user interface for declaring periodic task

func Remove

func Remove(name TaskName)

Remove removes an inactive task from the scheduler

func Subscribe

func Subscribe() <-chan Event

Subscribe returns the gron event channel

Types

type Event

type Event struct {
	TaskName
	E   etype
	Msg string
	At  time.Time
}

Event presents the task event

type Option

type Option func(task Task) (Task, error)

Option is a struct for friendly APIs See also: https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis

func At

func At(ts string) Option

At sets the specific time of a day Support one of the following spec:

  • HH:MM:SS
  • :MM:SS
  • ::SS

func Do

func Do(fn interface{}) Option

Do assigns the periodic task

func Every

func Every(n uint) Option

Every sets the interval value

func Friday

func Friday() Option

Friday makes sure that this task will be run on Friday

func Hour

func Hour() Option

Hour is a singular Hours

func Hours

func Hours() Option

Hours sets the interval unit

func Minute

func Minute() Option

Minute is a singular minutes

func Minutes

func Minutes() Option

Minutes sets the interval unit

func Monday

func Monday() Option

Monday makes sure that this task will be run on Monday

func Name

func Name(name string) Option

Name sets the identifier of the task

func Saturday

func Saturday() Option

Saturday makes sure that this task will be run on Saturday

func Second

func Second() Option

Second is a singular Seconds

func Seconds

func Seconds() Option

Seconds sets the interval unit

func Sunday

func Sunday() Option

Sunday makes sure that this task will be run on Sunday

func Thursday

func Thursday() Option

Thursday makes sure that this task will be run on Thursday

func Tuesday

func Tuesday() Option

Tuesday makes sure that this task will be run on Tuesday

func Wednesday

func Wednesday() Option

Wednesday makes sure that this task will be run on Wednesday

type Task

type Task struct {
	Name TaskName
	// contains filtered or unexported fields
}

Task is a periodic task

type TaskName

type TaskName string

TaskName is the name of the task

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL