The Scheduler
enables scheduled job execution at specified intervals or according to cron expressions, facilitating routine operations such as nightly backups, weekly report generation, or periodic data synchronization tasks.
Start with a valid Redis configuration. Initialize the Scheduler with necessary options like the time zone:
redisConfig := queue.NewRedisConfig(
queue.WithRedisAddress("localhost:6379"), // Specify your Redis server address
// Additional configuration options as needed...
)
scheduler, err := queue.NewScheduler(redisConfig,
queue.WithSchedulerLocation(time.UTC), // Adjust the time zone as needed
)
if err != nil {
log.Fatal("Scheduler creation failed:", err)
}
Incorporate custom logic prior to job enqueuing:
scheduler.WithPreEnqueueFunc(func(job *queue.Job) {
// Insert pre-enqueue operations here
log.Printf("Job preparation: %s\n", job.Type)
})
Execute follow-up actions after job enqueuing, especially for error handling:
scheduler.WithPostEnqueueFunc(func(job *queue.JobInfo, err error) {
if err != nil {
log.Printf("Enqueue failed for job: %s, error: %v\n", job.Type, err)
} else {
log.Printf("Job enqueued successfully: %s\n", job.Type)
}
})
For scheduling jobs based on cron expressions:
jobType := "report:generate"
payload := map[string]interface{}{"reportType": "weekly"}
cronExpression := "0 9 * * 1" // Example: Every Monday at 9:00 AM
_, err = scheduler.RegisterCron(cronExpression, jobType, payload)
if err != nil {
log.Fatalf("Cron job scheduling failed: %v", err)
}
For interval-based job scheduling:
jobType := "status:check"
payload := map[string]interface{}{"target": "database"}
interval := 15 * time.Minute // Example: Every 15 minutes
_, err = scheduler.RegisterPeriodic(interval, jobType, payload)
if err != nil {
log.Fatalf("Periodic job scheduling failed: %v", err)
}
Starting the Scheduler:
if err := scheduler.Start(); err != nil {
log.Fatal("Starting scheduler failed:", err)
}
Stopping the Scheduler:
if err := scheduler.Stop(); err != nil {
log.Println("Scheduler shutdown error:", err)
}