The package supports flexible time management (milliseconds, seconds, minutes, hours) and provides the ability to cancel tasks.
-
🕒 Flexible Intervals: Support for milliseconds, seconds, minutes, and hours.
-
🔄 Method Chaining: Convenient API with method chaining for task configuration.
-
🛑 Task Cancellation: Ability to stop tasks via a cancellation channel.
-
🧩 Goroutines: Tasks run in separate goroutines.
-
🔒 Thread Safety: Safe for use in concurrent applications.
Installation
go get github.com/pashkov256/schedify
package main
import (
"fmt"
"time"
"github.com/pashkov256/schedify"
)
func main() {
scheduler := schedify.New()
cancelCh := make(chan struct{})
// Task that runs every 2 seconds
scheduler.Every().Second(2).Do(func() {
fmt.Println("Task runs every 2 seconds")
}, scheduler, cancelCh)
// Task that runs every minute
scheduler.Every().Minute(1).Do(func() {
fmt.Println("Task runs every minute")
}, scheduler, cancelCh)
// Wait for 10 seconds
time.Sleep(10 * time.Second)
// Cancel all tasks
close(cancelCh)
// Wait for all tasks to complete
scheduler.Wait()
}
Second(num uint) *Interval
: Sets the interval in seconds.
Minute(num uint) *Interval
: Sets the interval in minutes.
Hour(num uint) *Interval
: Sets the interval in hours.
scheduler.Every().Second(10) // Every 10 seconds
scheduler.Every().Minute(5) // Every 5 minutes
Do(fn func(), schPointer *Scheduler, cancelCh <-chan struct{})
Do(fn func(), schPointer *Scheduler, cancelCh <-chan struct{})
Runs the task at the specified interval. Accepts:
-
fn: The function to execute.
-
schPointer: A pointer to the scheduler.
-
cancelCh: A channel to cancel the task.
Example:
scheduler.Every().Second(2).Do(func() {
fmt.Println("Task runs every 2 seconds")
}, scheduler, cancelCh)
Wait()
Waits for all tasks to complete. Used for graceful shutdown of the scheduler.
scheduler.Wait()