quartz

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2021 License: MIT Imports: 15 Imported by: 0

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 string parameter hash code.

func NowNano

func NowNano(location *time.Location) int64

NowNano returns the current time by location in nanoseconds.

Types

type CronExpressionParser

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

CronExpressionParser parses cron expressions.

func NewCronExpressionParser

func NewCronExpressionParser(lastDefined int) *CronExpressionParser

NewCronExpressionParser returns a new CronExpressionParser.

type CronField

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

CronField represents a parsed cron expression as an array.

func (*CronField) String

func (cf *CronField) String() string

String is the CronField fmt.Stringer implementation.

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.

func (*CronTrigger) Day

func (ct *CronTrigger) Day() []int

func (*CronTrigger) DayOfWeek

func (ct *CronTrigger) DayOfWeek() []int

func (*CronTrigger) Description

func (ct *CronTrigger) Description() string

Description returns a CronTrigger description.

func (*CronTrigger) ExpressionForHuman

func (ct *CronTrigger) ExpressionForHuman() string

func (*CronTrigger) Hour

func (ct *CronTrigger) Hour() []int

func (*CronTrigger) Minute

func (ct *CronTrigger) Minute() []int

func (*CronTrigger) Month

func (ct *CronTrigger) Month() []int

func (*CronTrigger) NextFireTime

func (ct *CronTrigger) NextFireTime(prev int64, location *time.Location) (int64, error)

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

func (*CronTrigger) Second

func (ct *CronTrigger) Second() []int

func (*CronTrigger) Year

func (ct *CronTrigger) Year() []int

type CurlJob

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 is the curl command Job, implements the quartz.Job interface.

func NewCurlJob

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

NewCurlJob returns a new CurlJob.

func (*CurlJob) Description

func (cu *CurlJob) Description() string

Description returns a CurlJob description.

func (*CurlJob) Execute

func (cu *CurlJob) Execute()

Execute Called by the Scheduler when a Trigger fires that is associated with the Job.

func (*CurlJob) Key

func (cu *CurlJob) Key() int

Key returns a CurlJob unique key.

type Item

type Item struct {
	Job     Job
	Trigger Trigger
	// contains filtered or unexported fields
}

Item is the PriorityQueue item.

type Job

type Job interface {
	// Execute Called by the Scheduler when a Trigger fires that is associated with the Job.
	Execute()

	// Description returns a Job description.
	Description() string

	// Key returns a Job unique key.
	Key() int
}

Job is the interface to be implemented by structs which represent a 'job' to be performed.

type JobStatus

type JobStatus int8

JobStatus represents a job status.

const (
	// NA is the initial JobStatus.
	NA JobStatus = iota
	// OK represents a successful JobStatus.
	OK
	// FAILURE represents a failed JobStatus.
	FAILURE
)

type PriorityQueue

type PriorityQueue []*Item

PriorityQueue implements the heap.Interface.

func (*PriorityQueue) Head

func (pq *PriorityQueue) Head() *Item

Head returns the first item of a PriorityQueue without removing it.

func (PriorityQueue) Len

func (pq PriorityQueue) Len() int

Len returns the PriorityQueue length.

func (PriorityQueue) Less

func (pq PriorityQueue) Less(i, j int) bool

Less is the items less comparator.

func (*PriorityQueue) Pop

func (pq *PriorityQueue) Pop() interface{}

Pop implements the heap.Interface.Pop. Removes and returns element Len() - 1.

func (*PriorityQueue) Push

func (pq *PriorityQueue) Push(x interface{})

Push implements the heap.Interface.Push. Adds x as element Len().

func (*PriorityQueue) Remove

func (pq *PriorityQueue) Remove(i int) interface{}

Remove removes and returns the element at index i from the PriorityQueue.

func (PriorityQueue) Swap

func (pq PriorityQueue) Swap(i, j int)

Swap exchanges the indexes of the items.

type RunOnceTrigger

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

RunOnceTrigger implements the quartz.Trigger interface. Could be triggered only once.

func NewRunOnceTrigger

func NewRunOnceTrigger(delay time.Duration) *RunOnceTrigger

NewRunOnceTrigger returns a new RunOnceTrigger.

func (*RunOnceTrigger) Description

func (st *RunOnceTrigger) Description() string

Description returns a RunOnceTrigger description.

func (*RunOnceTrigger) NextFireTime

func (st *RunOnceTrigger) NextFireTime(prev int64, location *time.Location) (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 the scheduled job with the metadata.

type Scheduler

type Scheduler interface {
	// start the scheduler
	Start(location *time.Location)
	// schedule the job with the specified trigger
	ScheduleJob(job Job, trigger Trigger, location *time.Location) error
	// get keys of all of the scheduled jobs
	GetJobKeys() []int
	// get the scheduled job metadata
	GetScheduledJob(key int) (*ScheduledJob, error)
	// remove the job from the execution queue
	DeleteJob(key int) error
	// clear all the scheduled jobs
	Clear()
	// shutdown the scheduler
	Stop()
}

A Scheduler is the Jobs orchestrator. Schedulers responsible for executing Jobs when their associated Triggers fire (when their scheduled time arrives).

type ShellJob

type ShellJob struct {
	Cmd       string
	Result    string
	JobStatus JobStatus
}

ShellJob is the shell command Job, implements the quartz.Job interface. Consider the runtime.GOOS when sending the shell command to execute.

func NewShellJob

func NewShellJob(cmd string) *ShellJob

NewShellJob returns a new ShellJob.

func (*ShellJob) Description

func (sh *ShellJob) Description() string

Description returns a ShellJob description.

func (*ShellJob) Execute

func (sh *ShellJob) Execute()

Execute Called by the Scheduler when a Trigger fires that is associated with the Job.

func (*ShellJob) Key

func (sh *ShellJob) Key() int

Key returns a ShellJob unique key.

type SimpleTrigger

type SimpleTrigger struct {
	Interval time.Duration
}

SimpleTrigger implements the quartz.Trigger interface; uses a time.Duration interval.

func NewSimpleTrigger

func NewSimpleTrigger(interval time.Duration) *SimpleTrigger

NewSimpleTrigger returns a new SimpleTrigger.

func (*SimpleTrigger) Description

func (st *SimpleTrigger) Description() string

Description returns a SimpleTrigger description.

func (*SimpleTrigger) NextFireTime

func (st *SimpleTrigger) NextFireTime(prev int64, location *time.Location) (int64, error)

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

type StdScheduler

type StdScheduler struct {
	sync.Mutex
	Queue *PriorityQueue
	// 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 for the specified key from the StdScheduler 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 by the unique key.

func (*StdScheduler) ScheduleJob

func (sched *StdScheduler) ScheduleJob(job Job, trigger Trigger, location *time.Location) error

ScheduleJob uses the specified Trigger to schedule the Job.

func (*StdScheduler) Start

func (sched *StdScheduler) Start(location *time.Location)

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, location *time.Location) (int64, error)

	// Description returns a Trigger description.
	Description() string
}

Trigger is the Triggers interface. Triggers are 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