gocron

package module
Version: v0.0.0-...-27f9f25 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2017 License: BSD-2-Clause Imports: 5 Imported by: 0

README

goCron: A Golang Job Scheduling Package.

GoDoc Stories in Ready

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

goCron is a Golang implementation of Ruby module clockwork and Python job scheduling package schedule, and personally, this package is my first Golang program, just for fun and practice.

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/jasonlvhit/gocron"
)

func task() {
	fmt.Println("I am runnning task.")
}

func taskWithParams(a int, b string) {
	fmt.Println(a, b)
}

func main() {
	// Do jobs with params
	gocron.Every(1).Second().Do(taskWithParams, 1, "hello")

	// Do jobs without params
	gocron.Every(1).Second().Do(task)
	gocron.Every(2).Seconds().Do(task)
	gocron.Every(1).Minute().Do(task)
	gocron.Every(2).Minutes().Do(task)
	gocron.Every(1).Hour().Do(task)
	gocron.Every(2).Hours().Do(task)
	gocron.Every(1).Day().Do(task)
	gocron.Every(2).Days().Do(task)

	// Do jobs on specific weekday
	gocron.Every(1).Monday().Do(task)
	gocron.Every(1).Thursday().Do(task)

	// function At() take a string like 'hour:min'
	gocron.Every(1).Day().At("10:30").Do(task)
	gocron.Every(1).Monday().At("18:30").Do(task)

	// remove, clear and next_run
	_, time := gocron.NextRun()
	fmt.Println(time)

	gocron.Remove(task)
	gocron.Clear()

	// function Start start all the pending jobs
	<- gocron.Start()

	// also , you can create a your new scheduler,
	// to run two scheduler concurrently
	s := gocron.NewScheduler()
	s.Every(3).Seconds().Do(task)
	<- s.Start()

}

and full test cases and document will be coming soon.

Once again, thanks to the great works of Ruby clockwork and Python schedule package. BSD license is used, see the file License for detail.

Hava fun!

Documentation

Overview

Package gocron : A Golang Job Scheduling Package.

An in-process scheduler for periodic jobs that uses the builder pattern for configuration. Schedule lets you run Golang functions periodically at pre-determined intervals using a simple, human-friendly syntax.

Inspired by the Ruby module clockwork <https://github.com/tomykaira/clockwork> and Python package schedule <https://github.com/dbader/schedule>

See also http://adam.heroku.com/past/2010/4/13/rethinking_cron/ http://adam.heroku.com/past/2010/6/30/replace_cron_with_clockwork/

maintained by Mark Salpeter mark@dealyze.com

Copyright 2014 Jason Lyu. jasonlvhit@gmail.com . All rights reserved. Use of this source code is governed by a BSD-style . license that can be found in the LICENSE file.

Index

Constants

View Source
const (
	// Day is the duration for a Days worth of time
	Day = 24 * time.Hour

	// Week is the duration for a Weeks worth of time
	Week = 7 * Day
)

Variables

View Source
var (
	// ErrTaskIsNotAFuncError is the error panicked when a task passed to `Job.Do`
	ErrTaskIsNotAFuncError = errors.New("the `task` your a scheduling must be of type func")

	// ErrMissmatchedTaskParams is the error panicked when someone passes too many or too few params to `Job.Do`
	ErrMissmatchedTaskParams = errors.New("the `task` your a scheduling must be of type func")

	// ErrJobIsNotInitialized is the error panicked when a job is scheduled that was not initialized
	ErrJobIsNotInitialized = errors.New("this job was not intialized")

	// ErrIncorrectTimeFormat is the error panicked when `At` is passed an incorrect time
	ErrIncorrectTimeFormat = errors.New("the time format is incorrect")

	// ErrIntervalNotValid error panicked when the interval is not valid
	ErrIntervalNotValid = errors.New("the interval must be greater than 0")
)

Functions

func Clear

func Clear()

Clear removes all of the jobs from the

func IsRunning

func IsRunning() bool

IsRunning returns true if the default scheduler has started

func Remove

func Remove(j *Job)

Remove removes the job from the default scheduler

func RunAll

func RunAll()

RunAll runs all jobs of the regardless if they are scheduled to run or not.

func RunAllWithDelay

func RunAllWithDelay(d time.Duration)

RunAllWithDelay runs all of the jobs with a delay between each of them

This can help to distribute the system load generated by the jobs more evenly over time.

func RunPending

func RunPending()

RunAll Runs all of the jobs that are scheduled to run

Please note that it is *intended behavior that `RunPending()` does not run missed jobs*. For example, if you've registered a job that should run every minute and you only call `RunPending()` in one hour increments then your job will only be run once every hour

func Start

func Start()

Start starts the scheduler

func Stop

func Stop()

Stop stops the default scheduler

Types

type Job

type Job struct {
	// contains filtered or unexported fields
}

Job calculates the time intervals in which a task should be executed.

func Every

func Every(interval uint64) *Job

Every schedules a new job in the default scheduler

func NextRun

func NextRun() (job *Job, time time.Time)

NextRun gets the next running time

func (*Job) At

func (j *Job) At(t string) *Job

At adds a time component to daily or weekly recurring tasks.

note: if no time is specified, the `At` time will default to whenever `Schedule.Start()` is called

Example

 // ...
	Every(1).Day().At("10:30").Do(task)    // performs a task every day at 10:30 am
	Every(1).Monday().At("22:30").Do(task) // performs a task every Monday at 10:30 pm
	Every(1).Monday().Do(task)             // performs a task every Monday at whatever time `Schedule.Start()` is called

func (*Job) Day

func (j *Job) Day() *Job

Day is an alias for `Days`

func (*Job) Days

func (j *Job) Days() *Job

Days sets a task to run every `x` number of hours

Example

 // ...
	Every(5).Days().Do(task) // executes the task func every 5 days

func (*Job) Do

func (j *Job) Do(task interface{}, params ...interface{}) *Job

Do specifies the taks that should be called executed and the parameters it should be passed Example

 // ...
	job := Every(1).Day().At("10:30").Do(task, paramOne, "paramTwo")  // performs `task(paramOne, "paramTwo")` every day at 10:30 am
 job.Do(task2, paramThree, "paramFour")                            // `task2(paramThree, "paramFour")` will perperformed at the same interval

func (*Job) Friday

func (j *Job) Friday() *Job

Friday is an alias for `Weekday(time.Friday)`

func (*Job) Hour

func (j *Job) Hour() *Job

Hour is an alias for `Hours`

func (*Job) Hours

func (j *Job) Hours() *Job

Hours sets a task to run every `x` number of hours

Example

 // ...
	Every(5).Hours().Do(task) // executes the task func every 5 hours

func (*Job) Location

func (j *Job) Location(loc *time.Location) *Job

Location sets the timezone of the job. Jobs created by `NewJob(...)` have a default location of`time.Local`. Jobs created by `Scheduler.Every(...)` have a default timezone of whatever `Scheduler.Location(...)` is set to.

Example

// ...
est, err := time.LoadLocation("America/New_York")
if err != nil { // you probably haven't set up your server correctly ;)
	panic(err)
}
Every(2).Monday().At("05:00").Location(est).Do(task) // executes the task every monday at 5:00 am eastern standard time

func (*Job) Minute

func (j *Job) Minute() *Job

Minute is an alias for `Minutes`

func (*Job) Minutes

func (j *Job) Minutes() *Job

Minutes sets a job to run every `x` number of minutes

Example

 // ...
	Every(5).Minutes().Do(task) // executes the task func every 5 minutes

func (*Job) Monday

func (j *Job) Monday() *Job

Monday is an alias for `Weekday(time.Monday)`

func (*Job) Saturday

func (j *Job) Saturday() *Job

Saturday is an alias for `Weekday(time.Saturday)`

func (*Job) Second

func (j *Job) Second() *Job

Second is an alias for `Seconds`

func (*Job) Seconds

func (j *Job) Seconds() *Job

Seconds sets a job to run every `x` number of seconds

Example

 // ...
	Every(5).Seconds().Do(task) // executes the task func every 5 seconds

func (*Job) Sunday

func (j *Job) Sunday() *Job

Sunday is an alias for `Weekday(time.Sunday)`

func (*Job) Thursday

func (j *Job) Thursday() *Job

Thursday is an alias for `Weekday(time.Thursday)`

func (*Job) Tuesday

func (j *Job) Tuesday() *Job

Tuesday is an alias for `Weekday(time.Tuesday)`

func (*Job) Wednesday

func (j *Job) Wednesday() *Job

Wednesday is an alias for `Weekday(time.Wednesday)`

func (*Job) Week

func (j *Job) Week() *Job

Week is an alias for `Weekday(time.Now().Weekday())`

func (*Job) Weekday

func (j *Job) Weekday(weekday time.Weekday) *Job

Weekday sets the task to be performed on a certian day of the week

Example

// ...
scheduler.Every(1).Weekday(time.Sunday).Do(task) // executes the task once every Sunday at whatever time `Scheduler.Start()` is called
scheduler.Every(2).Weekday(time.Monday).At("05:00").Do(task) // executes the task every other Monday at 7 am

func (*Job) Weeks

func (j *Job) Weeks() *Job

Weeks is an alias for `Weekday(time.Now().Weekday())`

type Scheduler

type Scheduler interface {
	// Start starts the scheduler
	Start()

	// IsRunning returns true if the job  has started
	IsRunning() bool

	// Stop stops the scheduler from executing jobs
	Stop()

	// Every creates a new job, and adds it to the `Scheduler`
	Every(interval uint64) *Job

	// Remove removes an individual job from the scheduler. It returns true if the job was found and removed from the `Scheduler`
	Remove(*Job) bool

	// Clear removes all of the jobs that have been added to the scheduler
	Clear()

	// Location sets the default location of every job created with `Every`.
	// The default location is `time.Local`
	Location(*time.Location)

	// NextRun returns the next next job to be run and the time in which it will be run
	NextRun() (*Job, time.Time)

	// RunAll runs all of the jobs regardless of wether or not they are pending
	RunAll()

	// RunPending runs all of the pending jobs
	RunPending()

	// RunAllWithDelay runs all of the jobs regardless of wether or not they are pending with a delay
	RunAllWithDelay(time.Duration)

	//Add job to the scheduler
	Add(*Job) bool
}

Scheduler keeps a slice of jobs that it executes at a regular interval

func NewScheduler

func NewScheduler() Scheduler

NewScheduler create a new scheduler. Note: the current implementation is not concurrency safe.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL