database

package
v0.0.0-...-97cac26 Latest Latest
Warning

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

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

Documentation

Overview

Package database provides persistent storage for auth tokens, jobs, and flavors, and computes "pressure" for each flavor.

Pressure is a numeric measure of demand for runner machines (flavors). It is defined as the number of incomplete jobs (completed_at IS NULL) assigned to a given flavor.

When a job is inserted, it is assigned a flavor using the following algorithm:

  1. The flavor's platform must match the job's platform.
  2. The flavor's labels must be a superset of (or equal to) the job's labels.
  3. If multiple flavors match, choose the one with the highest priority.
  4. If there is still a tie, pick one at random.

Index

Constants

View Source
const (
	DefaultLimit = 100
)

Variables

View Source
var (
	ErrNotExist = errors.New("does not exist")
	ErrExist    = errors.New("already exists")
)

Functions

func Migrate

func Migrate(ctx context.Context, dbUri string) error

Types

type Database

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

func New

func New(ctx context.Context, uri string) (*Database, error)

New creates a new Database instance

func (*Database) AddFlavor

func (d *Database) AddFlavor(ctx context.Context, flavor *Flavor) error

AddFlavor inserts a new flavor into the database. Returns ErrExist if a flavor with the same name already exists. After insertion, the flavor-assignment (pressure) algorithm runs for all jobs that do not yet have an assigned flavor.

func (*Database) AddJob

func (d *Database) AddJob(ctx context.Context, job *Job) error

AddJob inserts a job into the database. All fields from job are persisted except AssignedFlavor, which is computed by the flavor-assignment (pressure) algorithm. If a job with the same (platform, id) already exists, it returns ErrExist.

func (*Database) CreateAuthToken

func (d *Database) CreateAuthToken(ctx context.Context, name string) ([32]byte, error)

CreateAuthToken creates an authentication token for the given name. If a token with the same name already exists, it returns ErrExist. The returned token is a 256-bit random byte slice and can be verified with VerifyAuthToken.

func (*Database) DeleteAuthToken

func (d *Database) DeleteAuthToken(ctx context.Context, name string) error

DeleteAuthToken deletes an authentication token. If the token doesn't exist, DeleteAuthToken does nothing.

func (*Database) DeleteFlavor

func (d *Database) DeleteFlavor(ctx context.Context, platform, name string) error

DeleteFlavor deletes a flavor. Jobs currently assigned to this flavor are reset and re-assigned to a new flavor using the same algorithm. If the flavor doesn't exist, it will do nothing.

func (*Database) DisableFlavor

func (d *Database) DisableFlavor(ctx context.Context, platform, name string) error

DisableFlavor disables a flavor, excluding it from the flavor-assignment pressure algorithm. Jobs currently assigned to this flavor are reset and re-assigned to a new flavor using the same algorithm. If the flavor doesn't exist, it will return ErrNotExist.

func (*Database) EnableFlavor

func (d *Database) EnableFlavor(ctx context.Context, platform, name string) error

EnableFlavor enables a flavor, and reverses the DisableFlavor. If the flavor doesn't exist, it will return ErrNotExist.

func (*Database) GetPressures

func (d *Database) GetPressures(ctx context.Context, platform string, flavors ...string) (map[string]int, error)

GetPressures returns the pressure for the specified flavors. For details on how pressure is computed, see the package documentation on the pressure algorithm.

func (*Database) ListFlavors

func (d *Database) ListFlavors(ctx context.Context, platform string) ([]Flavor, error)

ListFlavors lists existing flavors.

func (*Database) ListJobs

func (d *Database) ListJobs(ctx context.Context, platform string, option ...ListJobOptions) ([]Job, error)

ListJobs returns stored jobs. Use ListJobOptions to filter results. If ListJobOptions.Limit is 0, DefaultLimit is applied.

func (*Database) UpdateJobCompleted

func (d *Database) UpdateJobCompleted(ctx context.Context, platform, id string, completedAt time.Time, raw map[string]interface{}) error

UpdateJobCompleted updates a job's completed_at field. If raw is provided, it is merged into the existing raw payload in the database. If the job doesn't exist, it will return ErrNotExist.

func (*Database) UpdateJobStarted

func (d *Database) UpdateJobStarted(ctx context.Context, platform, id string, startedAt time.Time, raw map[string]interface{}) error

UpdateJobStarted updates a job's started_at field. If raw is provided, it is merged into the existing raw payload in the database. If the job doesn't exist, it will return ErrNotExist.

func (*Database) VerifyAuthToken

func (d *Database) VerifyAuthToken(ctx context.Context, token [32]byte) (string, error)

VerifyAuthToken verifies an authentication token. It returns the associated token name if the token is valid. If the token is invalid or not found, it returns ErrNotExist.

type Flavor

type Flavor struct {
	Platform        string   `db:"platform"         json:"platform"`
	Name            string   `db:"name"             json:"name"`
	Labels          []string `db:"labels"           json:"labels"`
	Priority        int      `db:"priority"         json:"priority"`
	IsDisabled      bool     `db:"is_disabled"      json:"is_disabled"`
	MinimumPressure int      `db:"minimum_pressure" json:"minimum_pressure"`
}

type Job

type Job struct {
	Platform       string                 `db:"platform"        json:"platform"`
	ID             string                 `db:"id"              json:"id"`
	Labels         []string               `db:"labels"          json:"labels"`
	CreatedAt      time.Time              `db:"created_at"      json:"created_at"`
	StartedAt      *time.Time             `db:"started_at"      json:"started_at,omitempty"`
	CompletedAt    *time.Time             `db:"completed_at"    json:"completed_at,omitempty"`
	Raw            map[string]interface{} `db:"raw"             json:"raw"`
	AssignedFlavor *string                `db:"assigned_flavor" json:"assigned_flavor,omitempty"`
}

type ListJobOptions

type ListJobOptions struct {
	WithId           string
	OnlyNotCompleted bool
	CreatedAfter     time.Time
	Limit            int
}

Jump to

Keyboard shortcuts

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