gorun

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2024 License: MIT Imports: 14 Imported by: 2

README

gorun - A distributed job scheduling system

I am considering preparing this code for a more general release. At this point the code is completely subject to change and not yet ready for general use.

With that caveat, a variant of this code has worked for me in a production environment, and anyone is free to fork this repo to use as a starting place.

Documentation

Index

Constants

View Source
const (
	StatusScheduled = "scheduled"
	StatusRunning   = "running"
	StatusCompleted = "completed"
	StatusFailed    = "failed"
)

Variables

View Source
var ErrGorunInternalError = errors.Sentinel("internal gorun job service error")
View Source
var ErrUnregisteredJobType = errors.Sentinel("unregistered job type")

Functions

func MarkIncompleteJobsHandler

func MarkIncompleteJobsHandler(ctx context.Context, args *MarkIncompleteJobs) (string, error)

func ProcessTriggersHandler

func ProcessTriggersHandler(ctx context.Context, args *ProcessTriggers) (string, error)

func RegisterHandler

func RegisterHandler[T JobData](h Handler[T])

Types

type GoRunService

type GoRunService interface {
	ScheduleImmediately(ctx context.Context, job JobData) (jobId string, err error)
	ScheduleAfter(ctx context.Context, delay time.Duration, job JobData) (jobId string, err error)
	ScheduleCron(ctx context.Context, cronExpr string, loc *time.Location, job JobData) (triggerId string, err error)
	ScheduleRepeated(ctx context.Context, interval time.Duration, job JobData) (triggerId string, err error)

	ScheduleCronWithKey(ctx context.Context, triggerId string, cronExpr string, loc *time.Location, job JobData) error
	ScheduleRepeatedWithKey(ctx context.Context, triggerId string, interval time.Duration, job JobData) error

	GetJob(ctx context.Context, jobId string) (*gorundb.JobData, error)
	ListJobs(ctx context.Context, start, end time.Time) ([]*gorundb.JobData, error)
	ListTriggers(ctx context.Context) ([]*gorundb.JobTrigger, error)
	DeleteTrigger(ctx context.Context, triggerId string) error

	Start(ctx context.Context) error
	Close()
}

func New

func New(db *sql.DB, opts ...Option) (GoRunService, error)

func NewFromEnv

func NewFromEnv(opts ...Option) (GoRunService, error)

type Handler

type Handler[T JobData] func(ctx context.Context, args *T) (string, error)

type JobData

type JobData interface {
	// Return a constant name for the job that will process the request - this is used to uniquely identify the job handler
	JobType() string
}

type MarkIncompleteJobs

type MarkIncompleteJobs struct{}

func (MarkIncompleteJobs) JobType

func (a MarkIncompleteJobs) JobType() string

type Option

type Option func(*options)

func DisableLogging

func DisableLogging() Option

func OnJobComplete added in v0.3.0

func OnJobComplete(f func(ctx context.Context, jobType string, jobId string, result string, err error)) Option

func OnJobInit added in v0.3.0

func OnJobInit(f func(ctx context.Context, jobType string, jobId string) context.Context) Option

func WithArgProcessor added in v0.3.0

func WithArgProcessor(f func(ctx context.Context, jobType string, jobId string, args any) error) Option

func WithBatchFreq

func WithBatchFreq(freq time.Duration) Option

How often each job server will check for new jobs to run

func WithBatchSize

func WithBatchSize(size int) Option

How many new jobs to run in each batch. Currently there is no limit on the number of concurrent jobs.

func WithJobTimeout

func WithJobTimeout(jobTimeout time.Duration) Option

The maximum amount of time a job can run before it is considered to have timed out

type ProcessTriggers

type ProcessTriggers struct{}

func (ProcessTriggers) JobType

func (a ProcessTriggers) JobType() string

type Trigger

type Trigger = triggers.Trigger

type Validateable

type Validateable interface {
	Validate() error
}

Directories

Path Synopsis
examples
helloworld module

Jump to

Keyboard shortcuts

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