state

package
v0.10.4 Latest Latest
Warning

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

Go to latest
Published: May 18, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TaskStaleThresholdSeconds is the time in seconds after which an in-progress task is considered stale and aborted.
	TaskStaleThresholdSeconds = 3600
	// ObsoleteTaskCheckInterval is the interval between checks for obsolete tasks.
	ObsoleteTaskCheckInterval = 60 * time.Minute
)

Variables

This section is empty.

Functions

This section is empty.

Types

type InMemoryState

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

InMemoryState provides a thread-safe in-memory implementation of task storage. It uses a read-write mutex to protect concurrent access to the tasks slice.

func (*InMemoryState) AddTask

func (state *InMemoryState) AddTask(task models.Task) (*models.Task, error)

AddTask adds a new task to the in-memory repository. It takes a models.Task parameter, updates timestamps and status, and appends the task to the list of tracked tasks. The method returns the newly created task and a nil error because the in-memory implementation does not surface persistence failures.

func (*InMemoryState) Check

func (state *InMemoryState) Check() bool

Check is a placeholder method that implements the Check() bool interface. It always returns true and does not perform any actual state checking. This method exists to fulfill the TaskRepository interface requirement and has no functional value.

func (*InMemoryState) Connect

func (state *InMemoryState) Connect(serverConfig *config.ServerConfig) error

Connect is a placeholder method that does not establish any connection. It logs a debug message indicating that the InMemoryState does not connect to anything and skips the connection process. This method exists to fulfill the TaskRepository interface requirement and has no functional value.

func (*InMemoryState) GetTask

func (state *InMemoryState) GetTask(id string) (*models.Task, error)

GetTask retrieves a task from the in-memory state based on the provided task ID. It takes a string parameter for the task ID. The method iterates over the tasks in the in-memory state and returns the task if a matching ID is found. If no task with the given ID is found, it returns an error indicating that the task was not found.

func (*InMemoryState) GetTasks

func (state *InMemoryState) GetTasks(startTime float64, endTime float64, app string, status string, limit int, offset int) ([]models.Task, int64)

GetTasks retrieves tasks from the in-memory state based on the provided time range, app, and status filters. Empty filter values (app == "" or status == "") are treated as wildcards.

func (*InMemoryState) ProcessObsoleteTasks

func (state *InMemoryState) ProcessObsoleteTasks(retryTimes uint)

ProcessObsoleteTasks scans the in-memory tasks for obsolete tasks and updates their status. It starts a process to watch for obsolete tasks by invoking the `processInMemoryObsoleteTasks` function. The function uses the `retry` package to periodically retry the task processing with a fixed delay of 60 minutes.

func (*InMemoryState) SetTaskStatus

func (state *InMemoryState) SetTaskStatus(id string, status string, reason string) error

SetTaskStatus updates the status and status reason of a task in the in-memory state based on the provided task ID. It takes a string parameter for the task ID, status, and reason. The method iterates over the tasks in the in-memory state and updates the task with the matching ID. Returns an error if the task ID is not found.

type PostgresState

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

func (*PostgresState) AddTask

func (state *PostgresState) AddTask(task models.Task) (*models.Task, error)

AddTask inserts a new task into the PostgreSQL database with the provided details. It takes a models.Task parameter and returns an error if the insertion fails. The method executes an INSERT query to add a new record with the task details, including the current UTC time.

func (*PostgresState) Check

func (state *PostgresState) Check() bool

Check verifies the connection to the PostgreSQL database by executing a simple test query. It returns true if the database connection is successful and the test query is executed without errors. It returns false if there is an error in the database connection or the test query execution.

func (*PostgresState) Connect

func (state *PostgresState) Connect(serverConfig *config.ServerConfig) error

Connect establishes a connection to the PostgreSQL database using the provided server configuration.

func (*PostgresState) GetDB

func (state *PostgresState) GetDB() *gorm.DB

GetDB returns the underlying GORM database instance. This allows sharing the database connection pool with other components.

func (*PostgresState) GetTask

func (state *PostgresState) GetTask(id string) (*models.Task, error)

GetTask retrieves a task from the PostgreSQL database based on the provided task ID. It returns the task as a pointer to models.Task and an error if the task is not found or an error occurs during retrieval. The method executes a SELECT query with the given task ID and scans the result into the task struct. It handles converting the created and updated timestamps to float64 values and unmarshalling the images from the database.

func (*PostgresState) GetTasks

func (state *PostgresState) GetTasks(startTime float64, endTime float64, app string, status string, limit int, offset int) ([]models.Task, int64)

GetTasks retrieves a list of tasks from the PostgreSQL database based on the provided time range, app, and status filters. Empty filter values (app == "" or status == "") are treated as wildcards.

func (*PostgresState) ProcessObsoleteTasks

func (state *PostgresState) ProcessObsoleteTasks(retryTimes uint)

ProcessObsoleteTasks monitors and handles obsolete tasks in the PostgreSQL state. It initiates a process to remove tasks with a status of 'app not found' and mark tasks older than 1 hour as 'aborted'. The function utilizes retry logic to handle potential errors and retry the process if necessary. The retry interval is set to 60 minutes, and the retry attempts are set to 0 (no limit).

func (*PostgresState) SetTaskStatus

func (state *PostgresState) SetTaskStatus(id string, status string, reason string) error

SetTaskStatus updates the status, status_reason, and updated fields of a task in the PostgreSQL database. It takes the task ID, new status, and status reason as input parameters. The updated field is set to the current UTC time. Returns an error if the task ID is not found.

type TaskRepository

type TaskRepository interface {
	Connect(serverConfig *config.ServerConfig) error
	AddTask(task models.Task) (*models.Task, error)
	GetTasks(startTime float64, endTime float64, app string, status string, limit int, offset int) ([]models.Task, int64)
	GetTask(id string) (*models.Task, error)
	SetTaskStatus(id string, status string, reason string) error
	Check() bool
	ProcessObsoleteTasks(retryTimes uint)
}

TaskRepository defines the contract for task persistence. Implementations are responsible for connecting to the underlying storage and offering CRUD-like operations for deployment tasks.

func NewState

func NewState(serverConfig *config.ServerConfig) (TaskRepository, error)

NewState creates a new task repository based on the provided server configuration. It initializes the appropriate repository according to the StateType field and ensures that the returned implementation is already connected to the storage backend.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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