scheduler

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2019 License: BSD-2-Clause Imports: 7 Imported by: 12

README

scheduler

build status codecov godoc license

中文文档

Introduction

scheduler is a job scheduling package for Go. It provides a simple, humans-friendly way to schedule the execution of the go function and includes delay and periodic.

Inspired by Linux cron and Python schedule.

Features

  • Delay execution, accurate to a second
  • Periodic execution, accurate to a second, like the cron style but more flexible
  • Cancel job
  • Failure retry

Installation

go get github.com/prprprus/scheduler

Example

job function

func task1(name string, age int) {
	fmt.Printf("run task1, with arguments: %s, %d\n", name, age)
}

func task2() {
	fmt.Println("run task2, without arguments")
}
Delay

Delayed supports four modes: seconds, minutes, hours, and days.

package main

import (
    "fmt"

    "github.com/prprprus/scheduler"
)

func main() {
	s, err := scheduler.NewScheduler(1000)
	if err != nil {
		panic(err) // just example
	}

	// delay with 1 second, job function with arguments
	jobID := s.Delay().Second(1).Do(task1, "prprprus", 23)

	// delay with 1 minute, job function without arguments
	jobID = s.Delay().Minute(1).Do(task2)

	// delay with 1 hour
	jobID = s.Delay().Hour(1).Do(task2)

	// Note: execute immediately
	jobID = s.Delay().Do(task2)

	// cancel job
	jobID = s.Delay().Day(1).Do(task2)
	err = s.CancelJob(jobID)
	if err != nil {
		panic(err)
	} else {
		fmt.Println("cancel delay job success")
	}
}
Every

Like the cron style, it also includes seconds, minutes, hours, days, weekday, and months, but the order and number are not fixed. You can freely arrange and combine them according to your own preferences. For example, the effects of Second(3).Minute(35).Day(6) and Minute(35).Day(6).Second(3) are the same. No need to remember the format! 🎉👏

But for the readability, recommend the chronological order from small to large (or large to small).

Note: Day() and Weekday() avoid simultaneous occurrences unless you know that the day is the day of the week.

package main

import (
    "fmt"

    "github.com/prprprus/scheduler"
)

func main() {
	s, err := scheduler.NewScheduler(1000)
	if err != nil {
		panic(err) // just example
	}

	// Specifies time to execute periodically
	jobID = s.Every().Second(45).Minute(20).Hour(13).Day(23).Weekday(3).Month(6).Do(task1, "prprprus", 23)
	jobID = s.Every().Second(15).Minute(40).Hour(16).Weekday(4).Do(task2)
	jobID = s.Every().Second(1).Do(task1, "prprprus", 23)

	// Note: execute immediately
	jobID = s.Every().Do(task2)

	jobID = s.Every().Second(1).Minute(1).Hour(1).Do(task2)
	err = s.CancelJob(jobID)
	if err != nil {
		panic(err)
	} else {
		fmt.Println("cancel periodically job success")
	}
}

Documentation

Full documentation

Contribution

Thank you for your interest in contribution of scheduler, your help and contribution is very valuable.

You can submit issue and pull requests and fork, please submit an issue before submitting pull requests.

License

See LICENSE for more information.

have fun✨👻✨

Documentation

Overview

Package scheduler provides a simple, humans-friendly way to schedule the execution of the go function. It includes delay execution and periodic execution.

Copyright (c) 2019, prprprus 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 (
	// Delay represents job type, the job will be delayed execute once according to job sched
	Delay = "Delay"

	// Every represents job type, the job will be cycled execute according to job sched
	Every = "Every"

	// Key of job sched
	Second  = "Second"
	Minute  = "Minute"
	Hour    = "Hour"
	Day     = "Day"
	Weekday = "Weekday"
	Month   = "Month"

	// EveryRune is value of job sched, like "*" in cron, represents every
	// second/minute/hour/day/weekday/month.
	EveryRune = -1
)

Variables

View Source
var (

	// ErrPendingJob is returned when the pending job not exist
	ErrPendingJob = errors.New("pending job not exist")

	// ErrOverlength is returned when the job size over maxJobSetSize variable
	ErrOverlength = errors.New("job set size overlength")

	// ErrJobType is returned when the job type not exist,
	// job type is one of Delay and Every.
	ErrJobType = errors.New("job type not exist")

	// ErrJobSched is returned when the job sched not exist,
	// under normal circumstances, this error will not occur,
	// unless the key definition of job sched is incorrectly modified.
	ErrJobSched = errors.New("job sched not exist")

	// ErrTimeNegative is returned when the time argument is negative
	ErrTimeNegative = errors.New("time argument can not be negative")

	// ErrDupJobID is returned when the generateID generates the same id
	ErrDupJobID = errors.New("Duplicate job id")

	// ErrAlreadyComplayed is returned when cancel a completed job
	ErrAlreadyComplayed = errors.New("Job hash been completed")

	// ErrCancelJob is returned when time.Timer.Stop function occur error
	ErrCancelJob = errors.New("cancel job failed")

	// ErrRangeSecond is returned when Second method argument is not int
	ErrRangeSecond = errors.New("argument 0 <= n <= 59 in Second method")

	// ErrRangeMinute is returned when Minute method argument is not int
	ErrRangeMinute = errors.New("argument 0 <= n <= 59 in Minute method")

	// ErrRangeHour is returned when Hour method argument is not int
	ErrRangeHour = errors.New("argument 0 <= n <= 23 in Hour method")

	// ErrRangeDay is returned when Day method argument is not int
	ErrRangeDay = errors.New("argument 1 <= n <= 31 in Day method")

	// ErrRangeWeekday is returned when Weekday method argument is not int
	ErrRangeWeekday = errors.New("argument 0 <= n <= 6 in Weekday method")

	// ErrRangeMonth is returned when Month method argument is not int
	ErrRangeMonth = errors.New("argument 1 <= n <= 12 in Month method")

	// EmptyJobType represents an empty job type
	EmptyJobType = ""

	// EmptySched represents an empty job sched
	EmptySched = map[string]int{}
)

Functions

func InitJobSched

func InitJobSched(jobType string) map[string]int

InitJobSched initiate job sched by job type

Types

type Job

type Job struct {
	ID   string // unique id
	Type string // job type

	// Sched is a job sched, like cron style but the order of time is not
	// fixed, can be arranged and combined at will.
	Sched map[string]int

	JTimer  *JobTimer  // JobTimer
	JTicker *JobTicker // JobTicker
	// contains filtered or unexported fields
}

Job is an abstraction of a scheduling task.

func (*Job) Day

func (j *Job) Day(n int) *Job

Day method set Day key for job sched.

func (*Job) Do

func (j *Job) Do(fn interface{}, args ...interface{}) (jobID string)

Do according to the job type and job sched execute job.

func (*Job) Hour

func (j *Job) Hour(n int) *Job

Hour method set Hour key for job sched.

func (*Job) Minute

func (j *Job) Minute(n int) *Job

Minute method set Minute key for job sched.

func (*Job) Month

func (j *Job) Month(n int) *Job

Month method set Month key for job sched.

func (*Job) Second

func (j *Job) Second(n int) *Job

Second method set Second key for job sched.

func (*Job) Weekday

func (j *Job) Weekday(n int) *Job

Weekday method set Weekday key for job sched.

type JobSet

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

JobSet stores pending jobs and completed jobs and it is concurrent safly.

type JobTicker

type JobTicker struct {
	ID string // unique id
	// contains filtered or unexported fields
}

JobTicker is the wrapper for time.Ticker, one job corresponds to a JobTicker.

type JobTimer

type JobTimer struct {
	ID string // unique id
	// contains filtered or unexported fields
}

JobTimer is the wrapper for time.Timer, one job corresponds to a JobTimer.

type Scheduler

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

Scheduler is responsible for scheduling jobs.

func NewScheduler

func NewScheduler(jss int) (*Scheduler, error)

NewScheduler new Scheduler instance.

func (*Scheduler) CancelJob

func (s *Scheduler) CancelJob(id string) error

CancelJob can cancel the job before scheduling.

func (*Scheduler) Delay

func (s *Scheduler) Delay() *Job

Delay method schedule job with Delay mode.

func (*Scheduler) Every

func (s *Scheduler) Every() *Job

Every method schedule job with Every mode.

func (*Scheduler) JobDone

func (s *Scheduler) JobDone(id string) (bool, error)

JobDone Check if the job is completed.

func (*Scheduler) JobSched

func (s *Scheduler) JobSched(id string) (map[string]int, error)

JobSched get job sched.

func (*Scheduler) JobType

func (s *Scheduler) JobType(id string) (string, error)

JobType get job type.

func (*Scheduler) PendingJob

func (s *Scheduler) PendingJob(id string) (*Job, error)

PendingJob get pending job by id.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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