Documentation
¶
Overview ¶
Package schedule provides functionality for scheduling and managing jobs in a distributed environment. It supports various scheduling patterns and includes features like job locking and distributed execution.
Index ¶
- type HandlerFunc
- type Job
- func (j *Job) DailyAt(time ...string) *Job
- func (j *Job) Immediate() *Job
- func (j *Job) OnOneServer() *Job
- func (j *Job) PerHour(hour int) *Job
- func (j *Job) PerMinuit(minuit int) *Job
- func (j *Job) PerSeconds(seconds int) *Job
- func (j *Job) RandomDelay(min, max int) *Job
- func (j *Job) WithoutOverlapping() *Job
- type RandomDelay
- type RunTime
- type RunType
- type Schedule
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HandlerFunc ¶
type HandlerFunc interface { Exec(ctx context.Context) Error() <-chan error Done() <-chan struct{} }
HandlerFunc interface defines the methods that a job handler must implement.
type Job ¶
type Job struct { Name string // Name of the job instance Logger *logger.Manager // Logger for the job Redis *redis.Manager // Redis client for job operations Handler HandlerFunc // Function to be executed EnableMultipleServers bool // Allow execution on multiple nodes EnableOverlapping bool // Allow job to run even if previous instance is still running RunTime *RunTime // Runtime parameters for the job TraceID *trace.ID // TraceID for job execution tracking }
Job represents a scheduled task with its properties and execution settings.
func (*Job) DailyAt ¶
DailyAt schedules the job to run at specific times each day.
Parameters:
- time: One or more time strings in "HH:MM:SS" format
Returns:
- *Job: The modified Job instance
Example:
job.DailyAt("07:30:00", "12:00:00", "18:00:00")
func (*Job) OnOneServer ¶
OnOneServer sets the job to run on only one server in a distributed environment.
Returns:
- *Job: The modified Job instance
Example:
job.OnOneServer()
func (*Job) PerHour ¶
PerHour schedules the job to run every specified number of hours.
Parameters:
- hour: Interval in hours
Returns:
- *Job: The modified Job instance
Example:
job.PerHour(4)
func (*Job) PerMinuit ¶
PerMinuit schedules the job to run every specified number of minutes.
Parameters:
- minuit: Interval in minutes
Returns:
- *Job: The modified Job instance
Example:
job.PerMinuit(15)
func (*Job) PerSeconds ¶
PerSeconds schedules the job to run every specified number of seconds.
Parameters:
- seconds: Interval in seconds
Returns:
- *Job: The modified Job instance
Example:
job.PerSeconds(30)
func (*Job) RandomDelay ¶
RandomDelay sets a random delay range for job execution.
Parameters:
- min: Minimum delay in seconds
- max: Maximum delay in seconds
Returns:
- *Job: The modified Job instance
Example:
job.RandomDelay(30, 60)
func (*Job) WithoutOverlapping ¶
WithoutOverlapping sets the job to not allow overlapping executions.
Returns:
- *Job: The modified Job instance
Example:
job.WithoutOverlapping()
type RandomDelay ¶
RandomDelay defines the minimum and maximum random delay for job execution.
type RunTime ¶
type RunTime struct { Type RunType // Type of schedule (daily, per second, per minute, per hour, immediate) Time interface{} // Execution time or interval Locked bool // Execution lock for non-overlapping jobs PerTypeLocked bool // Lock for interval-based job types Done chan struct{} // Channel to signal job completion RandomDelay *RandomDelay // Random delay settings for job execution }
RunTime contains the runtime parameters for a job.
type RunType ¶ added in v1.5.0
type RunType string
RunType represents the type of job execution.
const ( DailyRunType RunType = "daily" // Run task daily SecondlyRunType RunType = "seconds" // Run task every X seconds MinutelyRunType RunType = "minute" // Run task every X minutes HourlyRunType RunType = "hour" // Run task every X hours ImmediateRunType RunType = "immediate" // Run task immediately DefaultServerLockTTL = 600 // Default single-server task lock time (10 minutes) )
Constants for job run types and default server lock TTL
type Schedule ¶
type Schedule struct { Logger *logger.Manager // Logger for the scheduler Redis *redis.Manager // Redis client for distributed locking Job []*Job // Slice of jobs managed by this scheduler TraceID *trace.ID // TraceID for logging and tracking }
Schedule represents the main scheduler structure.
func New ¶
New creates and returns a new Schedule instance.
Parameters:
- logger: A logger.Manager instance for logging
- redis: A redis.Manager instance for Redis operations
- traceID: A trace.ID instance for request tracing
Returns:
- *Schedule: A new Schedule instance
Example:
scheduler := New(loggerInstance, redisInstance, traceIDInstance)
func (*Schedule) AddJob ¶
func (s *Schedule) AddJob(name string, handlerFunc HandlerFunc) *Job
AddJob adds a new job to the scheduler.
Parameters:
- name: A string representing the name of the job
- handlerFunc: A HandlerFunc that defines the job's execution logic
Returns:
- *Job: The newly created Job instance
Example:
job := scheduler.AddJob("dailyReport", reportHandler)