cron

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2023 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package cron provides a simple yet powerful cron scheduling library. It allows users to schedule functions to be executed at specific times or intervals, following a format similar to traditional UNIX cron, but with extended support for seconds and milliseconds. Parsing of UNIX cron is done by https://pkg.go.dev/github.com/robfig/cron/v3@v3.0.1

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Job

type Job struct {
	Schedule _cron.Schedule  `json:"schedule"`
	Blocking bool            `json:"blocking"`
	Timezone *time.Location  `json:"timezone"`
	Ctx      context.Context `json:"-"`

	Fn func(ctx context.Context) `json:"-"`
	// contains filtered or unexported fields
}

Job represents a cron job with a specific schedule and task. It holds the schedule string, the parsed schedule, execution settings like blocking behavior, timezone, and the function to execute.

func Schedule

func Schedule(scheduleStr string) *Job

Schedule initializes a new Job with a given cron schedule string. The function panics if the schedule string is invalid. The schedule string supports the traditional UNIX cron format with optional seconds field at the beginning.

func (*Job) Execute

func (j *Job) Execute(fn func(ctx context.Context)) *Job

Execute sets the function (Fn) to be executed by the Job. The provided function should accept a context.Context parameter.

func (*Job) MarshalJSON

func (j *Job) MarshalJSON() ([]byte, error)

MarshalJSON customizes the JSON output of Job.

func (*Job) SetBlocking

func (j *Job) SetBlocking(blocking bool) *Job

SetBlocking configures the Job's blocking behavior. If set to true, the job will run its task synchronously. If false, the job will run asynchronously.

func (*Job) SetTimezone

func (j *Job) SetTimezone(loc *time.Location) *Job

SetTimezone sets the timezone in which the Job's schedule will be interpreted.

func (*Job) Start

func (j *Job) Start()

Start initiates the execution of the Job according to its schedule. The job runs either synchronously or asynchronously based on its Blocking setting.

func (*Job) Stop

func (j *Job) Stop()

Stop halts the execution of the Job. It cancels the Job's context, effectively stopping the running task.

func (*Job) WithContext

func (j *Job) WithContext(ctx context.Context) *Job

WithContext sets a custom context for the Job. This context is used for controlling the execution of the job's function.

Jump to

Keyboard shortcuts

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