gocron

package
v1.10.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2024 License: MIT Imports: 6 Imported by: 0

README

gocron

Scheduled task library encapsulated on cron v3.


Example of use

package main

import (
	"fmt"
	"time"

	"github.com/zhufuyi/sponge/pkg/gocron"
	"github.com/zhufuyi/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())
}

Documentation

Overview

Package gocron is scheduled task library.

Index

Constants

This section is empty.

Variables

View Source
var (
	SecondType = 0
	MinuteType = 1
)

Functions

func DeleteTask

func DeleteTask(name string)

DeleteTask stop and delete the specified task

func EveryHour

func EveryHour(size int) string

EveryHour every hour size (1~23)

func EveryMinute

func EveryMinute(size int) string

EveryMinute every minute size (1~59)

func EverySecond

func EverySecond(size int) string

EverySecond every second size (1~59)

func Everyday

func Everyday(size int) string

Everyday size (1~31)

func GetRunningTasks

func GetRunningTasks() []string

GetRunningTasks gets a list of running task names

func Init

func Init(opts ...Option) error

Init initialize and start timed tasks

func IsRunningTask

func IsRunningTask(name string) bool

IsRunningTask determine if the task is running

func Run

func Run(tasks ...*Task) error

Run the tasks

func Stop

func Stop()

Stop all scheduled tasks

Types

type Option

type Option func(*options)

Option set the cron options.

func WithGranularity added in v1.10.0

func WithGranularity(granularity int) Option

WithGranularity set log

func WithLog

func WithLog(log *zap.Logger, isOnlyPrintError ...bool) Option

WithLog set granularity

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL