src

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	// DefaultGloballyUnique
	// default to disable globally unique.
	DefaultGloballyUnique = false

	// DefaultNodeUnique
	// default to enabled node unique.
	DefaultNodeUnique = true

	// DefaultRunOnStartup
	// default to disable run for crontab startup.
	DefaultRunOnStartup = false
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cron

type Cron interface {
	// Add
	// a worker into crontab.
	Add(worker *Worker) error

	GetWorker(class string) *Worker
	GetWorkers() map[string]*Worker
	Process() process.Process
	Start(ctx context.Context) error
	Started() bool
	Stop()
	Stopped() bool
}

Cron is an interface of Crontab.

type Crontab

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

Crontab is a scheduler for crontab. It's worked as a provider for process.

func NewCrontab

func NewCrontab(name string) *Crontab

NewCrontab creates a new crontab instance.

Example
crontab := NewCrontab("example")
println("crontab:", crontab.GetName())
Output:

crontab: example

func (*Crontab) Add

func (o *Crontab) Add(worker *Worker) error

Add adds a worker into crontab.

func (*Crontab) After

func (o *Crontab) After(_ context.Context) (err error)

After wait all running scheduler done before stopped. It's called by process, do not call it directly.

func (*Crontab) Del

func (o *Crontab) Del(worker *Worker)

Del deletes a worker from crontab.

func (*Crontab) DelByClass

func (o *Crontab) DelByClass(class string)

DelByClass deletes a worker from crontab by class name.

func (*Crontab) GetName

func (o *Crontab) GetName() string

GetName returns the crontab name.

func (*Crontab) GetWorker

func (o *Crontab) GetWorker(class string) *Worker

GetWorker returns the worker with given class.

func (*Crontab) GetWorkers

func (o *Crontab) GetWorkers() map[string]*Worker

GetWorkers returns the added worker mapping.

func (*Crontab) Process

func (o *Crontab) Process() process.Process

Process returns a process of the crontab.

func (*Crontab) Run

func (o *Crontab) Run(ctx context.Context) (err error)

Run crontab service. It's called by process, do not call it directly.

func (*Crontab) Start

func (o *Crontab) Start(ctx context.Context) (err error)

Start starts the crontab service by call process.

Example
var (
	crontab *Crontab
	ctx     = context.Background()
	err     error
	worker  *Worker
)

go func() {
	time.Sleep(time.Second * 5)

	if err == nil {
		crontab.Stop()
	}
}()

worker = NewWorker(EveryMinute, &ej{})
crontab = NewCrontab("example")
if err = crontab.Add(worker); err == nil {
	err = crontab.Start(ctx)
}
Output:

func (*Crontab) Started

func (o *Crontab) Started() bool

Started return true if process started.

func (*Crontab) Stop

func (o *Crontab) Stop()

Stop stops the crontab service by call process.

func (*Crontab) Stopped

func (o *Crontab) Stopped() bool

Stopped return true if process stopped.

type Error

type Error struct {
	Type           string
	Message, Value string
}

func (Error) Error

func (o Error) Error() string

type Job

type Job interface {
	OnRun(ctx context.Context) (err error)
}

Job is a required handler of a job, it's called by worker. If an error is returned then call JobFailed.OnFailed() otherwise JobSucceed.OnSucceed() called.

Called sequence:

  1. OnBefore
  2. Do
  3. OnFailed / OnSucceed
  4. OnFinish

Code:

func (o *YourJob) OnRun(ctx context.Context) (err error){
    // ...
    return
}

type JobBefore

type JobBefore interface {
	OnBefore(ctx context.Context) (err error)
}

JobBefore is an optional handler of a job, it's called by worker. If an error returned then quit other handlers otherwise Job.OnRun() called.

func (o *YourJob) OnBefore(ctx context.Context) (err error){
    // ...
    return
}

type JobFailed

type JobFailed interface {
	OnFailed(ctx context.Context, err error)
}

JobFailed is an optional handler of a job, it's called by worker.

func (o *YourJob) OnFailed(ctx context.Context){
    // ...
}

type JobFinish

type JobFinish interface {
	OnFinish(ctx context.Context)
}

JobFinish is an optional handler of a job, it's called by worker.

func (o *YourJob) OnFinish(ctx context.Context){
    // ...
}

type JobSucceed

type JobSucceed interface {
	OnSucceed(ctx context.Context)
}

JobSucceed is an optional handler of a job, it's called by worker.

func (o *YourJob) OnSucceed(ctx context.Context){
    // ...
}

type Strategy

type Strategy string

Strategy is a type name used to define timed execution time for crontab like follows.

  • "*/1 * * * * * *" (every second)
  • "0 1/* * * * * *" (every minute)
  • "0 0 1/* * * * *" (every hour)
const (
	// EveryMinute
	// schedule once every 0 seconds per minute like follows.
	//
	//   1. 09:00:00
	//   2. 09:01:00
	EveryMinute Strategy = "0 */1 * * * * *"

	// EveryHour
	// schedule once every 0 seconds and 0 minutes per hour like follows.
	//
	//   1. 09:00:00
	//   2. 10:00:00
	EveryHour Strategy = "0 0 */1 * * * *"
)

func (Strategy) Parse

func (o Strategy) Parse() (*cronexpr.Expression, error)

Parse timed execution time.

func (Strategy) String

func (o Strategy) String() string

String convert strategy as string.

type Worker

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

Worker is a job manager that schedule in crontab. It's call job handlers in a goroutine.

func NewWorker

func NewWorker(strategy Strategy, job Job) *Worker

NewWorker creates a worker with job.

Example
worker := NewWorker(EveryMinute, &ej{})
println("class:", worker.class)
println("strategy:", worker.strategy)
Output:

class: gitee.com/go-libs/crontab/src.ej
strategy: 0 */1 * * * * *

func (*Worker) GetClass

func (o *Worker) GetClass() string

GetClass gets worker class.

func (*Worker) GetStrategy

func (o *Worker) GetStrategy() Strategy

GetStrategy gets worker strategy.

func (*Worker) SetGloballyUnique

func (o *Worker) SetGloballyUnique(v bool) *Worker

SetGloballyUnique skip if previous scheduling in any node is not completed and value of v is true.

func (*Worker) SetNodeUnique

func (o *Worker) SetNodeUnique(v bool) *Worker

SetNodeUnique skip if previous scheduling is not completed and value of v is true.

func (*Worker) SetRunOnStartup

func (o *Worker) SetRunOnStartup(v bool) *Worker

SetRunOnStartup schedule the job once immediately when crontab starts if enabled.

Jump to

Keyboard shortcuts

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