task

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: May 2, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FinishReasonTimeout = "timeout"
)

Variables

View Source
var (
	ErrTaskTypeNotFound  = errors.New("task type not found")
	ErrTaskAlreadyExists = errors.New("task already exists")
	ErrTaskNotFound      = errors.New("task not found")
)
View Source
var (
	WithTaskName = func(name string) CreateOption {
		return func(t *Task) {
			t.Name = name
		}
	}
	WithTimeout = func(timeout int64) CreateOption {
		return func(t *Task) {
			t.Timeout = timeout
		}
	}
	WithId = func(id string) CreateOption {
		return func(t *Task) {
			t.Id = id
		}
	}
	UniqueTask = func(unique ...bool) CreateOption {
		return func(t *Task) {
			if len(unique) > 0 {
				t.Unique = unique[0]
			} else {
				t.Unique = true
			}
		}
	}
	WithMeta = func(meta map[string]any) CreateOption {
		return func(t *Task) {
			t.Meta = meta
		}
	}
)

Functions

func Init

func Init(_ context.Context)

func RegisterHandler

func RegisterHandler(name string, handler Handler)

Types

type CreateOption

type CreateOption func(t *Task)

type Handler

type Handler interface {
	Created(ctx context.Context, task *Task)
	Handle(ctx context.Context, task *Task) (err error)
	Archive(ctx context.Context, task *Task) (delete bool)
}

type Status

type Status int
const (
	StatusCreated Status = iota
	StatusSuccess
	StatusFailed
)

type Task

type Task struct {
	Name         string         `json:"name"`
	Id           string         `json:"id"`
	Type         string         `json:"type"`
	Status       Status         `json:"status"`
	CreatedAt    int64          `json:"created_at"`
	FinishedAt   int64          `json:"finished_at"`
	FinishReason string         `json:"finish_reason"`
	Timeout      int64          `json:"timeout"` // second, default 30
	Unique       bool           `json:"unique"`
	Meta         map[string]any `json:"meta"`
	// contains filtered or unexported fields
}

func Create

func Create(ctx context.Context, typ string, opts ...CreateOption) (t *Task, err error)

func GetTask

func GetTask(ctx context.Context, typ, id string) (t *Task, err error)

func ListTasks

func ListTasks(ctx context.Context) (ts []*Task, err error)

func (*Task) Wait

func (t *Task) Wait(ctx context.Context)

Jump to

Keyboard shortcuts

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