gojob

package
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

README

定时任务

 基于库: https://github.com/go-co-op/gocron/ 
 带参数的任务函数 定时执行还没支持

官方文档: https://pkg.go.dev/github.com/go-co-op/gocron/v2
package main

import (
	"fmt"
	gojob "github.com/gif-gif/go.io/go-job"
	golog "github.com/gif-gif/go.io/go-log"
	"github.com/go-co-op/gocron/v2"
	"github.com/gogf/gf/util/gconv"
	"github.com/google/uuid"
	"time"
)

func main() {
	n := 0
	cron, err := gojob.New()
	if err != nil {
		golog.WithTag("gojob").Error(err)
	}
	defer cron.Stop()
	cron.Start()

	job, err := cron.DurationJob(&[]gocron.JobOption{
		gocron.WithLimitedRuns(2),                         //最大执行次数
		gocron.WithSingletonMode(gocron.LimitModeWait),    // 限制重叠执行
		gocron.WithStartAt(gocron.WithStartImmediately()), //马上开始
		gocron.WithEventListeners(
			gocron.AfterJobRunsWithError(
				func(jobID uuid.UUID, jobName string, err error) {
					golog.WithTag("AfterJobRunsWithError-gojob").Error(jobID, jobName, err.Error())
				},
			),
			gocron.AfterJobRunsWithPanic(
				func(jobID uuid.UUID, jobName string, err any) {
					golog.WithTag("AfterJobRunsWithPanic-gojob").Error(jobID, jobName, err)
				},
			),
			gocron.AfterLockError(func(jobID uuid.UUID, jobName string, err error) {
				golog.WithTag("AfterLockError-gojob").Error(jobID, jobName, err.Error())
			}),
		),
	}, 1, func(nn int) error {
		golog.WithTag("gojobStart").Info("testing->" + gconv.String(nn))
		time.Sleep(time.Second * 5)
		golog.WithTag("gojobEnd").Info("testing->" + gconv.String(nn))
		a := 1 / nn                                            //test for panic
		return fmt.Errorf("gojobEnd failed" + gconv.String(a)) //test for error
	}, n)

	if err != nil {
		golog.WithTag("gojob").Error(err)
	} else {
		golog.WithTag("gojob").Info("job.ID:" + job.ID().String())
	}

	time.Sleep(time.Second * 500)
	golog.InfoF("end of gojob")
}

func simpleUseGoJob() {
	n := 0
	cron, err := gojob.New()
	if err != nil {
		golog.WithTag("gojob").Error(err)
	}
	defer cron.Stop()
	cron.Start()

	job, err := cron.DurationJob(nil, 1, func(nn int) error {
		golog.WithTag("gojobStart").Info("testing->" + gconv.String(nn))
		time.Sleep(time.Second * 3)
		golog.WithTag("gojobEnd").Info("testing->" + gconv.String(nn))
		return nil
	}, n)

	if err != nil {
		golog.WithTag("gojob").Error(err)
	} else {
		golog.WithTag("gojob").Info("job.ID:" + job.ID().String())
	}

	time.Sleep(time.Second * 500)
	golog.InfoF("end of gojob")
}



Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CronsModel

type CronsModel struct {
	// contains filtered or unexported fields
}

func New

func New(options ...gocron.SchedulerOption) (*CronsModel, error)

func (*CronsModel) Cron

func (o *CronsModel) Cron() gocron.Scheduler

func (*CronsModel) CronJob added in v1.1.2

func (c *CronsModel) CronJob(spec string, options *[]gocron.JobOption, function any, parameters ...any) (gocron.Job, error)

spec is crontab pattern crontab 表达式

func (*CronsModel) DailyJob added in v1.1.2

func (c *CronsModel) DailyJob(options *[]gocron.JobOption, interval uint, hours []uint, minute uint, fn any, parameters ...any) (gocron.Job, error)

每天定时执行

func (*CronsModel) Day

func (c *CronsModel) Day(options *[]gocron.JobOption, fn any, parameters ...any) (gocron.Job, error)

crontab 每天0点0分0秒执行

func (*CronsModel) DayHour

func (c *CronsModel) DayHour(options *[]gocron.JobOption, hour int, fn any, parameters ...any) (gocron.Job, error)

crontab 每天x点0分0秒执行

func (*CronsModel) DayHourMinute

func (c *CronsModel) DayHourMinute(options *[]gocron.JobOption, hour, minute int, fn any, parameters ...any) (gocron.Job, error)

crontab 每天x点x分0秒执行

func (*CronsModel) DurationJob added in v1.1.2

func (c *CronsModel) DurationJob(options *[]gocron.JobOption, seconds int, fn any, parameters ...any) (gocron.Job, error)

隔多少秒执行 , 可以通过 gocron.WithSingletonMode(gocron.LimitModeWait)来限制重叠执行

func (*CronsModel) DurationRandomJob added in v1.1.2

func (c *CronsModel) DurationRandomJob(options *[]gocron.JobOption, minDuration, maxDuration time.Duration, function any, parameters ...any) (gocron.Job, error)

DurationRandomJob 定义一个新作业,该作业以提供的最小和最大持续时间值之间的随机间隔运行

func (*CronsModel) Hour

func (c *CronsModel) Hour(options *[]gocron.JobOption, fn any, parameters ...any) (gocron.Job, error)

crontab 每小时执行

func (*CronsModel) HourX

func (c *CronsModel) HourX(options *[]gocron.JobOption, x int, fn any, parameters ...any) (gocron.Job, error)

crontab 每隔x小时执行

func (*CronsModel) Minute

func (c *CronsModel) Minute(options *[]gocron.JobOption, fn any, parameters ...any) (gocron.Job, error)

crontab 每分钟执行

func (*CronsModel) MinuteX

func (c *CronsModel) MinuteX(options *[]gocron.JobOption, x int, fn any, parameters ...any) (gocron.Job, error)

crontab 每隔x分钟执行

func (*CronsModel) MonthlyJob added in v1.1.2

func (c *CronsModel) MonthlyJob(options *[]gocron.JobOption, interval uint, daysOfTheMonth []int, hours []uint, minute uint, fn any, parameters ...any) (gocron.Job, error)

interval 月频, 0-6-->周日 周一 ... 周六, hours 具体执行时间列表

func (*CronsModel) OneTimeJobForMinute added in v1.1.2

func (c *CronsModel) OneTimeJobForMinute(options *[]gocron.JobOption, minute uint, fn any, parameters ...any) (gocron.Job, error)

当前时间 minute 分钟之后执行一次

func (*CronsModel) OneTimeJobForSeconds added in v1.1.2

func (c *CronsModel) OneTimeJobForSeconds(options *[]gocron.JobOption, seconds uint, fn any, parameters ...any) (gocron.Job, error)

当前时间 seconds 秒之后执行一次

func (*CronsModel) RemoveJob added in v1.1.4

func (c *CronsModel) RemoveJob(jobID uuid.UUID) error

func (*CronsModel) Second

func (c *CronsModel) Second(options *[]gocron.JobOption, fn any, parameters ...any) (gocron.Job, error)

crontab 每秒钟执行

func (*CronsModel) SecondX

func (c *CronsModel) SecondX(options *[]gocron.JobOption, x int, fn any, parameters ...any) (gocron.Job, error)

crontab 每隔x秒执行

func (*CronsModel) Start

func (c *CronsModel) Start()

func (*CronsModel) Stop

func (c *CronsModel) Stop() error

func (*CronsModel) WeeklyJob added in v1.1.2

func (c *CronsModel) WeeklyJob(options *[]gocron.JobOption, interval uint, daysOfTheWeek []time.Weekday, hours []uint, minutes uint, fn any, parameters ...any) (gocron.Job, error)

interval 周频, 0-6-->周日 周一 ... 周六, hours 具体执行时间列表

func (*CronsModel) WithStartAt added in v1.1.4

func (c *CronsModel) WithStartAt(start time.Time) gocron.JobOption

在某一时刻运行 s, _ := NewScheduler() defer func() { _ = s.Shutdown() }()

start := time.Date(9999, 9, 9, 9, 9, 9, 9, time.UTC)

j, _ := s.NewJob(

DurationJob(
	time.Second,
),
NewTask(
	func(one string, two int) {
		fmt.Printf("%s, %d", one, two)
	},
	"one", 2,
),
WithStartAt(
	WithStartDateTime(start),
),

) s.Start()

next, _ := j.NextRun() fmt.Println(next)

_ = s.StopJobs()

定时执行启动 开始时间

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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