Skip to content

farseer-go/tasks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

66 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tasks 本地调度

包:"github.com/farseer-go/tasks"

tasks组件可以帮助我们快速编写一个定时任务,使用非常简单。

codecov Build

tasks.TaskContext 上下文

// 运行任务的上下文
type TaskContext struct { }

// 设置休眠时间
func (receiver *TaskContext) SetNextTime(nextTime time.Time)

// 设置休眠时间
func (receiver *TaskContext) SetNextDuration(d time.Duration)

提供了两个方法SetNextTimeSetNextDuration,他们只是支持的类型不同,效果是一样的

1、tasks.Run 运行任务

函数的定义

// 运行一个任务,运行前先休眠
func Run(taskName string, interval time.Duration, taskFn func(context *TaskContext), ctx context.Context)
  • taskName:任务名称
  • interval:任务运行的间隔时间
  • taskFn:要运行的任务回调函数

演示:

import "github.com/farseer-go/tasks"
import "github.com/farseer-go/fs/flog"

func main () {
    tasks.Run("testRun", 1*time.Second, testRun, context.Background())
    func testRun(context *tasks.TaskContext) {
        flog.Info("doing...")
    }
}

2、tasks.RunNow 立即运行任务

函数的定义

// 运行一个任务
func RunNow(taskName string, interval time.Duration, taskFn func(context *TaskContext), ctx context.Context)
  • taskName:任务名称
  • interval:任务运行的间隔时间
  • taskFn:要运行的任务回调函数

演示:

import "github.com/farseer-go/tasks"
import "github.com/farseer-go/fs/flog"

func main () {
    tasks.Run("testRun", 1*time.Second, testRun, context.Background())
	func RunNow(context *tasks.TaskContext) {
        flog.Info("doing...")
    }
}

说明

tasks.Runtasks.RunNow都是会执行任务,只不过tasks.Run会先根据interval参数先sleep,而tasks.RunNow立即执行