task

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2023 License: MIT Imports: 6 Imported by: 0

README

Task module

Task runner

Files: task_config.go, task_runner.go, task_test.go, task.go

Task scheduler is a simple scheduler that runs tasks in a given interval. It is used to run tasks that need to be run periodically.

Job queue

Files: job_queue.go

Job queue is a simple queue that can be used to queue jobs that need to be run in a separate goroutine.

Retry logic

File: retry.go

This file has a single Retry function which retries the given config if it fails.

Command runner

TODO

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Retry added in v0.1.0

func Retry(
	ctx context.Context,
	maxRetry int,
	callback RetryCallback,
	backoff RetryBackoff,
	errHook func(err error),
) (err error)

Types

type Handler added in v0.0.15

type Handler func(*Task) error

type Job added in v0.1.0

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

type JobHandler added in v0.1.0

type JobHandler func(ctx context.Context, arg any) error

type JobQueue added in v0.1.0

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

func NewJobQueue added in v0.1.0

func NewJobQueue(size int) *JobQueue

func (*JobQueue) Close added in v0.1.0

func (q *JobQueue) Close() error

Close closes the queue and waits for all jobs to finish

func (*JobQueue) Queue added in v0.1.0

func (q *JobQueue) Queue(job JobHandler, arg ...any)

Queue adds a new job to the queue

type RetryBackoff added in v0.1.0

type RetryBackoff func(retry int) time.Duration

type RetryCallback added in v0.1.0

type RetryCallback func(count int) (stop bool, err error)

type RunnerContainer added in v0.2.6

type RunnerContainer struct {
	Configs   []*TaskConfig
	Callbacks []Handler
	Runners   []*TaskRunner
}

func NewRunnerContainer added in v0.2.6

func NewRunnerContainer() *RunnerContainer

func (*RunnerContainer) Append added in v0.2.6

func (r *RunnerContainer) Append(cfg *TaskConfig, cb Handler)

func (*RunnerContainer) CloseAll added in v0.2.6

func (r *RunnerContainer) CloseAll() error

func (*RunnerContainer) StartAll added in v0.2.6

func (r *RunnerContainer) StartAll() error

func (*RunnerContainer) StartAllContext added in v0.2.6

func (r *RunnerContainer) StartAllContext(ctx context.Context) error

type Task

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

Task is passed to task handlers and contains the payload

func (*Task) Args added in v0.0.15

func (t *Task) Args() []any

Args returns the task arguments.

func (*Task) Elapsed

func (t *Task) Elapsed() time.Duration

Elapsed returns the time elapsed since the last run.

func (*Task) Id

func (t *Task) Id() uuid.UUID

Id returns the task id.

func (*Task) Name added in v0.2.7

func (t *Task) Name() string

func (*Task) Stop

func (t *Task) Stop()

Stop stops the task.

type TaskConfig

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

TaskConfig is responsible for configuring a task and scheduling it.

func Every added in v0.0.15

func Every(interval ...int) *TaskConfig

Every begins configuring a task. supply zero or one intervals. no intervals will be counted as 1

func Once added in v0.0.15

func Once() *TaskConfig

Once begins configuring a task. sets the task to run only once. use Config.After or TaskConfig.From to set the time.

func (*TaskConfig) After

func (c *TaskConfig) After(interval time.Duration) *TaskConfig

After starts the task after the specified duration. Short hand for From(time.Now().Add(interval))

func (*TaskConfig) At

func (c *TaskConfig) At(hour, minute int) *TaskConfig

At sets the time of day to run the task.

func (*TaskConfig) Clone added in v0.2.7

func (c *TaskConfig) Clone() *TaskConfig

Clone

func (*TaskConfig) Day

func (c *TaskConfig) Day() *TaskConfig

Day sets the interval to days.

func (*TaskConfig) Days

func (c *TaskConfig) Days() *TaskConfig

Days is same as Day.

func (*TaskConfig) Do

func (c *TaskConfig) Do(f Handler, args ...any) (r *TaskRunner, err error)

Do run the task with the supplied payload in a new goroutine.

func (*TaskConfig) DoContext added in v0.2.4

func (c *TaskConfig) DoContext(ctx context.Context, f Handler, args ...any) (r *TaskRunner, err error)

DoContext run the task with the supplied payload in a new goroutine.

func (*TaskConfig) Friday

func (c *TaskConfig) Friday() *TaskConfig

Friday sets the unit to weeks and only runs on Fridays.

func (*TaskConfig) From

func (c *TaskConfig) From(from time.Time) *TaskConfig

From sets the run time of the task.

func (*TaskConfig) Hour

func (c *TaskConfig) Hour() *TaskConfig

Hour sets the interval to hours.

func (*TaskConfig) Hours

func (c *TaskConfig) Hours() *TaskConfig

Hours is same as Hour.

func (*TaskConfig) Millisecond

func (c *TaskConfig) Millisecond() *TaskConfig

Millisecond sets the interval to milliseconds.

func (*TaskConfig) Milliseconds

func (c *TaskConfig) Milliseconds() *TaskConfig

Milliseconds is same as Millisecond.

func (*TaskConfig) Minute

func (c *TaskConfig) Minute() *TaskConfig

Minute sets the interval to minutes.

func (*TaskConfig) Minutes

func (c *TaskConfig) Minutes() *TaskConfig

Minutes is same as Minute.

func (*TaskConfig) Monday

func (c *TaskConfig) Monday() *TaskConfig

Monday sets the unit to weeks and only runs on Mondays.

func (*TaskConfig) OnBeforeStart

func (c *TaskConfig) OnBeforeStart(f Handler) *TaskConfig

OnBeforeStart sets the handler to be called before the task starts.

func (*TaskConfig) OnFinish

func (c *TaskConfig) OnFinish(f Handler) *TaskConfig

OnFinish sets the finish handler for the task.

func (*TaskConfig) Saturday

func (c *TaskConfig) Saturday() *TaskConfig

Saturday sets the unit to weeks and only runs on Saturdays.

func (*TaskConfig) Second

func (c *TaskConfig) Second() *TaskConfig

Second sets the interval to seconds.

func (*TaskConfig) Seconds

func (c *TaskConfig) Seconds() *TaskConfig

Seconds is same as Second.

func (*TaskConfig) Sunday

func (c *TaskConfig) Sunday() *TaskConfig

Sunday sets the unit to weeks and only runs on Sundays.

func (*TaskConfig) Thursday

func (c *TaskConfig) Thursday() *TaskConfig

Thursday sets the unit to weeks and only runs on Thursdays.

func (*TaskConfig) To

func (c *TaskConfig) To(to time.Time) *TaskConfig

To sets the end time of the task.

func (*TaskConfig) Tuesday

func (c *TaskConfig) Tuesday() *TaskConfig

Tuesday sets the unit to weeks and only runs on Tuesdays.

func (*TaskConfig) Wednesday

func (c *TaskConfig) Wednesday() *TaskConfig

Wednesday sets the unit to weeks and only runs on Wednesdays.

func (*TaskConfig) Week

func (c *TaskConfig) Week() *TaskConfig

Week sets the interval to weeks.

func (*TaskConfig) Weeks

func (c *TaskConfig) Weeks() *TaskConfig

Weeks is same as Week.

func (*TaskConfig) WithName added in v0.2.7

func (c *TaskConfig) WithName(name string) *TaskConfig

type TaskRunner added in v0.1.0

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

TaskRunner is responsible for running the task given to them

func (*TaskRunner) Close added in v0.1.0

func (r *TaskRunner) Close() error

Close closes the task runner.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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