gojob

package
v1.2.15 Latest Latest
Warning

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

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

README

定时任务

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 GoJob added in v1.1.7

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

func New

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

func (*GoJob) Cron added in v1.1.7

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

func (*GoJob) CronJob added in v1.1.7

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

spec is crontab pattern crontab 表达式

func (*GoJob) DailyJob added in v1.1.7

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

每天定时执行

func (*GoJob) Day added in v1.1.7

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

crontab 每天0点0分0秒执行

func (*GoJob) DayHour added in v1.1.7

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

crontab 每天x点0分0秒执行

func (*GoJob) DayHourMinute added in v1.1.7

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

crontab 每天x点x分0秒执行

func (*GoJob) DurationJob added in v1.1.7

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

隔多少秒执行

func (*GoJob) DurationRandomJob added in v1.1.7

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

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

func (*GoJob) Hour added in v1.1.7

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

crontab 每小时执行

func (*GoJob) HourX added in v1.1.7

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

crontab 每隔x小时执行

func (*GoJob) Minute added in v1.1.7

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

crontab 每分钟执行

func (*GoJob) MinuteX added in v1.1.7

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

crontab 每隔x分钟执行

func (*GoJob) MonthlyJob added in v1.1.7

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

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

func (*GoJob) NewJob added in v1.1.9

func (c *GoJob) NewJob(jobDefinition gocron.JobDefinition, task gocron.Task, options ...gocron.JobOption) (gocron.Job, error)

func (*GoJob) OneTimeJobForMinute added in v1.1.7

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

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

func (*GoJob) OneTimeJobForSeconds added in v1.1.7

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

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

func (*GoJob) RemoveJob added in v1.1.7

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

func (*GoJob) Second added in v1.1.7

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

crontab 每秒钟执行

func (*GoJob) SecondX added in v1.1.7

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

crontab 每隔x秒执行

func (*GoJob) Start added in v1.1.7

func (c *GoJob) Start()

func (*GoJob) Stop added in v1.1.7

func (c *GoJob) Stop() error

func (*GoJob) WeeklyJob added in v1.1.7

func (c *GoJob) 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 (*GoJob) WithStartAt added in v1.1.7

func (c *GoJob) 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