The Scheduler package is a flexible and powerful task scheduling library for Go applications, inspired by the zenstruck/schedule-bundle (https://github.com/zenstruck/schedule-bundle). It provides a clean, fluent interface for defining and managing scheduled tasks with various timing options. The package supports different types of tasks, including callback functions, HTTP ping requests, and process executions.
- Flexible Scheduling: Define tasks to run at specific times, intervals, or using cron expressions
- Multiple Task Types: Support for callback functions, HTTP ping requests, and process executions
- Persistent Storage: Task execution state can be persisted to ensure consistent scheduling across application restarts
- Timezone Support: Schedule tasks in different timezones
- Rich Extension System: Add filters, callbacks, and conditions to tasks
- Concurrent Execution: Tasks are executed concurrently for optimal performance
go get github.com/robertgontarski/schedulerpackage main
import (
"fmt"
"github.com/robertgontarski/scheduler"
"time"
)
func main() {
// Create a new schedule with persistent storage
schedule := scheduler.NewSchedule("tasks.json")
// Create a callback task that runs every minute
echoTask := scheduler.NewCallbackTask(func(context *scheduler.TaskRunContext) error {
fmt.Println("Executing the task at", time.Now().Format("15:04:05"))
return nil
}).
IdentifiedBy("echo-task").
Description("Echo task with storage").
EveryMinute()
// Add the task to the schedule
schedule.AddTask(echoTask)
// Process due tasks
schedule.ProcessTasks()
}// Create a task that pings a URL every 5 minutes
pingTask := scheduler.NewPingTask("https://example.com").
IdentifiedBy("ping-example").
Description("Ping example.com every 5 minutes").
EveryFiveMinutes()
schedule.AddTask(pingTask)// Create a task that runs a system command daily at 2:30 AM
processTask := scheduler.NewProcessTask("backup.sh", "--full").
IdentifiedBy("daily-backup").
Description("Run daily backup").
Daily().
At("2:30")
schedule.AddTask(processTask)// Run a task every Monday, Wednesday, and Friday at 8:00 AM
task := scheduler.NewCallbackTask(func(context *scheduler.TaskRunContext) error {
// Task logic here
return nil
}).
Mondays().
Wednesdays().
Fridays().
At("8:00")
// Run a task only between 9:00 AM and 5:00 PM
task := scheduler.NewCallbackTask(func(context *scheduler.TaskRunContext) error {
// Task logic here
return nil
}).
EveryHour().
OnlyBetween("9:00", "17:00", true)
// Run a task with custom cron expression
task := scheduler.NewCallbackTask(func(context *scheduler.TaskRunContext) error {
// Task logic here
return nil
}).
Cron("*/15 9-17 * * 1-5") // Every 15 minutes from 9 AM to 5 PM, Monday to Friday// Add before and after callbacks
task := scheduler.NewCallbackTask(func(context *scheduler.TaskRunContext) error {
fmt.Println("Main task execution")
return nil
}).
Before(func(context *scheduler.TaskRunContext) {
fmt.Println("Before task execution")
}, "Preparation").
After(func(context *scheduler.TaskRunContext) {
fmt.Println("After task execution")
}, "Cleanup")
// Handle success and failure
task := scheduler.NewCallbackTask(func(context *scheduler.TaskRunContext) error {
// Task logic that might fail
return nil
}).
OnSuccess(func(context *scheduler.TaskRunContext) {
fmt.Println("Task completed successfully")
}, "Success handler").
OnFailure(func(context *scheduler.TaskRunContext) {
fmt.Println("Task failed")
}, "Failure handler")The Schedule is the central component that manages tasks and their execution. It determines which tasks are due for execution and runs them concurrently.
The package provides several task types:
- CallbackTask: Executes a Go function
- PingTask: Makes an HTTP request to a specified URL
- ProcessTask: Executes a system command or process
The TaskStorage component provides persistence for task execution state, ensuring that task schedules are maintained across application restarts.
The CronExpression component handles parsing and evaluation of cron expressions for task scheduling.
This project is licensed under the MIT License - see the LICENSE file for details.