gcron

package
v1.16.8 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2022 License: MIT Imports: 15 Imported by: 14

Documentation

Overview

Package gcron implements a cron pattern parser and job runner.

Example (CronAddSingleton)
package main

import (
	"time"

	"github.com/gogf/gf/os/gcron"
	"github.com/gogf/gf/os/glog"
)

func main() {
	gcron.AddSingleton("* * * * * *", func() {
		glog.Println("doing")
		time.Sleep(2 * time.Second)
	})
	select {}
}
Output:

Index

Examples

Constants

View Source
const (
	StatusReady   = gtimer.StatusReady
	StatusRunning = gtimer.StatusRunning
	StatusStopped = gtimer.StatusStopped
	StatusClosed  = gtimer.StatusClosed
)

Variables

This section is empty.

Functions

func DelayAdd

func DelayAdd(delay time.Duration, pattern string, job func(), name ...string)

DelayAdd adds a timed task to default cron object after `delay` time.

func DelayAddOnce

func DelayAddOnce(delay time.Duration, pattern string, job func(), name ...string)

DelayAddOnce adds a timed task after `delay` time to default cron object. This timed task can be run only once.

func DelayAddSingleton

func DelayAddSingleton(delay time.Duration, pattern string, job func(), name ...string)

DelayAddSingleton adds a singleton timed task after `delay` time to default cron object.

func DelayAddTimes

func DelayAddTimes(delay time.Duration, pattern string, times int, job func(), name ...string)

DelayAddTimes adds a timed task after `delay` time to default cron object. This timed task can be run specified times.

func GetLogLevel

func GetLogLevel() int

GetLogLevel returns the logging level for default cron object. Deprecated, use GetLogger instead.

func GetLogPath

func GetLogPath() string

GetLogPath returns the logging folder path of default cron object. Deprecated, use GetLogger instead.

func GetLogger added in v1.16.6

func GetLogger() *glog.Logger

GetLogger returns the logger in the cron.

func Remove

func Remove(name string)

Remove deletes scheduled task which named `name`.

func SetLogLevel

func SetLogLevel(level int)

SetLogLevel sets the logging level for default cron object. Deprecated, use SetLogger instead.

func SetLogPath

func SetLogPath(path string)

SetLogPath sets the logging folder path for default cron object. Deprecated, use SetLogger instead.

func SetLogger added in v1.16.6

func SetLogger(logger *glog.Logger)

SetLogger sets the logger for cron.

func Size

func Size() int

Size returns the size of the timed tasks of default cron.

func Start

func Start(name ...string)

Start starts running the specified timed task named `name`. If no`name` specified, it starts the entire cron.

func Stop

func Stop(name ...string)

Stop stops running the specified timed task named `name`. If no`name` specified, it stops the entire cron.

Types

type Cron

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

func New

func New() *Cron

New returns a new Cron object with default settings.

func (*Cron) Add

func (c *Cron) Add(pattern string, job func(), name ...string) (*Entry, error)

Add adds a timed task. A unique `name` can be bound with the timed task. It returns and error if the `name` is already used.

func (*Cron) AddEntry added in v1.16.6

func (c *Cron) AddEntry(pattern string, job func(), times int, singleton bool, name ...string) (*Entry, error)

AddEntry creates and returns a new Entry object.

func (*Cron) AddOnce

func (c *Cron) AddOnce(pattern string, job func(), name ...string) (*Entry, error)

AddOnce adds a timed task which can be run only once. A unique `name` can be bound with the timed task. It returns and error if the `name` is already used.

func (*Cron) AddSingleton

func (c *Cron) AddSingleton(pattern string, job func(), name ...string) (*Entry, error)

AddSingleton adds a singleton timed task. A singleton timed task is that can only be running one single instance at the same time. A unique `name` can be bound with the timed task. It returns and error if the `name` is already used.

func (*Cron) AddTimes

func (c *Cron) AddTimes(pattern string, times int, job func(), name ...string) (*Entry, error)

AddTimes adds a timed task which can be run specified times. A unique `name` can be bound with the timed task. It returns and error if the `name` is already used.

func (*Cron) Close

func (c *Cron) Close()

Close stops and closes current cron.

func (*Cron) DelayAdd

func (c *Cron) DelayAdd(delay time.Duration, pattern string, job func(), name ...string)

DelayAdd adds a timed task after `delay` time.

func (*Cron) DelayAddEntry added in v1.16.6

func (c *Cron) DelayAddEntry(delay time.Duration, pattern string, job func(), times int, singleton bool, name ...string)

DelayAddEntry adds a timed task after `delay` time.

func (*Cron) DelayAddOnce

func (c *Cron) DelayAddOnce(delay time.Duration, pattern string, job func(), name ...string)

DelayAddOnce adds a timed task after `delay` time. This timed task can be run only once.

func (*Cron) DelayAddSingleton

func (c *Cron) DelayAddSingleton(delay time.Duration, pattern string, job func(), name ...string)

DelayAddSingleton adds a singleton timed task after `delay` time.

func (*Cron) DelayAddTimes

func (c *Cron) DelayAddTimes(delay time.Duration, pattern string, times int, job func(), name ...string)

DelayAddTimes adds a timed task after `delay` time. This timed task can be run specified times.

func (*Cron) Entries

func (c *Cron) Entries() []*Entry

Entries return all timed tasks as slice(order by registered time asc).

func (*Cron) GetLogLevel

func (c *Cron) GetLogLevel() int

GetLogLevel returns the logging level. Deprecated, use GetLogger instead.

func (*Cron) GetLogPath

func (c *Cron) GetLogPath() string

GetLogPath return the logging folder path. Deprecated, use GetLogger instead.

func (*Cron) GetLogger added in v1.16.6

func (c *Cron) GetLogger() *glog.Logger

GetLogger returns the logger in the cron.

func (*Cron) Remove

func (c *Cron) Remove(name string)

Remove deletes scheduled task which named `name`.

func (*Cron) Search

func (c *Cron) Search(name string) *Entry

Search returns a scheduled task with the specified `name`. It returns nil if not found.

func (*Cron) SetLogLevel

func (c *Cron) SetLogLevel(level int)

SetLogLevel sets the logging level. Deprecated, use SetLogger instead.

func (*Cron) SetLogPath

func (c *Cron) SetLogPath(path string)

SetLogPath sets the logging folder path. Deprecated, use SetLogger instead.

func (*Cron) SetLogger added in v1.16.6

func (c *Cron) SetLogger(logger *glog.Logger)

SetLogger sets the logger for cron.

func (*Cron) Size

func (c *Cron) Size() int

Size returns the size of the timed tasks.

func (*Cron) Start

func (c *Cron) Start(name ...string)

Start starts running the specified timed task named `name`. If no`name` specified, it starts the entire cron.

func (*Cron) Stop

func (c *Cron) Stop(name ...string)

Stop stops running the specified timed task named `name`. If no`name` specified, it stops the entire cron.

type Entry

type Entry struct {
	Name string    // Entry name.
	Job  func()    `json:"-"` // Callback function.
	Time time.Time // Registered time.
	// contains filtered or unexported fields
}

Entry is timing task entry.

func Add

func Add(pattern string, job func(), name ...string) (*Entry, error)

Add adds a timed task to default cron object. A unique `name` can be bound with the timed task. It returns and error if the `name` is already used.

func AddOnce

func AddOnce(pattern string, job func(), name ...string) (*Entry, error)

AddOnce adds a timed task which can be run only once, to default cron object. A unique `name` can be bound with the timed task. It returns and error if the `name` is already used.

func AddSingleton

func AddSingleton(pattern string, job func(), name ...string) (*Entry, error)

AddSingleton adds a singleton timed task, to default cron object. A singleton timed task is that can only be running one single instance at the same time. A unique `name` can be bound with the timed task. It returns and error if the `name` is already used.

func AddTimes

func AddTimes(pattern string, times int, job func(), name ...string) (*Entry, error)

AddTimes adds a timed task which can be run specified times, to default cron object. A unique `name` can be bound with the timed task. It returns and error if the `name` is already used.

func Entries

func Entries() []*Entry

Entries return all timed tasks as slice.

func Search(name string) *Entry

Search returns a scheduled task with the specified `name`. It returns nil if no found.

func (*Entry) Close

func (entry *Entry) Close()

Close stops and removes the entry from cron.

func (*Entry) IsSingleton

func (entry *Entry) IsSingleton() bool

IsSingleton return whether this entry is a singleton timed task.

func (*Entry) SetSingleton

func (entry *Entry) SetSingleton(enabled bool)

SetSingleton sets the entry running in singleton mode.

func (*Entry) SetStatus

func (entry *Entry) SetStatus(status int) int

SetStatus sets the status of the entry.

func (*Entry) SetTimes

func (entry *Entry) SetTimes(times int)

SetTimes sets the times which the entry can run.

func (*Entry) Start

func (entry *Entry) Start()

Start starts running the entry.

func (*Entry) Status

func (entry *Entry) Status() int

Status returns the status of entry.

func (*Entry) Stop

func (entry *Entry) Stop()

Stop stops running the entry.

Jump to

Keyboard shortcuts

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