README
¶
gocron
Scheduled task library encapsulated on cron v3.
Example of use
package main
import (
"fmt"
"time"
"github.com/18721889353/sunshine/pkg/gocron"
)
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())
}
Documentation
¶
Overview ¶
Package gocron is scheduled task library.
Index ¶
- Variables
- func DeleteTask(name string)
- func EveryHour(size int) string
- func EveryMinute(size int) string
- func EverySecond(size int) string
- func Everyday(size int) string
- func GetRunningTasks() []string
- func Init(opts ...Option) error
- func IsRunningTask(name string) bool
- func Run(tasks ...*Task) error
- func Stop()
- type Option
- type Task
Constants ¶
This section is empty.
Variables ¶
View Source
var ( SecondType = 0 MinuteType = 1 )
Functions ¶
func GetRunningTasks ¶
func GetRunningTasks() []string
GetRunningTasks gets a list of running task names
func IsRunningTask ¶
IsRunningTask determine if the task is running
Types ¶
type Option ¶
type Option func(*options)
Option set the cron options.
func WithGranularity ¶ added in v1.1.17
WithGranularity set log
type Task ¶
type Task struct { // seconds (0-59) minutes (0- 59) hours (0-23) days (1-31) months (1-12) weeks (0-6) // "*/5 * * * * *" means every five seconds. // "0 15,45 9-12 * * * " indicates execution at the 15th and 45th minutes from 9 a.m. to 12 a.m. each day TimeSpec string Name string // task name Fn func() // task function IsRunOnce bool // if the task is only run once }
Task scheduled task
Click to show internal directories.
Click to hide internal directories.