Skip to content

robertgontarski/scheduler

Repository files navigation

Scheduler Package

Overview

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.

Features

  • 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

Installation

go get github.com/robertgontarski/scheduler

Usage Examples

Basic Example

package 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()
}

HTTP Ping Task

// 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)

Process Execution Task

// 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)

Advanced Scheduling

// 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

Task Extensions

// 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")

Core Components

Schedule

The Schedule is the central component that manages tasks and their execution. It determines which tasks are due for execution and runs them concurrently.

Tasks

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

TaskStorage

The TaskStorage component provides persistence for task execution state, ensuring that task schedules are maintained across application restarts.

CronExpression

The CronExpression component handles parsing and evaluation of cron expressions for task scheduling.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages