store

package
v0.0.0-...-4622752 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Overview

Package store provides the storage layer for task and operation rule management. It defines the Store interface for persisting and retrieving task and operation rule data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PostgresStore

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

PostgresStore implements the Store interface using PostgreSQL.

func NewPostgres

func NewPostgres(pg *cdb.Session) *PostgresStore

NewPostgres creates a new PostgreSQL-backed task store.

func (*PostgresStore) AssociateRuleWithRack

func (s *PostgresStore) AssociateRuleWithRack(
	ctx context.Context,
	rackID uuid.UUID,
	ruleID uuid.UUID,
) error

AssociateRuleWithRack associates a rule with a rack. The operation type and operation code are extracted from the rule.

func (*PostgresStore) CountWaitingTasksForRack

func (s *PostgresStore) CountWaitingTasksForRack(
	ctx context.Context,
	rackID uuid.UUID,
) (int, error)

CountWaitingTasksForRack returns the count of waiting tasks for the rack.

func (*PostgresStore) CreateRule

func (s *PostgresStore) CreateRule(
	ctx context.Context,
	rule *operationrules.OperationRule,
) error

CreateRule creates a new operation rule

func (*PostgresStore) CreateTask

func (s *PostgresStore) CreateTask(
	ctx context.Context,
	task *taskdef.Task,
) error

CreateTask creates a new task record. Participates in a surrounding transaction if one was started by RunInTransaction.

func (*PostgresStore) DeleteRule

func (s *PostgresStore) DeleteRule(
	ctx context.Context,
	id uuid.UUID,
) error

DeleteRule deletes an operation rule by ID

func (*PostgresStore) DisassociateRuleFromRack

func (s *PostgresStore) DisassociateRuleFromRack(
	ctx context.Context,
	rackID uuid.UUID,
	opType taskcommon.TaskType,
	operationCode string,
) error

DisassociateRuleFromRack removes the rule association for a rack, operation type, and operation code

func (*PostgresStore) GetDefaultRule

func (s *PostgresStore) GetDefaultRule(
	ctx context.Context,
	opType taskcommon.TaskType,
	operationCode string,
) (*operationrules.OperationRule, error)

GetDefaultRule retrieves the default rule for an operation type and code

func (*PostgresStore) GetRackRuleAssociation

func (s *PostgresStore) GetRackRuleAssociation(
	ctx context.Context,
	rackID uuid.UUID,
	opType taskcommon.TaskType,
	operationCode string,
) (*uuid.UUID, error)

GetRackRuleAssociation retrieves the rule ID associated with a rack for an operation type and operation code

func (*PostgresStore) GetRule

GetRule retrieves an operation rule by ID

func (*PostgresStore) GetRuleByName

func (s *PostgresStore) GetRuleByName(
	ctx context.Context,
	name string,
) (*operationrules.OperationRule, error)

GetRuleByName retrieves an operation rule by its name

func (*PostgresStore) GetRuleByOperationAndRack

func (s *PostgresStore) GetRuleByOperationAndRack(
	ctx context.Context,
	opType taskcommon.TaskType,
	operationCode string,
	rackID *uuid.UUID,
) (*operationrules.OperationRule, error)

GetRuleByOperationAndRack retrieves the appropriate rule for an operation code and rack. Resolution order: rack association > default rule

func (*PostgresStore) GetTask

func (s *PostgresStore) GetTask(
	ctx context.Context,
	id uuid.UUID,
) (*taskdef.Task, error)

GetTask retrieves a single task by its ID.

func (*PostgresStore) GetTaskByIdempotencyKey

func (s *PostgresStore) GetTaskByIdempotencyKey(
	ctx context.Context,
	key string,
) (*taskdef.Task, error)

GetTaskByIdempotencyKey retrieves the existing task for a stable submission key. A missing key returns nil so callers can continue normal creation.

func (*PostgresStore) GetTasks

func (s *PostgresStore) GetTasks(
	ctx context.Context,
	ids []uuid.UUID,
) ([]*taskdef.Task, error)

GetTasks retrieves tasks by their IDs.

func (*PostgresStore) LatestLeakageShutdownTaskStatuses

func (s *PostgresStore) LatestLeakageShutdownTaskStatuses(
	ctx context.Context,
	componentIDs []uuid.UUID,
) (map[uuid.UUID]taskcommon.TaskStatus, error)

LatestLeakageShutdownTaskStatuses returns one newest leakage-triggered forced-shutdown Task status per requested component. The component predicate uses the existing task attributes GIN index; the lateral expansion identifies which requested component each matching Task targets.

func (*PostgresStore) ListActiveTasksForRack

func (s *PostgresStore) ListActiveTasksForRack(
	ctx context.Context,
	rackID uuid.UUID,
) ([]*taskdef.Task, error)

ListActiveTasksForRack returns pending and running tasks for the given rack.

func (*PostgresStore) ListFirmwareTasksForRack

func (s *PostgresStore) ListFirmwareTasksForRack(ctx context.Context, rackID uuid.UUID) ([]taskdef.FirmwareTaskDetails, error)

ListFirmwareTasksForRack reads complete safe firmware intent in the caller's transaction or store connection, retaining recorded selections without inventory.

func (*PostgresStore) ListNonTerminalTasksForRacks

func (s *PostgresStore) ListNonTerminalTasksForRacks(
	ctx context.Context,
	rackIDs []uuid.UUID,
) ([]*taskdef.Task, error)

ListNonTerminalTasksForRacks returns Waiting, Pending, and Running tasks for the requested racks.

func (*PostgresStore) ListPowerTasksForRack

func (s *PostgresStore) ListPowerTasksForRack(
	ctx context.Context,
	rackID uuid.UUID,
) ([]taskdef.PowerTaskDetails, error)

ListPowerTasksForRack reads complete, typed power intent without resolving targets against inventory. Missing inventory must not erase recorded UUIDs.

func (*PostgresStore) ListRackRuleAssociations

func (s *PostgresStore) ListRackRuleAssociations(
	ctx context.Context,
	rackID uuid.UUID,
) ([]*operationrules.RackRuleAssociation, error)

ListRackRuleAssociations retrieves all rule associations for a rack

func (*PostgresStore) ListRacksWithWaitingTasks

func (s *PostgresStore) ListRacksWithWaitingTasks(
	ctx context.Context,
) ([]uuid.UUID, error)

ListRacksWithWaitingTasks returns distinct rack IDs with waiting tasks.

func (*PostgresStore) ListRules

ListRules lists operation rules matching the given criteria with pagination

func (*PostgresStore) ListTaskContextForRack

func (s *PostgresStore) ListTaskContextForRack(ctx context.Context, rackID uuid.UUID) ([]taskdef.TaskContextMetadata, error)

ListTaskContextForRack reads nearby work without decoding operation payloads.

func (*PostgresStore) ListTasks

func (s *PostgresStore) ListTasks(
	ctx context.Context,
	options *taskcommon.TaskListOptions,
	pagination *dbquery.Pagination,
) ([]*taskdef.Task, int32, error)

ListTasks lists tasks matching the given criteria.

func (*PostgresStore) ListWaitingTasksForRack

func (s *PostgresStore) ListWaitingTasksForRack(
	ctx context.Context,
	rackID uuid.UUID,
) ([]*taskdef.Task, error)

ListWaitingTasksForRack returns waiting tasks for the rack, oldest first.

func (*PostgresStore) LockIdempotencyKey

func (s *PostgresStore) LockIdempotencyKey(ctx context.Context, key string) error

LockIdempotencyKey takes a transaction-scoped advisory lock for stable submission lookup and resume. The lock is released automatically when the transaction ends.

func (*PostgresStore) LockRack

func (s *PostgresStore) LockRack(ctx context.Context, rackID uuid.UUID) error

LockRack takes a transaction-scoped advisory lock for rack-level task admission. The lock is released automatically when the transaction ends.

func (*PostgresStore) RunInTransaction

func (s *PostgresStore) RunInTransaction(
	ctx context.Context,
	fn func(ctx context.Context) error,
) error

RunInTransaction executes fn within a database transaction, propagating the transaction through the context so nested store calls participate.

func (*PostgresStore) SetRuleAsDefault

func (s *PostgresStore) SetRuleAsDefault(
	ctx context.Context,
	id uuid.UUID,
) error

SetRuleAsDefault sets a rule as the default for its operation. Automatically unsets any existing default for the same (operation_type, operation_code).

func (*PostgresStore) UpdateRule

func (s *PostgresStore) UpdateRule(
	ctx context.Context,
	id uuid.UUID,
	updates map[string]interface{},
) error

UpdateRule updates specific fields of an operation rule

func (*PostgresStore) UpdateScheduledTask

func (s *PostgresStore) UpdateScheduledTask(
	ctx context.Context,
	task *taskdef.Task,
) error

UpdateScheduledTask updates task scheduling information.

func (*PostgresStore) UpdateTaskReport

func (s *PostgresStore) UpdateTaskReport(
	ctx context.Context,
	arg *taskdef.TaskReportUpdate,
) error

UpdateTaskReport replaces the stored report with the snapshot in arg. Empty snapshots are dropped rather than written so a malformed caller cannot clear the stored report by accident.

func (*PostgresStore) UpdateTaskStatus

func (s *PostgresStore) UpdateTaskStatus(
	ctx context.Context,
	arg *taskdef.TaskStatusUpdate,
) error

UpdateTaskStatus persists status and message, plus optional report and queue deadline changes. Finished statuses clear the queue deadline; otherwise nil optional values leave their stored columns untouched. No read-modify-write is performed, so concurrent transitions cannot lose updates.

type Store

type Store interface {

	// RunInTransaction executes fn within a database transaction.
	// The transaction is propagated through the context so that nested store
	// calls automatically participate without extra plumbing.
	RunInTransaction(ctx context.Context, fn func(ctx context.Context) error) error

	// CreateTask creates a new task record.
	CreateTask(ctx context.Context, task *taskdef.Task) error

	// LockRack takes a transaction-scoped lock for rack-level task admission.
	// Callers must invoke it inside RunInTransaction before conflict checks.
	LockRack(ctx context.Context, rackID uuid.UUID) error

	// LockIdempotencyKey takes a transaction-scoped lock for stable submission
	// lookup and resume. Callers must invoke it inside RunInTransaction.
	LockIdempotencyKey(ctx context.Context, key string) error

	// GetTaskByIdempotencyKey retrieves the existing task for a stable
	// submission key. It returns nil when no task exists for the key.
	GetTaskByIdempotencyKey(
		ctx context.Context,
		key string,
	) (*taskdef.Task, error)

	// GetTask retrieves a single task by its ID.
	// Returns an error if the task is not found.
	GetTask(ctx context.Context, id uuid.UUID) (*taskdef.Task, error)

	// GetTasks retrieves tasks by their IDs.
	GetTasks(ctx context.Context, ids []uuid.UUID) ([]*taskdef.Task, error)

	// ListTasks lists tasks matching the given criteria.
	ListTasks(ctx context.Context, options *taskcommon.TaskListOptions, pagination *dbquery.Pagination) ([]*taskdef.Task, int32, error)

	// ListPowerTasksForRack returns all Waiting, Pending, and Running power
	// tasks for the exact rack ID, ordered by created_at then id, ascending.
	// Targets retain recorded UUID/type membership without inventory lookup;
	// UUIDs within each type are sorted and unique, with empty groups retained.
	// No matches returns a non-nil empty slice. The read joins a transaction
	// established by RunInTransaction, or uses the store connection otherwise.
	//
	// Any error returns no tasks: *taskcommon.PowerTaskLimitError when more
	// than MaxPowerTasksPerRack records match; ctx.Err() when a query fails
	// after caller cancellation/expiry; or a sanitized gRPC Internal error
	// for other read/conversion failures. The limit error is an internal type
	// that RPC callers must translate, not a gRPC status.
	ListPowerTasksForRack(ctx context.Context, rackID uuid.UUID) ([]taskdef.PowerTaskDetails, error)

	// ListFirmwareTasksForRack returns Waiting, Pending, and Running firmware
	// tasks for the exact rack, ordered by creation time then UUID, ascending.
	// Recorded UUID/type selections survive without inventory lookup; UUIDs
	// within each type are sorted and unique, with empty groups retained.
	// Target metadata excludes raw manifests, artifact URLs and authentication;
	// reports and free-form descriptions/messages are absent from the projection.
	// No matches returns a non-nil empty slice. The read joins a transaction
	// established by RunInTransaction, or uses the store connection otherwise.
	// Errors return no tasks: *taskcommon.FirmwareTaskLimitError above
	// MaxFirmwareTasksPerRack rows, ctx.Err() after query cancellation/expiry,
	// or sanitized Internal for other read/conversion failures. This internal
	// reader has no RPC consumer; transport callers must translate the limit error.
	ListFirmwareTasksForRack(ctx context.Context, rackID uuid.UUID) ([]taskdef.FirmwareTaskDetails, error)

	// ListTaskContextForRack returns nonterminal metadata ordered by creation
	// time then UUID. It joins the current transaction and excludes operation
	// payloads/reports. Overflow returns TaskContextLimitError and no rows;
	// cancellation is preserved and other failures return sanitized Internal.
	ListTaskContextForRack(ctx context.Context, rackID uuid.UUID) ([]taskdef.TaskContextMetadata, error)

	// ListNonTerminalTasksForRacks returns Waiting, Pending, and Running tasks
	// for the requested racks.
	ListNonTerminalTasksForRacks(ctx context.Context, rackIDs []uuid.UUID) ([]*taskdef.Task, error)

	// LatestLeakageShutdownTaskStatuses returns the newest leakage-triggered
	// forced-shutdown Task status for each requested component that has one.
	LatestLeakageShutdownTaskStatuses(
		ctx context.Context,
		componentIDs []uuid.UUID,
	) (map[uuid.UUID]taskcommon.TaskStatus, error)

	// UpdateScheduledTask updates task scheduling information (execution ID, executor type).
	UpdateScheduledTask(ctx context.Context, task *taskdef.Task) error

	// UpdateTaskStatus updates status and message, plus an optional queue deadline.
	UpdateTaskStatus(ctx context.Context, arg *taskdef.TaskStatusUpdate) error

	// UpdateTaskReport merges a report snapshot without a status change.
	UpdateTaskReport(ctx context.Context, arg *taskdef.TaskReportUpdate) error

	// ListActiveTasksForRack returns non-finished, non-waiting tasks for a rack
	// (i.e. tasks with status pending or running).
	ListActiveTasksForRack(ctx context.Context, rackID uuid.UUID) ([]*taskdef.Task, error)

	// ListWaitingTasksForRack returns waiting tasks for a rack, ordered oldest-first.
	ListWaitingTasksForRack(ctx context.Context, rackID uuid.UUID) ([]*taskdef.Task, error)

	// CountWaitingTasksForRack returns the number of waiting tasks for a rack.
	CountWaitingTasksForRack(ctx context.Context, rackID uuid.UUID) (int, error)

	// ListRacksWithWaitingTasks returns distinct rack IDs that have at least
	// one task in the waiting state.
	ListRacksWithWaitingTasks(ctx context.Context) ([]uuid.UUID, error)

	// CreateRule creates a new operation rule.
	CreateRule(ctx context.Context, rule *operationrules.OperationRule) error

	// UpdateRule updates specific fields of an operation rule.
	UpdateRule(ctx context.Context, id uuid.UUID, updates map[string]interface{}) error

	// DeleteRule deletes an operation rule by ID.
	DeleteRule(ctx context.Context, id uuid.UUID) error

	// SetRuleAsDefault sets a rule as the default for its operation.
	// Automatically unsets any existing default for the same (operation_type, operation).
	SetRuleAsDefault(ctx context.Context, id uuid.UUID) error

	// GetRule retrieves an operation rule by ID.
	GetRule(ctx context.Context, id uuid.UUID) (*operationrules.OperationRule, error)

	// GetRuleByName retrieves an operation rule by its name.
	GetRuleByName(ctx context.Context, name string) (*operationrules.OperationRule, error)

	// GetDefaultRule retrieves the default rule for an operation type and operation code.
	// Returns the rule with is_default=true for the given operation type and operation code.
	GetDefaultRule(ctx context.Context, opType taskcommon.TaskType, operationCode string) (*operationrules.OperationRule, error)

	// GetRuleByOperationAndRack retrieves the appropriate rule for an operation type, operation code, and rack.
	// Resolution order: rack association > default rule
	// If rackID is nil, returns the default rule.
	GetRuleByOperationAndRack(ctx context.Context, opType taskcommon.TaskType, operationCode string, rackID *uuid.UUID) (*operationrules.OperationRule, error)

	// ListRules lists operation rules matching the given criteria with pagination.
	ListRules(ctx context.Context, options *taskcommon.OperationRuleListOptions, pagination *dbquery.Pagination) ([]*operationrules.OperationRule, int32, error)

	// AssociateRuleWithRack associates a rule with a rack.
	// The operation type and operation code are extracted from the rule.
	// If an association already exists, it will be updated.
	AssociateRuleWithRack(ctx context.Context, rackID uuid.UUID, ruleID uuid.UUID) error

	// DisassociateRuleFromRack removes the rule association for a rack, operation type, and operation code.
	DisassociateRuleFromRack(ctx context.Context, rackID uuid.UUID, opType taskcommon.TaskType, operationCode string) error

	// GetRackRuleAssociation retrieves the rule ID associated with a rack for an operation type and operation code.
	// Returns nil if no association exists.
	GetRackRuleAssociation(ctx context.Context, rackID uuid.UUID, opType taskcommon.TaskType, operationCode string) (*uuid.UUID, error)

	// ListRackRuleAssociations retrieves all rule associations for a rack.
	// Returns a list of associations with full details (operation type, operation code, rule ID).
	ListRackRuleAssociations(ctx context.Context, rackID uuid.UUID) ([]*operationrules.RackRuleAssociation, error)
}

Store defines the interface for task and operation rule data persistence. It provides operations for creating, retrieving, and updating both tasks and operation rules.

Jump to

Keyboard shortcuts

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