looper

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2025 License: MIT Imports: 7 Imported by: 1

README

➰ looper - Looper for jobs with redis lock

Go Report Card Go Reference

looper is a for running jobs in loop, it supports redis locking for using looper in mulitple instances

TODO: everything

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFailedToConnectToLocker    = errors.New("looper - failed to connect to locker")
	ErrFailedToObtainLock         = errors.New("looper - failed to obtain lock")
	ErrFailedToReleaseLock        = errors.New("looper - failed to release lock")
	ErrFailedToCreateLockTable    = errors.New("looper - failed to create lock table")
	ErrFailedToCheckLockExistence = errors.New("looper - failed to check lock existence")
)

Functions

func SetPanicHandler

func SetPanicHandler(handler PanicHandlerFunc)

Types

type Config

type Config struct {
	// Startup time ensuring a consistent delay between registered jobs on start of looper.
	//
	// StartupTime = 1 second; 5 registered jobs; Jobs would be initiated
	// with 200ms delay
	StartupTime time.Duration

	Locker Locker
}

type HookAfterJob

type HookAfterJob func(jobName string, duration time.Duration)

type HookAfterJobError

type HookAfterJobError func(jobName string, duration time.Duration, err error)

type HookBeforeJob

type HookBeforeJob func(jobName string)

type Job

type Job struct {
	// Job function which get triggered by looper.
	JobFn JobFn

	// Name of the job.
	Name string

	// Timeout for job, maximum time, the job can run, after timeout the job get killed.
	Timeout time.Duration

	// Wait duration before next job execution after successful execution of previous job.
	WaitAfterSuccess time.Duration

	// Wait duration before next job execution after unsuccessful execution of previous job.
	WaitAfterError time.Duration

	// If job is Active, and can be started.
	Active bool

	// If job is started.
	Started bool

	// If job is currently running.
	Running bool

	// Last time the job ran.
	LastRun time.Time

	// Count of successful job runs.
	RunCountSuccess uint64

	// Count of unsuccessful job runs.
	RunCountError uint64

	// Copy of last error, that occured.
	LastError error

	// Hook function before job runs.
	BeforeJob HookBeforeJob

	// Hook function after job runs successfully.
	AfterJob HookAfterJob

	// Hook function after job runs unsuccessfully.
	AfterJobError HookAfterJobError

	// If the job should use locker
	WithLocker bool
	// contains filtered or unexported fields
}

type JobFn

type JobFn func(ctx context.Context) error

type Lock added in v0.0.5

type Lock interface {
	Unlock(ctx context.Context) error
}

Lock represents an obtained Lock

type Locker added in v0.0.5

type Locker interface {
	Lock(ctx context.Context, key string, timeout time.Duration) (Lock, error)
}

Lock if an error is returned by lock, the job will not be scheduled.

type Looper

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

Looper

func New

func New(cfg *Config) *Looper

func (*Looper) AddJob

func (l *Looper) AddJob(ctx context.Context, jobInput *Job) (err error)

func (*Looper) Jobs

func (l *Looper) Jobs() []*Job

func (*Looper) RegisterHooks

func (l *Looper) RegisterHooks(
	beforeJob HookBeforeJob,
	afterJob HookAfterJob,
	afterJobError HookAfterJobError,
)

func (*Looper) Start

func (l *Looper) Start()

func (*Looper) StartJobByName

func (l *Looper) StartJobByName(jobName string) error

func (*Looper) Stop

func (l *Looper) Stop()

type PanicHandlerFunc

type PanicHandlerFunc func(jobName string, recoverData any)

Panic handler

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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