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:
- The flavor's platform must match the job's platform.
- The flavor's labels must be a superset of (or equal to) the job's labels.
- If multiple flavors match, choose the one with the highest priority.
- If there is still a tie, pick one at random.
Index ¶
- Constants
- Variables
- func Migrate(ctx context.Context, dbUri string) error
- type Database
- func (d *Database) AddFlavor(ctx context.Context, flavor *Flavor) error
- func (d *Database) AddJob(ctx context.Context, job *Job) error
- func (d *Database) CreateAuthToken(ctx context.Context, name string) ([32]byte, error)
- func (d *Database) DeleteAuthToken(ctx context.Context, name string) error
- func (d *Database) DeleteFlavor(ctx context.Context, platform, name string) error
- func (d *Database) DisableFlavor(ctx context.Context, platform, name string) error
- func (d *Database) EnableFlavor(ctx context.Context, platform, name string) error
- func (d *Database) GetPressures(ctx context.Context, platform string, flavors ...string) (map[string]int, error)
- func (d *Database) ListFlavors(ctx context.Context, platform string) ([]Flavor, error)
- func (d *Database) ListJobs(ctx context.Context, platform string, option ...ListJobOptions) ([]Job, error)
- func (d *Database) UpdateJobCompleted(ctx context.Context, platform, id string, completedAt time.Time, ...) error
- func (d *Database) UpdateJobStarted(ctx context.Context, platform, id string, startedAt time.Time, ...) error
- func (d *Database) VerifyAuthToken(ctx context.Context, token [32]byte) (string, error)
- type Flavor
- type Job
- type ListJobOptions
Constants ¶
const (
DefaultLimit = 100
)
Variables ¶
var ( ErrNotExist = errors.New("does not exist") ErrExist = errors.New("already exists") )
Functions ¶
Types ¶
type Database ¶
type Database struct {
// contains filtered or unexported fields
}
func (*Database) AddFlavor ¶
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 ¶
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 ¶
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 ¶
DeleteAuthToken deletes an authentication token. If the token doesn't exist, DeleteAuthToken does nothing.
func (*Database) DeleteFlavor ¶
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 ¶
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 ¶
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 ¶
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.
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"`
}