gscheduler

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2024 License: MIT Imports: 8 Imported by: 2

README

gscheduler

简介

gscheduler 是golang实现的一个简单的任务调度器。 实现功能:

  • 任务的添加
  • 任务的删除
  • 任务的修改
  • 任务的查询

实例

package main

import (
	"fmt"
	"time"

	"github.com/vine-io/gscheduler"
)

func main() {
	scheduler := gscheduler.NewScheduler()
	scheduler.Start()

	a := 1

	job1 := gscheduler.JobBuilder().Name("job1").Duration(time.Second).Fn(func() {
		fmt.Printf("[%s] a = %d\n", time.Now(), a)
		a++
	}).Out()

	job2 := gscheduler.JobBuilder().Name("job2").Duration(time.Second).Fn(func() {
		fmt.Println("job2", time.Now())
	}).Out()

	// 添加任务
	scheduler.AddJob(job1)
	scheduler.AddJob(job2)
	
	// 删除任务
	scheduler.RemoveJob(job1)
    
	// 修改任务
    scheduler.UpdateJob(job2)
	
	// 查询任务
	scheduler.GetJob(job2.ID())

	time.Sleep(time.Second * 10)
}

构建任务

func main() {
	// 构建一个符合正则表达式的任务
	c, _ := cron.Parse("*/10 * * * * * *")
	gscheduler.JobBuilder().Name("cron-job").Spec(c).Out()

	// 构建一次性延时任务
	gscheduler.JobBuilder().Name("delay-job").Delay(time.Now().Add(time.Hour * 3)).Out()

	// 构建间隔执行的任务
	gscheduler.JobBuilder().Name("duration-job").Duration(time.Second * 10).Out()
	
	// 构建多次执行的任务
	gscheduler.JobBuilder().Name("three-times-job").Duration(time.Second*5).Times(3).Out()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddJob

func AddJob(job *Job) error

func Count

func Count() uint64

func FromJob

func FromJob(job *Job) *builder

FromJob get build of Job

func JobBuilder

func JobBuilder() *builder

JobBuilder the builder of Job

examples:

c, err := cron.Parse("*/10 * * * * * *")
job := JobBuilder().Id("jobid").Name("cron-job").Spec(c).Out()

job := JobBuilder().Name("delay-job").Delay(time.Now().Add(time.Hour*3)).Out()

job := JobBuilder().Name("duration-job").Duration(time.Second*10).Out()

job := JobBuilder().Name("once-job").Duration(time.Second*5).Times(1).Out()

func RemoveJob

func RemoveJob(job *Job) error

func Reset

func Reset()

func Start

func Start()

func Stop

func Stop()

func StopGraceful

func StopGraceful()

func UpdateJob

func UpdateJob(job *Job) error

Types

type Job

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

func GetJob

func GetJob(id string) (*Job, error)

func GetJobs

func GetJobs() ([]*Job, error)

func (Job) ID

func (j Job) ID() string

func (Job) LastTime

func (j Job) LastTime() time.Time

func (*Job) Less

func (j *Job) Less(another rbtree.Item) bool

func (Job) Name

func (j Job) Name() string

func (Job) NextTime

func (j Job) NextTime() time.Time

func (*Job) SetCron

func (j *Job) SetCron(cron cron.Crontab)

func (*Job) SetFn

func (j *Job) SetFn(fn func())

func (*Job) SetTimes

func (j *Job) SetTimes(t uint64)

func (*Job) Start

func (j *Job) Start()

type Scheduler

type Scheduler interface {
	GetJobs() ([]*Job, error)
	GetJob(id string) (*Job, error)
	AddJob(job *Job) error
	Count() uint64
	UpdateJob(job *Job) error
	RemoveJob(job *Job) error
	Start()
	Stop()
	StopGraceful()
	Reset()
}

func NewScheduler

func NewScheduler() Scheduler

type Status

type Status string
const (
	Waiting Status = "waiting"
	Running Status = "running"
)

type Store

type Store interface {
	GetJobs() ([]*Job, error)
	GetByName(string) (*Job, error)
	GetById(string) (*Job, error)
	Count() uint
	Put(*Job) error
	Del(*Job) error
	Min() (*Job, error)
}

Directories

Path Synopsis
Package rbtree implements operations on Red-Black tree.
Package rbtree implements operations on Red-Black tree.

Jump to

Keyboard shortcuts

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