schedule

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2024 License: MIT Imports: 9 Imported by: 0

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

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

func (j *Job) DailyAt(time ...string) *Job

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) Immediate added in v1.5.0

func (j *Job) Immediate() *Job

func (*Job) OnOneServer

func (j *Job) OnOneServer() *Job

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

func (j *Job) PerHour(hour int) *Job

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

func (j *Job) PerMinuit(minuit int) *Job

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

func (j *Job) PerSeconds(seconds int) *Job

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

func (j *Job) RandomDelay(min, max int) *Job

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

func (j *Job) WithoutOverlapping() *Job

WithoutOverlapping sets the job to not allow overlapping executions.

Returns:

  • *Job: The modified Job instance

Example:

job.WithoutOverlapping()

type RandomDelay

type RandomDelay struct {
	Min int // Minimum delay in seconds
	Max int // Maximum delay in seconds
}

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

func New(logger *logger.Manager, redis *redis.Manager, traceID *trace.ID) *Schedule

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)

func (*Schedule) Start

func (s *Schedule) Start()

Start begins the scheduling process for all added jobs.

This method starts a goroutine that ticks every second and attempts to run each job in the scheduler.

Jump to

Keyboard shortcuts

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