timer

package
v1.13.1 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cron

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

Cron is a struct that holds a channel for timers and a callback function. The Cron struct is used to manage cron jobs in the dispatcher. It is responsible for dispatching the cron jobs and executing their callbacks.

func (*Cron) Stop

func (c *Cron) Stop()

Stop the cron job and clean up resources The Stop method is used to stop the cron job and clean up resources. It is called when the cron job is no longer needed.

type CronExpr

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

Field name | Mandatory? | Allowed values | Allowed special characters ---------- | ---------- | -------------- | -------------------------- Seconds | No | 0-59 | * / , - Minutes | Yes | 0-59 | * / , - Hours | Yes | 0-23 | * / , - Day of month | Yes | 1-31 | * / , - Month | Yes | 1-12 | * / , - Day of week | Yes | 0-6 | * / , -

example: ``` */5 * * * * * 每 5 秒执行一次。 0 0 12 * * * 每天中午 12 点执行一次。 0 15 10 * * 1-5 每周一到周五的 10:15 执行一次。 ```

func NewCronExpr

func NewCronExpr(expr string) (cronExpr *CronExpr, err error)

NewCronExpr parses a cron expression and returns a CronExpr struct. The cron expression should be in the format of "sec min hour dom month dow" or "min hour dom month dow" (without seconds). The fields are separated by spaces. The seconds field is optional and defaults to 0 if not provided. The function returns an error if the cron expression is invalid. The function supports the following formats for each field: 1. * (every value) 2. num (specific value) 3. num-num (range of values) 4. */num (every num value) 5. num/num (every num value in the range of num to max) 6. num-num/num (range of values with increment) The function also supports the following special characters: 1. , (list of values) 2. - (range of values) 3. / (increment) 4. * (every value) 5. ? (no specific value) 6. L (last value) 7. W (nearest weekday) 8. # (nth weekday of the month) The function returns a pointer to a CronExpr struct and an error if the cron expression is invalid. The CronExpr struct contains fields for seconds, minutes, hours, day of month, month, and day of week. The fields are represented as bit masks, where each bit represents a value in the range of the field. The function uses bitwise operations to parse the cron expression and set the appropriate bits in the fields. The function also checks for valid ranges and increments for each field and returns an error if any of the fields are invalid.

func (*CronExpr) Next

func (e *CronExpr) Next(t time.Time) time.Time

Next returns the next time that matches the cron expression after the given time t. The function returns a time.Time object representing the next matching time.

type Dispatcher

type Dispatcher struct {
	ChanTimer chan *Timer
}

Dispatcher is a struct that holds a channel for timers. The Dispatcher struct is used to manage the timers and their callbacks. It is responsible for dispatching the timers and executing their callbacks.

func NewDispatcher

func NewDispatcher(l int) *Dispatcher

NewDispatcher creates a new Dispatcher with a channel for timers. The channel size is specified by the parameter l. The Dispatcher is used to manage the timers and their callbacks.

func (*Dispatcher) AfterFunc

func (disp *Dispatcher) AfterFunc(d time.Duration, cb func()) *Timer

AfterFunc creates a new Timer that will execute the callback function after the specified duration. The method takes a duration and a callback function as parameters. It returns a pointer to the Timer struct that was created.

func (*Dispatcher) CronFunc

func (disp *Dispatcher) CronFunc(cronExpr *CronExpr, _cb func()) *Cron

cronExpr is a pointer to a CronExpr struct that represents a cron expression. The CronExpr struct is used to parse and evaluate cron expressions. The CronExpr struct contains fields for seconds, minutes, hours, day of month, month, and day of week.

type Timer

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

Timer is a struct that holds a time.Timer and a callback function. The Timer struct is used to manage timers in the dispatcher.

func (*Timer) Cb

func (t *Timer) Cb()

Cb is a method that executes the callback function of the timer. It is called when the timer expires and the callback function is set. The method uses a deferred function to recover from any panic that may occur during the execution of the callback function.

func (*Timer) Stop

func (t *Timer) Stop()

The Stop method is used to stop the timer and execute the callback function. It is called when the timer is no longer needed.

Jump to

Keyboard shortcuts

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