schedule

package module
v0.0.0-...-49afda7 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2018 License: Apache-2.0 Imports: 6 Imported by: 0

README

schedule: A Golang Job Scheduling Package.

GoDoc Go Report Card

schedule is a Golang job scheduling package which lets you run Go functions periodically at pre-determined interval using a simple, human-friendly syntax.

schedule can optionally use a mysql database to guarentee that each scheduled job only runs once per cluster of servers

schedule is inspired by the Ruby module clockwork and Python job scheduling package schedule

This package has been heavily inspired by the good, but rather buggy goCron package

See also this two great articles:

Back to this package, you could just use this simple API as below, to run a cron scheduler.

package main

import (
	"fmt"
	"github.com/marksalpeter/schedule"
)

func task(j Job, _ time.Time) {
	fmt.Printf("I run every %s %s\n", j.Amount(), j.Interval())
}

func main() {

	// add an example for every task
	now := time.Now()
	schedule.Add("once-task").Once().Starting(now).Do(task)
	schedule.Add("second-task").Every(1).Seconds().Starting(now).Do(task)
	schedule.Add("minute-task").Every(1).Minutes().Starting(now).Do(task)
	schedule.Add("hour-task").Every(1).Hours().Starting(now).Do(task)
	schedule.Add("day-task").Every(1).Days().At(now.Hours(), now.Minutes(), now.Seconds()).Starting(now).Do(task)
	schedule.Add("week-task").Every(1).Weeks().On(int(now.Weekday())).At(now.Hours(), now.Minutes(), now.Seconds()).Starting(now).Do(task)
	schedule.Add("month-task").Every(1).Months().In(now.Month()).On(now.Day()).At(now.Hours(), now.Minutes(), now.Seconds()).Starting(now).Do(task)
	schedule.Add("year-task").Every(1).Years().In(now.Month()).On(now.Day()).At(now.Hours(), now.Minutes(), now.Seconds()).Starting(now).Do(task)

	// you can see all of the jobs in the scheduler here
	fmt.Printf("%+v\n", schedule.List())

	select{}
}

Roadmap

schedule is in beta, but the api is very unlikely to change. here is what is needed fully releasable version 1

  • finish writing tests for all functionality of the non db synchronized local version
  • test for database synchronicity
  • bug fix for database synchronicity
  • examples for godoc
  • basic example in README.md
  • database example in README.md
  • set up vendor

Documentation

Overview

Package schedule is a golang job scheduling package

Schedule is a Golang job scheduling package which lets you run Go functions periodically at pre-determined interval using a simple, human-friendly syntax. Schedule can optionally use a mysql database to synchronize its jobscheduling across multiple server instances. Schedule is inspired by the Ruby module [clockwork](<https://github.com/tomykaira/clockwork>) and Python job scheduling package schedule(<https://github.com/dbader/schedule>). This package has been heavily inspired by the good, but rather buggy [goCron](https://github.com/jasonlvhit/gocron) package.

Index

Constants

View Source
const (
	// Once happens one time only
	Once = IntervalType("once")

	// Years is set if `Interval.Years` is called
	Years = IntervalType("years")

	// Months is set if `Interval.Months` is called
	Months = IntervalType("months")

	// Weeks is set if `Interval.Weeks` is called
	Weeks = IntervalType("weeks")

	// Days is set if `Interval.Days` is called
	Days = IntervalType("days")

	// Hours is set if `Interval.Hours` is called
	Hours = IntervalType("hours")

	// Minutes is set if `Interval.MinutesDaily` is called
	Minutes = IntervalType("minutes")

	// Seconds is set if `Interval.Seconds` is called
	Seconds = IntervalType("seconds")
)

Variables

View Source
var DefaultScheduler = New(&Config{Name: "default"})

DefaultScheduler is the `Scheduler“ referenced by the `Add` and `List` funcs

Functions

This section is empty.

Types

type Amount

type Amount interface {
	Every(i ...int) Interval
	Once() Starting
}

Amount determines the amount of some interval of time that will elapse between executions

func Add

func Add(name string) Amount

Add adds jobs to the `DefaultScheduler`

type Config

type Config struct {
	// Name is the name of the scheduler
	Name string

	// Database is the name of the mysql database used to synchronize the scheduler
	// If a database is not passed in, the scheduler will not use database synchronicity
	Database string

	// Instancs is the address of the database instance used to synchronize the scheduler
	Instance string

	// Username is the username of the mysql user
	Username string

	// Password is the password of the mysql user
	Password string

	// LogDB when set to true, all sql transactions will be logged
	LogDB bool
}

Config configures the scheduler

type Day

type Day interface {
	On(day int) Time
}

Day adds the day to the job

type Interval

type Interval interface {
	Years() Month
	Months() Day
	Weeks() Day
	Days() Time
	Hours() Starting
	Minutes() Starting
	Seconds() Starting
}

Interval determines the interval of time that will elapse between executions

type IntervalType

type IntervalType string

IntervalType is a string representation of the interval chosen by the `Interval` interface

func (*IntervalType) Scan

func (it *IntervalType) Scan(value interface{}) error

Scan implements `sql.Scanner`

func (IntervalType) Value

func (it IntervalType) Value() (driver.Value, error)

Value implements the `driver.Valuer` interface

type Job

type Job interface {
	// Name is the name of the job. It is unique to the scheduler that it is added to
	Name() string

	// Amount is the amount of some interval of time that will elapse between executions.
	// If there is only 1 execution of this task, it will be set to zero
	Amount() int

	// Interval is the interval of time that will elapse between executions
	Interval() IntervalType

	// Description is a plain english sentence that describes when this job is executed
	Description() string

	// Scheduler is the `Scheduler` that this job belongs to
	Scheduler() Scheduler
	// contains filtered or unexported methods
}

Job represents a task that is queued on the system at a certain time

func List

func List() []Job

List returns the jobs from the `DefaultScheuler`

type Month

type Month interface {
	In(time.Month) Day
}

Month adds the month to the job

type Scheduler

type Scheduler interface {
	// Name is the unique name of the scheduler. Note: any scheduler with the same name will reference the same table name for synchronicity purposes
	Name() string

	// List returs a list of jobs added to this scheduler
	List() []Job

	// Add create a new job ascociated with the scheduler and returns its first builder method
	// Note: it will not be added to the scheduler until it is done being built (ie `Do` is called)
	Add(name string) Amount

	// Start starts the scheduler
	Start()

	// Stop stops the scheduler
	Stop()
	// contains filtered or unexported methods
}

Scheduler executes a sets of `Jobs` at a given time

func New

func New(cfg *Config) Scheduler

New creates a new `Scheduler`

type Starting

type Starting interface {
	Starting(time.Time) Task
}

Starting set the time we start counting

type Task

type Task interface {
	Do(func(Job, time.Time)) error
}

Task adds the func that will be executed by the `Scheduler`. It is the final step in the `Job` builder methods.

type Time

type Time interface {
	At(hours, minutes, seconds int) Starting
}

Time sets the time that the job will execute

Jump to

Keyboard shortcuts

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