quartz

package
Version: v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2022 License: MIT Imports: 14 Imported by: 4

Documentation

Overview

Package quartz is a simple, zero-dependency scheduling library for Go. Inspired by the Quartz Java scheduler.

See README.md for more info.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HashCode

func HashCode(s string) int

HashCode calculates and returns a hash code for the given string.

func NowNano

func NowNano() int64

NowNano returns the current UTC Unix time in nanoseconds.

Types

type CronTrigger

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

CronTrigger implements the quartz.Trigger interface. Used to fire a Job at given moments in time, defined with Unix 'cron-like' schedule definitions.

Examples:

Expression Meaning "0 0 12 * * ?" Fire at 12pm (noon) every day "0 15 10 ? * *" Fire at 10:15am every day "0 15 10 * * ?" Fire at 10:15am every day "0 15 10 * * ? *" Fire at 10:15am every day "0 * 14 * * ?" Fire every minute starting at 2pm and ending at 2:59pm, every day "0 0/5 14 * * ?" Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day "0 0/5 14,18 * * ?" Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day "0 0-5 14 * * ?" Fire every minute starting at 2pm and ending at 2:05pm, every day "0 10,44 14 ? 3 WED" Fire at 2:10pm and at 2:44pm every Wednesday in the month of March. "0 15 10 ? * MON-FRI" Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday "0 15 10 15 * ?" Fire at 10:15am on the 15th day of every month

func NewCronTrigger

func NewCronTrigger(expr string) (*CronTrigger, error)

NewCronTrigger returns a new CronTrigger using the UTC location.

func NewCronTriggerWithLoc added in v0.4.0

func NewCronTriggerWithLoc(expr string, location *time.Location) (*CronTrigger, error)

NewCronTriggerWithLoc returns a new CronTrigger with the given time.Location.

func (*CronTrigger) Description

func (ct *CronTrigger) Description() string

Description returns the description of the trigger.

func (*CronTrigger) NextFireTime

func (ct *CronTrigger) NextFireTime(prev int64) (int64, error)

NextFireTime returns the next time at which the CronTrigger is scheduled to fire.

type CurlJob added in v0.3.0

type CurlJob struct {
	RequestMethod string
	URL           string
	Body          string
	Headers       map[string]string
	Response      string
	StatusCode    int
	JobStatus     JobStatus
	// contains filtered or unexported fields
}

CurlJob represents a cURL command Job, implements the quartz.Job interface. cURL is a command-line tool for getting or sending data including files using URL syntax.

func NewCurlJob added in v0.3.0

func NewCurlJob(
	method string,
	url string,
	body string,
	headers map[string]string,
) (*CurlJob, error)

NewCurlJob returns a new CurlJob.

func (*CurlJob) Description added in v0.3.0

func (cu *CurlJob) Description() string

Description returns the description of the CurlJob.

func (*CurlJob) Execute added in v0.3.0

func (cu *CurlJob) Execute()

Execute is called by a Scheduler when the Trigger associated with this job fires.

func (*CurlJob) Key added in v0.3.0

func (cu *CurlJob) Key() int

Key returns the unique CurlJob key.

type Function added in v0.5.0

type Function[R any] func() (R, error)

Function represents an argument-less function which returns a generic type R and a possible error.

type FunctionJob added in v0.5.0

type FunctionJob[R any] struct {
	Result    *R
	Error     error
	JobStatus JobStatus
	// contains filtered or unexported fields
}

FunctionJob represents a Job that invokes the passed Function, implements the quartz.Job interface.

func NewFunctionJob added in v0.5.0

func NewFunctionJob[R any](function Function[R]) *FunctionJob[R]

NewFunctionJob returns a new FunctionJob without an explicit description.

func NewFunctionJobWithDesc added in v0.5.0

func NewFunctionJobWithDesc[R any](desc string, function Function[R]) *FunctionJob[R]

NewFunctionJob returns a new FunctionJob with an explicit description.

func (*FunctionJob[R]) Description added in v0.5.0

func (f *FunctionJob[R]) Description() string

Description returns the description of the FunctionJob.

func (*FunctionJob[R]) Execute added in v0.5.0

func (f *FunctionJob[R]) Execute()

Execute is called by a Scheduler when the Trigger associated with this job fires. It invokes the held function, setting the results in Result and Error members.

func (*FunctionJob[R]) Key added in v0.5.0

func (f *FunctionJob[R]) Key() int

Key returns the unique FunctionJob key.

type Job

type Job interface {
	// Execute is called by a Scheduler when the Trigger associated with this job fires.
	Execute()

	// Description returns the description of the Job.
	Description() string

	// Key returns the unique key for the Job.
	Key() int
}

Job represents an interface to be implemented by structs which represent a 'job' to be performed.

type JobStatus added in v0.3.0

type JobStatus int8

JobStatus represents a Job status.

const (
	// NA is the initial Job status.
	NA JobStatus = iota

	// OK indicates the Job was completed successfully.
	OK

	// FAILURE indicates the Job failed.
	FAILURE
)

type RunOnceTrigger

type RunOnceTrigger struct {
	Delay time.Duration
	// contains filtered or unexported fields
}

RunOnceTrigger implements the quartz.Trigger interface. This type of Trigger can only be fired once and will expire immediately.

func NewRunOnceTrigger

func NewRunOnceTrigger(delay time.Duration) *RunOnceTrigger

NewRunOnceTrigger returns a new RunOnceTrigger with the given delay time.

func (*RunOnceTrigger) Description

func (ot *RunOnceTrigger) Description() string

Description returns the description of the trigger.

func (*RunOnceTrigger) NextFireTime

func (ot *RunOnceTrigger) NextFireTime(prev int64) (int64, error)

NextFireTime returns the next time at which the RunOnceTrigger is scheduled to fire. Sets exprired to true afterwards.

type ScheduledJob

type ScheduledJob struct {
	Job                Job
	TriggerDescription string
	NextRunTime        int64
}

ScheduledJob wraps a scheduled Job with its metadata.

type Scheduler

type Scheduler interface {
	// Start starts the scheduler.
	Start()

	// IsStarted determines whether the scheduler has been started.
	IsStarted() bool

	// ScheduleJob schedules a job using a specified trigger.
	ScheduleJob(job Job, trigger Trigger) error

	// GetJobKeys returns the keys of all of the scheduled jobs.
	GetJobKeys() []int

	// GetScheduledJob returns the scheduled job with the specified key.
	GetScheduledJob(key int) (*ScheduledJob, error)

	// DeleteJob removes the job with the specified key from the Scheduler's execution queue.
	DeleteJob(key int) error

	// Clear removes all of the scheduled jobs.
	Clear()

	// Stop shutdowns the scheduler.
	Stop()
}

Scheduler represents a Job orchestrator. Schedulers are responsible for executing Jobs when their associated Triggers fire (when their scheduled time arrives).

type ShellJob added in v0.3.0

type ShellJob struct {
	Cmd       string
	Result    string
	JobStatus JobStatus
}

ShellJob represents a shell command Job, implements the quartz.Job interface. Be aware of runtime.GOOS when sending shell commands for execution.

func NewShellJob added in v0.3.0

func NewShellJob(cmd string) *ShellJob

NewShellJob returns a new ShellJob.

func (*ShellJob) Description added in v0.3.0

func (sh *ShellJob) Description() string

Description returns the description of the ShellJob.

func (*ShellJob) Execute added in v0.3.0

func (sh *ShellJob) Execute()

Execute is called by a Scheduler when the Trigger associated with this job fires.

func (*ShellJob) Key added in v0.3.0

func (sh *ShellJob) Key() int

Key returns the unique ShellJob key.

type SimpleTrigger

type SimpleTrigger struct {
	Interval time.Duration
}

SimpleTrigger implements the quartz.Trigger interface; uses a fixed interval.

func NewSimpleTrigger

func NewSimpleTrigger(interval time.Duration) *SimpleTrigger

NewSimpleTrigger returns a new SimpleTrigger using the given interval.

func (*SimpleTrigger) Description

func (st *SimpleTrigger) Description() string

Description returns the description of the trigger.

func (*SimpleTrigger) NextFireTime

func (st *SimpleTrigger) NextFireTime(prev int64) (int64, error)

NextFireTime returns the next time at which the SimpleTrigger is scheduled to fire.

type StdScheduler

type StdScheduler struct {
	sync.Mutex
	// contains filtered or unexported fields
}

StdScheduler implements the quartz.Scheduler interface.

func NewStdScheduler

func NewStdScheduler() *StdScheduler

NewStdScheduler returns a new StdScheduler.

func (*StdScheduler) Clear

func (sched *StdScheduler) Clear()

Clear removes all of the scheduled jobs.

func (*StdScheduler) DeleteJob

func (sched *StdScheduler) DeleteJob(key int) error

DeleteJob removes the Job with the specified key if present.

func (*StdScheduler) GetJobKeys

func (sched *StdScheduler) GetJobKeys() []int

GetJobKeys returns the keys of all of the scheduled jobs.

func (*StdScheduler) GetScheduledJob

func (sched *StdScheduler) GetScheduledJob(key int) (*ScheduledJob, error)

GetScheduledJob returns the ScheduledJob with the specified key.

func (*StdScheduler) IsStarted added in v0.3.6

func (sched *StdScheduler) IsStarted() bool

IsStarted determines whether the scheduler has been started.

func (*StdScheduler) ScheduleJob

func (sched *StdScheduler) ScheduleJob(job Job, trigger Trigger) error

ScheduleJob schedules a Job using a specified Trigger.

func (*StdScheduler) Start

func (sched *StdScheduler) Start()

Start starts the StdScheduler execution loop.

func (*StdScheduler) Stop

func (sched *StdScheduler) Stop()

Stop exits the StdScheduler execution loop.

type Trigger

type Trigger interface {
	// NextFireTime returns the next time at which the Trigger is scheduled to fire.
	NextFireTime(prev int64) (int64, error)

	// Description returns the description of the Trigger.
	Description() string
}

Trigger represents the mechanism by which Jobs are scheduled.

Jump to

Keyboard shortcuts

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