tasks

package
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2021 License: GPL-3.0 Imports: 10 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExecWithInterval added in v0.0.4

func ExecWithInterval(fn StoppableFunc, start, limit time.Duration)

ExecWithInterval executes a function with a dynamic interval the interval is getting multiplied by 2 in each round, up to the given limit and then it starts all over 1s > 2s > 4s > 8s ... > 1s > 2s > ...

func ExecWithTimeout

func ExecWithTimeout(ctx context.Context, fn Func, t time.Duration) (bool, interface{}, error)

ExecWithTimeout triggers some function in the given time frame, returns true if completed

Types

type Dispatcher added in v0.0.4

type Dispatcher interface {
	// Queue adds a new task
	Queue(Task)
	// Dispatch will dispatch the next task
	Dispatch()
	// Start starts ticks
	Start()
	// Stats returns the number of waiting tasks and the number of running tasks
	Stats() *DispatcherStats
}

Dispatcher maintains a queue of tasks to dispatch

func NewDispatcher added in v0.0.4

func NewDispatcher(opts DispatcherOptions) Dispatcher

NewDispatcher creates a new instance

type DispatcherOptions added in v0.0.4

type DispatcherOptions struct {
	// Ctx is a context for stopping the dispatcher
	Ctx context.Context
	// Logger used for logs
	Logger *zap.Logger
	// Interval is the time interval ticker used by dispatcher
	// if the value was not provided (zero) -> no interval will run.
	// *the calls to Dispatch() should be in a higher level
	Interval time.Duration
	// Concurrent is the limit of concurrent tasks running
	// if zero or negative (<= 0) then defaultConcurrentLimit will be used
	Concurrent int
}

DispatcherOptions describes the needed arguments for dispatcher instance

type DispatcherStats added in v0.0.4

type DispatcherStats struct {
	// Waiting is the number of tasks that waits in queue
	Waiting int
	// Running is the number of running tasks
	Running int
	// Time is the time when the stats snapshot was taken
	Time time.Time
}

DispatcherStats represents runtime stats of the dispatcher

type Func

type Func = func(stopper Stopper) (interface{}, error)

Func is the interface of functions to trigger

type StoppableFunc added in v0.0.4

type StoppableFunc func(lastTick time.Duration) (stop bool, cont bool)

StoppableFunc represents a function that returns two boolean to help with its execution stop will stop the interval, while continue will make the interval value to remain the same if both are false, the interval will be increased (x2)

type Stopper added in v0.0.4

type Stopper interface {
	// IsStopped returns true if the stopper already stopped
	IsStopped() bool
	// Chan returns a bool channel to be notified once stopped
	Chan() chan bool
}

Stopper represents the object used to stop running functions should be used by the running function, once stopped the function act accordingly

type Task added in v0.0.4

type Task struct {
	Fn func() error
	ID string
}

Task represents a some function to execute

func NewTask added in v0.0.4

func NewTask(fn func() error, id string) *Task

NewTask creates a new task

Jump to

Keyboard shortcuts

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