crond

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2024 License: MIT Imports: 10 Imported by: 0

README

crond lib

crond包基于github.com/go-co-op/gocron的基础上,添加了分布式锁定操作; 基于分布式锁的操作,用以解决在集群模式下,多cron可能在同一时刻发起调度的问题

使用

单机环境下执行cron调度
func ExampleStartTest() {
	var job1Fn = func(jobID int) {
		log.Printf("exec job[#%d] fn", jobID)
	}

	// 准备job任务
	job1 := NewJob("job1", func() { job1Fn(1) }, "* * * * *")
	job2 := NewJob("job2", func() { job1Fn(2) }, "*/2 * * * *")

	// 准备scheduler调度器
	locker, err := NewRedisLocker("redis://user:password@localhost:6789/3?dial_timeout=3&db=1&read_timeout=6s&max_retries=2")
	if err != nil {
		log.Fatalf("parse redis url got err: %s", err)
	}

	crond := NewScheduler(&Config{
		Async:            false,           // 不阻塞主协程
		SingletonModeAll: true,            // 调度器不会重复调度同类型新的job任务
	}, locker)

	// 添加job任务
	err = crond.AddJobs(job1, job2)
	if err != nil {
		log.Fatalf("crond add job got err: %s", err)
	}

	// job执行
	crond.Start()
}

Documentation

Overview

Package cron is a generated GoMock package.

Index

Constants

View Source
const (
	ExpressEveryMin   = "* * * * *"
	ExpressEvery5Min  = "*/5 * * * *"
	ExpressEvery10Min = "*/10 * * * *"
	ExpressEvery30Min = "*/30 * * * *"
	ExpressEveryHour  = "0 * * * *" // 每小时整点执行
	ExpressEveryDay   = "0 1 * * *" // 凌晨1:00执行
	ExpressEveryMonth = "0 1 1 * *" // 每个月
)

cron格式`*/1 * * * *`,分(0-59) 时(0-23) 日(1-31) 月(1-12) 天(0-6)

Variables

View Source
var DefaultScheduleCfg = &SchedulerCfg{
	TimeZone:         "Asia/Shanghai",
	Async:            true,
	SingletonModeAll: true,
}

DefaultScheduleCfg 默认Schedule配置

Functions

This section is empty.

Types

type DistributeLocker

type DistributeLocker interface {
	Lock(key string, ttl time.Duration) error
	UnLock(key string) error
}

DistributeLocker 分布式锁接口 该接口主要是针对多个JOB执行时刻,进行并发控制,确保在不同机器上的Job仅执行一次行

type MockLocker

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

MockLocker is a mock of DistributeLocker interface

func NewMockLocker

func NewMockLocker(ctrl *gomock.Controller) *MockLocker

NewMockLocker creates a new mock instance

func (*MockLocker) EXPECT

func (m *MockLocker) EXPECT() *MockLockerMockRecorder

EXPECT returns an object that allows the caller to indicate expected use

func (*MockLocker) Lock

func (m *MockLocker) Lock(key string, ttl time.Duration) error

Lock mocks base method

func (*MockLocker) UnLock

func (m *MockLocker) UnLock(key string) error

UnLock mocks base method

type MockLockerMockRecorder

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

MockLockerMockRecorder is the mock recorder for MockLocker

func (*MockLockerMockRecorder) Lock

func (mr *MockLockerMockRecorder) Lock(key, ttl interface{}) *gomock.Call

Lock indicates an expected call of Lock

func (*MockLockerMockRecorder) UnLock

func (mr *MockLockerMockRecorder) UnLock(key interface{}) *gomock.Call

UnLock indicates an expected call of UnLock

type RedisLocker

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

RedisLocker 基于Redis实现的分布式锁,

func NewRedisLocker

func NewRedisLocker(redisURL string) (*RedisLocker, error)

NewRedisLocker 创建一个Redis分布式锁 Examples:

redis://user:password@localhost:6789/3?dial_timeout=3&db=1&read_timeout=6s&max_retries=2

func (*RedisLocker) Lock

func (r *RedisLocker) Lock(key string, ttl time.Duration) error

Lock 分布式加锁

func (*RedisLocker) UnLock

func (r *RedisLocker) UnLock(key string) error

UnLock 分布式解锁

type Scheduler

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

Scheduler Cron调度程序

func NewScheduler

func NewScheduler(cfg *SchedulerCfg) (*Scheduler, error)

NewScheduler 初始化一个cron 调度器

func (*Scheduler) AddTasks

func (s *Scheduler) AddTasks(tasks ...*Task) error

AddTasks 新增一个cron task任务

func (*Scheduler) Start

func (s *Scheduler) Start()

Start 启动Cron定时服务

type SchedulerCfg

type SchedulerCfg struct {
	TimeZone         string // 时区配置,默认为Asia/Shanghai
	Async            bool   // 启动job的方式是阻塞还是非阻塞
	SingletonModeAll bool   // 启动job是否采用单例模式,单例模式下若job如果之前有运行且未完成,则调度器不会重复调度同类型新的job任务(若无特殊要求,推荐开启)
}

SchedulerCfg Cron调度器配置

type Task

type Task struct {
	Name    string // job描述名称
	Express string // cron格式`*/1 * * * *`,分(0-59) 时(0-23) 日(1-31) 月(1-12) 周天(0-6)
	Exec    func() // job调度后执行的内容

	Locker DistributeLocker // 分布式Redis锁
	TTL    time.Duration    // 分布式锁,过期时间
}

Task 待执行的JOB任务

func NewTask

func NewTask(name string, exec func(), express string) *Task

NewTask 创建一个任务

func (*Task) Execute

func (t *Task) Execute()

Execute 任务执行

func (*Task) SetDistributeLocker

func (t *Task) SetDistributeLocker(locker DistributeLocker, ttl time.Duration) *Task

SetDistributeLocker 设置task任务依赖的分布式锁,以及持锁时间 更细粒度的cron任务持有的分布式锁,可以基于task自定义,否则采用默认调度器的统一持锁时间

func (*Task) SingleExec

func (t *Task) SingleExec() func()

SingleExec 单机执行

Jump to

Keyboard shortcuts

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