cron

package
v1.1.8 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultRetryAttempts  = 0
	DefaultRestartOnError = false
)

Variables

This section is empty.

Functions

func Err added in v1.1.5

func Err(err error) error

Err wraps err with a stack trace captured at the call site. Use it inside a cron job to get traces that reach the failure site:

func myJob() error {
    if err := doWork(); err != nil {
        return cron.Err(err)
    }
    return nil
}

Without Err(), the cron runner can only capture the stack trace of the goroutine that called the job, which is not very useful for debugging.

Types

type Config

type Config struct {
	// When set to true the server will attempt to restart failed jobs
	RestartOnError bool

	// The number of times a job will be retried before it is deleted
	// when an error occurs
	//
	// Default: 0
	RetryAttempts int

	// This is the periodic time in which the server can execute
	// background tasks background tasks can run infinitely
	// as long as the server is running
	// for example you can use this to make requests to other servers
	// or update your database
	//
	// Default: 5 minutes
	BackgroundTimeout time.Duration
}

type Cron

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

func New

func New(cfg ...Config) *Cron

func (*Cron) AddJobs

func (c *Cron) AddJobs(jobs ...Job)

func (*Cron) Start

func (c *Cron) Start()

Call this method to start the cron

By default cron jobs are executed in their own goroutines. This method starts the cron jobs in a new goroutine away from the server's main goroutine.

This ensures non blocking execution of cron jobs hence the server can handle requests as cron jobs are executed with minimal impact on the server

type Job

type Job struct {
	Fn   func() error
	Time time.Duration
	// contains filtered or unexported fields
}

This is the structure of a background job you can use this to put whatever jobs you want to perform in the background as the server runs and Pine will take care of executing them in the background

time is optional and defaults to 5 minutes according to the server configuration

Fn is the function that will be executed. It should always return an error. If error is not nil the error will be used to delete the task from the queue otherwise when nil the task will run indefinitely

type JobError added in v1.1.5

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

JobError is an error that carries a stack trace captured at the point the error was created — inside the job function, before it returned. Construct one with Err() so the cron runner can report the full trace.

func (*JobError) Error added in v1.1.5

func (e *JobError) Error() string

func (*JobError) Unwrap added in v1.1.5

func (e *JobError) Unwrap() error

Jump to

Keyboard shortcuts

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