# README
gocron
Scheduled task library encapsulated on cron v3.
Example of use
package main
import (
"fmt"
"time"
"github.com/go-dev-frame/sponge/pkg/gocron"
"github.com/go-dev-frame/sponge/pkg/logger"
)
var task1 = func() {
fmt.Println("this is task1")
fmt.Println("running task list:", gocron.GetRunningTasks())
}
var taskOnce = func() {
fmt.Println("this is task2, only run once")
fmt.Println("running task list:", gocron.GetRunningTasks())
}
func main() {
err := gocron.Init(
gocron.WithLogger(logger.Get()),
// gocron.WithLogger(logger.Get(), true), // only print error logs, ignore info logs
)
if err != nil {
panic(err)
}
gocron.Run([]*gocron.Task{
{
Name: "task1",
TimeSpec: "@every 2s",
Fn: task1,
},
{
Name: "taskOnce",
TimeSpec: "@every 3s",
Fn: taskOnce,
IsRunOnce: true, // run only once
},
}...)
time.Sleep(time.Second * 10)
// stop task1
gocron.DeleteTask("task1")
// view running tasks
fmt.Println("running task list:", gocron.GetRunningTasks())
}
# Functions
DeleteTask stop and delete the specified task.
Everyday size (1~31).
EveryHour every hour size (1~23).
EveryMinute every minute size (1~59).
EverySecond every second size (1~59).
GetRunningTasks gets a list of running task names.
Init initialize and start timed tasks.
IsRunningTask determine if the task is running.
Run the tasks.
Stop all scheduled tasks.
WithGranularity set log.
WithLog set granularity.
# Type aliases
Option set the cron options.