gocron

package
v1.8.2 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 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"
)

var task1 = func() {
     fmt.Println("this is task1")
     fmt.Println("running task list:", gocron.GetRunningTasks())
}

var taskOnce = func() {
	taskName := "taskOnce"
    fmt.Println("this is taskOnce")
    gocron.DeleteTask(taskName)
}

func main() {
    err := gocron.Init()
    if err != nil {
        panic(err)
    }
	
    gocron.Run([]*gocron.Task{
        {
            Name:     "task1",
            TimeSpec: "@every 2s",
            Fn:       task1,
        },
        {
            Name:     "taskOnce",
            TimeSpec: "@every 5s",
            Fn:       taskOnce,
        },
    }...)

    time.Sleep(time.Minute)

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

This section is empty.

Functions

func DeleteTask

func DeleteTask(name string)

DeleteTask delete 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 scheduled task

Types

type Option

type Option func(*options)

Option set the cron options.

func WithLog

func WithLog(log *zap.Logger) Option

WithLog 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
}

Task scheduled task

Jump to

Keyboard shortcuts

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