gocron

package module
v0.0.0-...-9349a22 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2018 License: BSD-2-Clause Imports: 7 Imported by: 6

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.

Have fun!

Documentation

Overview

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/

Copyright 2014 Jason Lyu. [email protected] . 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 MAXJOBNUM = 10000

Max number of jobs, hack it if you need.

Variables

This section is empty.

Functions

func ChangeLoc

func ChangeLoc(newLocation *time.Location)

Change the time location

func Clear

func Clear()

Clear

func Remove

func Remove(j interface{})

Remove

func RunAll

func RunAll()

Run all jobs regardless if they are scheduled to run or not.

func RunAllwithDelay

func RunAllwithDelay(d int)

Run all the jobs with a delay in seconds

A delay of `delay` seconds is added between each job. This can help to distribute the system load generated by the jobs more evenly over time.

func RunPending

func RunPending()

Run all jobs that are scheduled to run

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

func Start

func Start() chan bool

Run all jobs that are scheduled to run

Types

type Job

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

func Every

func Every(interval uint64) *Job

Schedule a new periodic job

func NewJob

func NewJob(intervel uint64) *Job

Create a new job with the time interval.

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

s.Every(1).Day().At("10:30").Do(task) s.Every(1).Monday().At("10:30").Do(task)

func (*Job) Day

func (j *Job) Day() (job *Job)

Set the job's unit with day, which interval is 1

func (*Job) Days

func (j *Job) Days() *Job

Set the job's unit with days

func (*Job) Do

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

Specifies the jobFunc that should be called every time the job runs

func (*Job) Friday

func (j *Job) Friday() (job *Job)

Set the start day with friday

func (*Job) Hour

func (j *Job) Hour() (job *Job)

set the unit with hour, which interval is 1

func (*Job) Hours

func (j *Job) Hours() (job *Job)

Set the unit with hours

func (*Job) Minute

func (j *Job) Minute() (job *Job)

Set the unit with minute, which interval is 1

func (*Job) Minutes

func (j *Job) Minutes() (job *Job)

set the unit with minute

func (*Job) Monday

func (j *Job) Monday() (job *Job)

s.Every(1).Monday().Do(task) Set the start day with Monday

func (*Job) NextScheduledTime

func (j *Job) NextScheduledTime() time.Time

NextScheduledTime returns the time of when this job is to run next

func (*Job) Saturday

func (j *Job) Saturday() (job *Job)

Set the start day with saturday

func (*Job) Second

func (j *Job) Second() (job *Job)

Set the unit with second

func (*Job) Seconds

func (j *Job) Seconds() (job *Job)

Set the unit with seconds

func (*Job) Sunday

func (j *Job) Sunday() (job *Job)

Set the start day with sunday

func (*Job) Thursday

func (j *Job) Thursday() (job *Job)

Set the start day with thursday

func (*Job) Tuesday

func (j *Job) Tuesday() (job *Job)

Set the start day with Tuesday

func (*Job) Wednesday

func (j *Job) Wednesday() (job *Job)

Set the start day woth Wednesday

func (*Job) Weeks

func (j *Job) Weeks() *Job

Set the units as weeks

type Scheduler

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

Class Scheduler, the only data member is the list of jobs.

func NewScheduler

func NewScheduler() *Scheduler

Create a new scheduler

func (*Scheduler) Clear

func (s *Scheduler) Clear()

Delete all scheduled jobs

func (*Scheduler) Every

func (s *Scheduler) Every(interval uint64) *Job

Schedule a new periodic job

func (*Scheduler) Len

func (s *Scheduler) Len() int

func (*Scheduler) Less

func (s *Scheduler) Less(i, j int) bool

func (*Scheduler) NextRun

func (s *Scheduler) NextRun() (*Job, time.Time)

Datetime when the next job should run.

func (*Scheduler) Remove

func (s *Scheduler) Remove(j interface{})

Remove specific job j

func (*Scheduler) RunAll

func (s *Scheduler) RunAll()

Run all jobs regardless if they are scheduled to run or not

func (*Scheduler) RunAllwithDelay

func (s *Scheduler) RunAllwithDelay(d int)

Run all jobs with delay seconds

func (*Scheduler) RunPending

func (s *Scheduler) RunPending()

Run all the jobs that are scheduled to run.

func (*Scheduler) Start

func (s *Scheduler) Start() chan bool

Start all the pending jobs Add seconds ticker

func (*Scheduler) Swap

func (s *Scheduler) Swap(i, j int)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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