entity

package
v0.8.9 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package entity contains domain entities for the task queue system.

Index

Constants

View Source
const (
	// TaskStatusNew is a new task.
	TaskStatusNew = "new"
	// TaskStatusPending is a task waiting to be processed.
	TaskStatusPending = "pending"
	// TaskStatusProcessing is a task in progress.
	TaskStatusProcessing = "processing"
	// TaskStatusDone is a task that was processed successfully.
	TaskStatusDone = "done"
	// TaskStatusCanceled is a canceled task.
	TaskStatusCanceled = "canceled"
	// TaskStatusError is a task is processed with an error and HAS attempts.
	TaskStatusError = "error"
	// TaskStatusAttemptsLeft is a task that was processed with an error and NO attempts.
	TaskStatusAttemptsLeft = "attempts_left"
)

Task status constants define the possible states of a task in the queue.

View Source
const NoTaskPayload = "{}"

NoTaskPayload is an empty JSON object payload for tasks without input data.

Variables

View Source
var (
	// ErrDuplicateTask is returned when attempting to insert a task with a duplicate external ID.
	ErrDuplicateTask = errors.New("task already exists")
	// ErrInvalidPayloadFormat is returned when the task payload is not valid JSON.
	ErrInvalidPayloadFormat = errors.New("payload format is invalid. should be json")
	// ErrPayloadUnmarshal is returned when a typed task payload cannot be unmarshaled from JSON.
	ErrPayloadUnmarshal = errors.New("payload unmarshal")
	// ErrPayloadMarshal is returned when a typed task payload cannot be marshaled to JSON.
	ErrPayloadMarshal = errors.New("payload marshal")

	// ErrTaskCancel is returned when a task is canceled during processing.
	ErrTaskCancel = errors.New("task canceled")
	// ErrTaskTimeout is returned when task processing exceeds the timeout limit.
	ErrTaskTimeout = errors.New("task processing timeout")
)

Functions

This section is empty.

Types

type Metadata added in v0.3.0

type Metadata map[string]any

Metadata represents arbitrary key-value data associated with a task for tracking and context.

func NewMetadataFromJSON added in v0.3.0

func NewMetadataFromJSON(ctx context.Context, metadata *string) Metadata

NewMetadataFromJSON deserializes a JSON string into a Metadata map.

func (Metadata) Merge added in v0.3.0

func (m Metadata) Merge(metadata Metadata) Metadata

Merge combines the current metadata with another metadata map, with values from the parameter taking precedence.

func (Metadata) ToJSON added in v0.3.0

func (m Metadata) ToJSON(ctx context.Context) string

ToJSON serializes the metadata map into a JSON string.

type Task

type Task struct {
	ID            uuid.UUID
	Type          TaskType
	ExternalID    string
	Payload       string
	Status        TaskStatus
	Attempts      int32
	Errors        *string
	Metadata      Metadata
	CreatedAt     time.Time
	UpdatedAt     *time.Time
	NextAttemptAt time.Time
}

Task represents a unit of work in the queue system.

func NewTask

func NewTask(taskType TaskType, payload string) *Task

NewTask creates a new task with the specified type and payload.

func NewTaskWithExternalID

func NewTaskWithExternalID(taskType, payload, externalID string) *Task

NewTaskWithExternalID creates a new task with a custom external ID.

func NewTaskWithPayload added in v0.6.0

func NewTaskWithPayload[T any](taskType TaskType, payload T) (*Task, error)

NewTaskWithPayload creates a new task with a typed payload marshaled as JSON.

func NewTaskWithPayloadAndExternalID added in v0.6.0

func NewTaskWithPayloadAndExternalID[T any](taskType TaskType, payload T, externalID string) (*Task, error)

NewTaskWithPayloadAndExternalID creates a new task with a typed payload and custom external ID.

func (*Task) AddError

func (t *Task) AddError(err error)

AddError appends an error message to the task's error log.

func (*Task) IsInTerminalState added in v0.5.0

func (t *Task) IsInTerminalState() bool

IsInTerminalState reports whether the task is in a terminal status.

type TaskProcessingOperations added in v0.2.0

type TaskProcessingOperations = string

TaskProcessingOperations represents the type of operation being performed on a task.

const (
	OperationFetch      TaskProcessingOperations = "fetch"      // Task fetching from database
	OperationProcessing TaskProcessingOperations = "processing" // Task execution by processor
	OperationCleanup    TaskProcessingOperations = "cleanup"    // Task cleanup operations
	OperationHealth     TaskProcessingOperations = "health"     // Health check operations
)

Task processing operation types for metrics and tracking.

type TaskStatus

type TaskStatus = string

TaskStatus represents the current status of a task.

type TaskType

type TaskType = string

TaskType represents the type of task to be executed.

type TypedTask added in v0.6.0

type TypedTask[T any] struct {
	*Task
	Payload T
}

TypedTask represents a task with a payload decoded into the expected Go type.

Jump to

Keyboard shortcuts

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