task

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

README

go-task Build Status GoDoc License

It supplies some task runners to run the task. Support Go1.7+.

Installation

$ go get -u github.com/xgfone/go-task

Example

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/xgfone/go-task"
)

type tasker struct {
	name string
}

func (t tasker) Name() string { return t.name }
func (t tasker) Run(ctx context.Context, now time.Time) (err error) {
	fmt.Printf("[%s] run task '%s'\n", now.Format(time.RFC3339), t.name)
	return
}

func newTask(name string) task.Task { return tasker{name: name} }

func runTask(ctx context.Context, now time.Time) (err error) {
	t := task.GetTaskFromCtx(ctx)
	fmt.Printf("[%s] run task '%s'\n", now.Format(time.RFC3339), t.Name())
	return
}

func main() {
	config := task.IntervalRunnerConfig{Tick: time.Second, Interval: time.Second * 3}
	runner := task.NewIntervalRunner(&config)

	// Add all the tasks
	runner.AddTask(newTask("task1"))                                        // Use Default Interval: 3s
	runner.AddTask(task.NewTask("task2", runTask))                          // Use Default Interval: 3s
	runner.AddTask(task.NewIntervalFuncTask("task3", time.Second, runTask)) // Use Special Interval: 1s

	// We only run the tasks for 10s.
	time.Sleep(time.Second * 10)
	runner.Stop()

	// Output:
	// [2021-06-03T23:41:14+08:00] run task 'task3'
	// [2021-06-03T23:41:14+08:00] run task 'task1'
	// [2021-06-03T23:41:14+08:00] run task 'task2'
	// [2021-06-03T23:41:16+08:00] run task 'task3'
	// [2021-06-03T23:41:17+08:00] run task 'task2'
	// [2021-06-03T23:41:17+08:00] run task 'task3'
	// [2021-06-03T23:41:17+08:00] run task 'task1'
	// [2021-06-03T23:41:19+08:00] run task 'task3'
	// [2021-06-03T23:41:20+08:00] run task 'task2'
	// [2021-06-03T23:41:20+08:00] run task 'task3'
	// [2021-06-03T23:41:20+08:00] run task 'task1'
	// [2021-06-03T23:41:22+08:00] run task 'task3'
}

Documentation

Overview

Package task supplies some task runners to run the task.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CanRunVip

func CanRunVip(vip string) func() bool

CanRunVip returns a function to reports whether the task can be run based on vip.

Return true if vip is empty.

Types

type IntervalRunnerConfig

type IntervalRunnerConfig struct {
	// The default interval that the task is run between twice.
	//
	// Default: 1m
	Interval time.Duration

	// Jitter is used to produce a random duration, [0, Jitter],
	// to be added to the interval duration of the task to avoid
	// converging on periodic behavior. If 0, disalbe it.
	//
	// Default: 0
	Jitter time.Duration

	// The clock tick interval.
	//
	// Default: 5s
	Tick time.Duration

	// Report whether the tasks can be run or not.
	// If nil, it is equal to return true.
	//
	// Default: nil
	CanRun func() bool

	// ErrorLog is used to log the error returned by the task, if set.
	//
	// Default: log.Printf
	ErrorLog func(fmt string, args ...interface{})
}

IntervalRunnerConfig is used to configure the interval runner.

type IntervalTask

type IntervalTask interface {
	Interval() time.Duration
	Task
}

IntervalTask is the interval task to be run.

func NewIntervalFuncTask

func NewIntervalFuncTask(name string, interval time.Duration, run TaskFunc) IntervalTask

NewIntervalFuncTask is equal to NewIntervalTask(interval, NewTask(name, run)).

func NewIntervalTask

func NewIntervalTask(interval time.Duration, task Task) IntervalTask

NewIntervalTask returns a new interval task.

type Runner

type Runner interface {
	AddTask(Task)
	DelTask(Task)
	Stop()
}

Runner is a task runner.

func NewIntervalRunner

func NewIntervalRunner(config *IntervalRunnerConfig) Runner

NewIntervalRunner returns a new runner to run the tasks at regular intervals.

Example
package main

import (
	"context"
	"fmt"
	"time"
)

func newTask(name string) Task {
	return NewTask(name, func(ctx context.Context, now time.Time) error {
		fmt.Printf("%s: run task '%s'\n", now.UTC().Format(time.RFC3339), name)
		return nil
	})
}

func main() {
	runner := NewIntervalRunner(&IntervalRunnerConfig{
		Interval: time.Second * 3,
		Tick:     time.Second,
	})

	runner.AddTask(newTask("task1"))
	runner.AddTask(newTask("task2"))
	runner.AddTask(NewIntervalTask(time.Second*5, newTask("task3")))
	runner.AddTask(NewIntervalTask(time.Second*10, newTask("task4")))

	time.Sleep(time.Second * 25)
	runner.Stop()

	// It will output like this:
	// 2021-06-03T14:56:36Z: run task 'task3'
	// 2021-06-03T14:56:36Z: run task 'task4'
	// 2021-06-03T14:56:36Z: run task 'task1'
	// 2021-06-03T14:56:36Z: run task 'task2'
	// 2021-06-03T14:56:39Z: run task 'task2'
	// 2021-06-03T14:56:39Z: run task 'task1'
	// 2021-06-03T14:56:42Z: run task 'task2'
	// 2021-06-03T14:56:42Z: run task 'task3'
	// 2021-06-03T14:56:42Z: run task 'task1'
	// 2021-06-03T14:56:46Z: run task 'task1'
	// 2021-06-03T14:56:46Z: run task 'task4'
	// 2021-06-03T14:56:46Z: run task 'task2'
	// 2021-06-03T14:56:48Z: run task 'task3'
	// 2021-06-03T14:56:49Z: run task 'task1'
	// 2021-06-03T14:56:49Z: run task 'task2'
	// 2021-06-03T14:56:53Z: run task 'task2'
	// 2021-06-03T14:56:53Z: run task 'task3'
	// 2021-06-03T14:56:53Z: run task 'task1'
	// 2021-06-03T14:56:56Z: run task 'task4'
	// 2021-06-03T14:56:56Z: run task 'task1'
	// 2021-06-03T14:56:56Z: run task 'task2'
	// 2021-06-03T14:56:58Z: run task 'task3'

}
Output:

type Task

type Task interface {
	Name() string
	Run(ctx context.Context, now time.Time) error
}

Task represents a task.

func GetTaskFromCtx

func GetTaskFromCtx(ctx context.Context) Task

GetTaskFromCtx returns the task from the context.

func NewTask

func NewTask(name string, run TaskFunc) Task

NewTask returns a new Task.

type TaskFunc

type TaskFunc func(ctx context.Context, now time.Time) error

TaskFunc is the task function.

Jump to

Keyboard shortcuts

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