task

package
v1.6.5 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2021 License: MIT Imports: 6 Imported by: 0

README

Task

Task是一个非常易用的定时任务管理工具(移植自beego框架)。

玩过 linux 的用户都知道有一个计划任务的工具 crontab,我们经常利用该工具来定时的做一些任务,但是有些时候我们的进程内也希望定时的来处理一些事情,例如定时的汇报当前进程的内存信息,goroutine 信息等。或者定时的进行手工触发 GC,或者定时的清理一些日志数据等,所以实现了秒级别的定时任务,首先让我们看看如何使用:

  1. 初始化一个任务

     tk1 := task.NewTask("tk1", "0 12 * * * *", func() error { fmt.Println("tk1"); return nil })
    

    函数原型:

    NewTask(tname string, spec string, f TaskFunc) *Task

    • tname 任务名称
    • spec 定时任务格式,请参考下面的详细介绍
    • f 执行的函数 func() error
  2. 可以测试开启运行

    可以通过如下的代码运行 TaskFunc,和 spec 无关,用于检测写的函数是否如预期所希望的这样:

     err := tk.Run()
     if err != nil {
         t.Fatal(err)
     }
    
  3. 加入全局的计划任务列表

     task.AddTask("tk1", tk1)
    
  4. 开始执行全局的任务

     task.StartTask()
     defer task.StopTask()
    

spec 详解

spec 格式是参照 crontab 做的,详细的解释如下所示:

//前6个字段分别表示:
//       秒钟:0-59
//       分钟:0-59
//       小时:1-23
//       日期:1-31
//       月份:1-12
//       星期:0-6(0 表示周日)

//还可以用一些特殊符号:
//       *: 表示任何时刻
//       ,: 表示分割,如第三段里:2,4,表示 2 点和 4 点执行
//      -:表示一个段,如第三端里: 1-5,就表示 1 到 5 点
//       /n : 表示每个n的单位执行一次,如第三段里,*/1, 就表示每隔 1 个小时执行一次命令。也可以写成1-23/1.
/////////////////////////////////////////////////////////
//  0/30 * * * * *                        每 30 秒 执行
//  0 43 21 * * *                         21:43 执行
//  0 15 05 * * *                        05:15 执行
//  0 0 17 * * *                          17:00 执行
//  0 0 17 * * 1                          每周一的 17:00 执行
//  0 0,10 17 * * 0,2,3                   每周日,周二,周三的 17:00和 17:10 执行
//  0 0-10 17 1 * *                       毎月1日从 17:00 到 7:10 毎隔 1 分钟 执行
//  0 0 0 1,15 * 1                        毎月1日和 15 日和 一日的 0:00 执行
//  0 42 4 1 * *                         毎月1日的 4:42 分 执行
//  0 0 21 * * 1-6                       周一到周六 21:00 执行
//  0 0,10,20,30,40,50 * * * *            每隔 10 分 执行
//  0 */10 * * * *                     每隔 10 分 执行
//  0 * 1 * * *                       从 1:0 到 1:59 每隔 1 分钟 执行
//  0 0 1 * * *                       1:00 执行
//  0 0 */1 * * *                      毎时 0 分 每隔 1 小时 执行
//  0 0 * * * *                       毎时 0 分 每隔 1 小时 执行
//  0 2 8-20/3 * * *                   8:02,11:02,14:02,17:02,20:02 执行
//  0 30 5 1,15 * *                    1 日 和 15 日的 5:30 执行
``

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	AdminTaskList map[string]Tasker
)

The bounds for each field.

Functions

func AddTask

func AddTask(taskname string, t Tasker)

AddTask add task with name

func DeleteTask

func DeleteTask(taskname string)

DeleteTask delete task with name

func StartTask

func StartTask()

StartTask start all tasks

func StopTask

func StopTask()

StopTask stop all tasks

Types

type MapSorter

type MapSorter struct {
	Keys []string
	Vals []Tasker
}

MapSorter sort map for tasker

func NewMapSorter

func NewMapSorter(m map[string]Tasker) *MapSorter

NewMapSorter create new tasker map

func (*MapSorter) Len

func (ms *MapSorter) Len() int

func (*MapSorter) Less

func (ms *MapSorter) Less(i, j int) bool

func (*MapSorter) Sort

func (ms *MapSorter) Sort()

Sort sort tasker map

func (*MapSorter) Swap

func (ms *MapSorter) Swap(i, j int)

type Schedule

type Schedule struct {
	Second uint64
	Minute uint64
	Hour   uint64
	Day    uint64
	Month  uint64
	Week   uint64
}

Schedule time taks schedule

func (*Schedule) Next

func (s *Schedule) Next(t time.Time) time.Time

Next set schedule to next time

type Task

type Task struct {
	Taskname string
	Spec     *Schedule
	SpecStr  string
	DoFunc   TaskFunc
	Prev     time.Time
	Next     time.Time
	Errlist  []*taskerr // like errtime:errinfo
	ErrLimit int        // max length for the errlist, 0 stand for no limit
}

Task task struct

func NewTask

func NewTask(tname string, spec string, f TaskFunc) *Task

NewTask add new task with name, time and func

func (*Task) GetNext

func (t *Task) GetNext() time.Time

GetNext get the next call time of this task

func (*Task) GetPrev

func (t *Task) GetPrev() time.Time

GetPrev get prev time of this task

func (*Task) GetSpec

func (t *Task) GetSpec() string

GetSpec get spec string

func (*Task) GetStatus

func (t *Task) GetStatus() string

GetStatus get current task status

func (*Task) Run

func (t *Task) Run() error

Run run all tasks

func (*Task) SetCron

func (t *Task) SetCron(spec string)

SetCron some signals:

*: any time
,:  separate signal

   -:duration

/n : do as n times of time duration

///////////////////////////////////////////////////////

0/30 * * * * *                        every 30s
0 43 21 * * *                         21:43
0 15 05 * * *                        05:15
0 0 17 * * *                          17:00
0 0 17 * * 1                           17:00 in every Monday
0 0,10 17 * * 0,2,3                   17:00 and 17:10 in every Sunday, Tuesday and Wednesday
0 0-10 17 1 * *                       17:00 to 17:10 in 1 min duration each time on the first day of month
0 0 0 1,15 * 1                        0:00 on the 1st day and 15th day of month
0 42 4 1 * *                         4:42 on the 1st day of month
0 0 21 * * 1-6                       21:00 from Monday to Saturday
0 0,10,20,30,40,50 * * * *            every 10 min duration
0 */10 * * * *                     every 10 min duration
0 * 1 * * *                       1:00 to 1:59 in 1 min duration each time
0 0 1 * * *                       1:00
0 0 */1 * * *                      0 min of hour in 1 hour duration
0 0 * * * *                       0 min of hour in 1 hour duration
0 2 8-20/3 * * *                   8:02, 11:02, 14:02, 17:02, 20:02
0 30 5 1,15 * *                    5:30 on the 1st day and 15th day of month

func (*Task) SetNext

func (t *Task) SetNext(now time.Time)

SetNext set next time for this task

func (*Task) SetPrev

func (t *Task) SetPrev(now time.Time)

SetPrev set prev time of this task

type TaskFunc

type TaskFunc func() error

TaskFunc task func type

type Tasker

type Tasker interface {
	GetSpec() string
	GetStatus() string
	Run() error
	SetNext(time.Time)
	GetNext() time.Time
	SetPrev(time.Time)
	GetPrev() time.Time
}

Tasker task interface

Jump to

Keyboard shortcuts

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