scheduler

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2023 License: GPL-3.0 Imports: 8 Imported by: 0

README

Scheduler

Go Reference

Scheduler package is a zero-dependency scheduling library for Go

Install

go get -u github.com/GoFarsi/scheduler

Features

  • Scheduling your functions
  • In a scheduler instance, you can run more than one thousand jobs at a time (Max Job is 10.000)
  • In the form of Safely, you can run your jobs and if a panic occurs, your jobs will be recovered and reported to the console (func (j *Job) DoJobSafely(jobFunction interface{}, params ...interface{}) error)
  • Multiple scheduler instances can be run simultaneously

Example

More Example in

package main

import (
	"fmt"
	"github.com/GoFarsi/scheduler"
)

var (
	Sched = Scheduler.NewScheduler()
)

func main() {
	if err := Sched.Every(5).Second().Do(Greeting); err != nil {
		panic(err)
	}

	<-Sched.Start()
}

func Greeting() {
	fmt.Println("Hello, World!")
}

Contributing

We'd love to see your contribution to the scheduler! you can contribute by following these steps :

  1. Fork the repository
  2. Create a new branch
  3. Make your changes
  4. Commit your changes
  5. Push your changes to the remote repository
  6. Create a pull request

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChangeTimeLocation

func ChangeTimeLocation(location *time.Location)

ChangeTimeLocation change the location of a time

func Clear

func Clear()

Clear all scheduled jobs

func Remove

func Remove(j interface{})

Remove a specific job

func RunAll

func RunAll()

RunAll runs all jobs

func RunAllWithDelay

func RunAllWithDelay(d int)

RunAllWithDelay runs all jobs with a delay

func RunPending

func RunPending()

RunPending runs all pending jobs

func Scheduled

func Scheduled(j interface{}) bool

Scheduled checks if specific job j was already added

func SetLocker

func SetLocker(l Locker)

SetLocker sets a locker implementation

func Start

func Start() chan bool

Start run all jobs that are scheduled to run

Types

type Job

type Job struct {
	JobFunction  string
	Functions    map[string]interface{}
	FuncParams   map[string][]interface{}
	Interval     uint64
	JobUnit      types.TimeUnit
	Tags         []string
	AtTime       time.Duration
	TimeLocation *time.Location
	LastRun      time.Time
	NextRun      time.Time
	FirstWeekDay time.Weekday
	JobError     error
	LockJob      bool
}

Job information about a job

func Every

func Every(interval uint64) *Job

Every schedules a new periodic job running in specific interval

func Jobs

func Jobs() []*Job

Jobs returns the list of job from the defaultScheduler

func NewJob

func NewJob(interval uint64) *Job

NewJob creates a new job

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 schedules the job to run at the given time

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

func (*Job) Day

func (j *Job) Day() *Job

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

func (*Job) Days

func (j *Job) Days() *Job

Days set the unit with days

func (*Job) Do

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

Do specify the jobFunc that should be executed every time the job runs

func (*Job) DoJobSafely

func (j *Job) DoJobSafely(jobFunction interface{}, params ...interface{}) error

DoJobSafely does the same thing as Do, except it logs unexpected panics rather than unwinding them up the chain

func (*Job) Err

func (j *Job) Err() error

Err check for errors when creating the job to make sure none occurred

func (*Job) Friday

func (j *Job) Friday() *Job

Friday sets the job start day friday

func (*Job) From

func (j *Job) From(t *time.Time) *Job

From plans the next run of the job

func (*Job) GetAtTime

func (j *Job) GetAtTime() string

GetAtTime returns the time of day that the job will run s.Every(1).Day().At("20:30").GetAtTime() == "20:30"

func (*Job) GetWeekday

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

GetWeekday this returns the day of the week that the job will run Only use this when .Weekday(...) is called on the job.

func (*Job) Hour

func (j *Job) Hour() *Job

Hour sets the unit with hour, which interval is 1

func (*Job) Hours

func (j *Job) Hours() *Job

Hours set the unit with hours

func (*Job) JobIsRunNow

func (j *Job) JobIsRunNow() bool

JobIsRunNow runs the job now

func (*Job) Location

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

Location it set the location at which to interpret "At" s.Every(1).Day().At("20:30").Loc(time.UTC).Do(task)

func (*Job) Lock

func (j *Job) Lock() *Job

Lock prevents the job from running multi instances

func (*Job) Minute

func (j *Job) Minute() *Job

Minute sets the unit with minute, which interval is 1

func (*Job) Minutes

func (j *Job) Minutes() *Job

Minutes set the unit with minutes

func (*Job) Monday

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

Monday set the start day with monday - s.Every(1).Monday().Do(task)

func (*Job) Month

func (j *Job) Month() *Job

Month sets the job's unit with week, which interval is 1

func (*Job) Months

func (j *Job) Months() *Job

Months set the unit with months

func (*Job) MustInterval

func (j *Job) MustInterval(i uint64) error

MustInterval set the job's unit with seconds,minutes,hours...

func (*Job) NextJobRun

func (j *Job) NextJobRun() error

NextJobRun sets the next run time for the job

func (*Job) NextScheduledTime

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

NextScheduledTime returns the next scheduled time of a job

func (*Job) PeriodDuration

func (j *Job) PeriodDuration() (time.Duration, error)

PeriodDuration returns the duration of the job

func (*Job) RoundToMidNight

func (j *Job) RoundToMidNight(t time.Time) time.Time

RoundToMidNight it set the job to run at the beginning of the next day

func (*Job) Run

func (j *Job) Run() ([]reflect.Value, error)

Run the job and reschedule it

func (*Job) Saturday

func (j *Job) Saturday() *Job

Saturday sets the job start day saturday

func (*Job) Second

func (j *Job) Second() *Job

Second sets the unit with second

func (*Job) Seconds

func (j *Job) Seconds() *Job

Seconds set the unit with seconds

func (*Job) SetJobUnit

func (j *Job) SetJobUnit(unit types.TimeUnit) *Job

SetJobUnit sets the JobUnit

func (*Job) Sunday

func (j *Job) Sunday() *Job

Sunday sets the job start day sunday

func (*Job) Tag

func (j *Job) Tag(t string, others ...string)

Tag adds a labels for a job

func (*Job) TagList

func (j *Job) TagList() []string

TagList returns the tags of a job

func (*Job) Thursday

func (j *Job) Thursday() *Job

Thursday sets the job start day thursday

func (*Job) Tuesday

func (j *Job) Tuesday() *Job

Tuesday sets the job start day tuesday

func (*Job) UnTag

func (j *Job) UnTag(t string)

UnTag removes a tag from a job

func (*Job) Wednesday

func (j *Job) Wednesday() *Job

Wednesday sets the job start day wednesday

func (*Job) Week

func (j *Job) Week() *Job

Week sets the job's unit with week, which interval is 1

func (*Job) Weekday

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

Weekday start job on specific weekday

func (*Job) Weeks

func (j *Job) Weeks() *Job

Weeks set the unit with weeks

type Locker

type Locker interface {
	Lock(string) (bool, error)
	Unlock(string) error
}

type Scheduler

type Scheduler struct {
	JobList  [types.MaxJobs]*Job // Jobs is the array of jobs
	JobSize  int                 // JobSize is the number of jobs
	Location *time.Location      // Location is the location of the scheduler
}

Scheduler is the main struct of the scheduler

func New

func New() *Scheduler

New creates a new scheduler

Example
greetingFunc := func() {
	fmt.Println("Hello, World!")
}

sched := New()

if err := sched.Every(5).Second().Do(greetingFunc); err != nil {
	panic(err)
}

<-sched.Start()
Output:

func (*Scheduler) ChangeLocation

func (s *Scheduler) ChangeLocation(newLocation *time.Location)

ChangeLocation changes the default time location of the scheduler

func (*Scheduler) Clear

func (s *Scheduler) Clear()

Clear removes all scheduled jobs

func (*Scheduler) Every

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

Every schedules a new job to run every duration

func (*Scheduler) GetRunnableJobs

func (s *Scheduler) GetRunnableJobs() (runningJobs [types.MaxJobs]*Job, n int)

GetRunnableJobs returns a list of jobs that are ready to run

func (*Scheduler) Jobs

func (s *Scheduler) Jobs() []*Job

Jobs returns list of jobs from scheduler

func (*Scheduler) Len

func (s *Scheduler) Len() int

Len returns the number of jobs in the scheduler

func (*Scheduler) Less

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

Less returns true if the job at index i is less than the job at index j

func (*Scheduler) NextRun

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

NextRun returns the next run time of the job at index i

func (*Scheduler) Remove

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

Remove job by function

func (*Scheduler) RemoveByCondition

func (s *Scheduler) RemoveByCondition(remove func(*Job) bool)

RemoveByCondition removes specific job j by condition

func (*Scheduler) RemoveByRef

func (s *Scheduler) RemoveByRef(j *Job)

RemoveByRef removes specific job j by reference

func (*Scheduler) RemoveByTag

func (s *Scheduler) RemoveByTag(t string)

RemoveByTag removes specific job j by tag

func (*Scheduler) RunAll

func (s *Scheduler) RunAll()

RunAll runs all jobs

func (*Scheduler) RunAllWithDelay

func (s *Scheduler) RunAllWithDelay(d int)

RunAllWithDelay runs all jobs with a delay

func (*Scheduler) RunPending

func (s *Scheduler) RunPending()

RunPending runs all pending jobs

func (*Scheduler) Scheduled

func (s *Scheduler) Scheduled(j interface{}) bool

Scheduled returns true if job j is scheduled

func (*Scheduler) Start

func (s *Scheduler) Start() chan bool

Start starts all pending jobs

func (*Scheduler) Swap

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

Swap swaps the two jobs

Directories

Path Synopsis
_example

Jump to

Keyboard shortcuts

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