blob: 0c00580438c0b47ffd1fc9b32bf9da7c64e0a196 [file] [log] [blame]
// Copyright 2020 The LUCI Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package prjmanager
import (
// ProjectKind is the Datastore entity kind for Project.
const ProjectKind = "Project"
// Project is an entity per LUCI Project in Datastore.
type Project struct {
// $kind must match ProjectKind.
_kind string `gae:"$kind,Project"`
_extra datastore.PropertyMap `gae:"-,extra"`
// ID is LUCI project name.
ID string `gae:"$id"`
// EVersion is entity version. Every update should increment it by 1.
EVersion int `gae:",noindex"`
// UpdateTime is exact time of when this entity was last updated.
// It's not indexed to avoid hot areas in the index.
UpdateTime time.Time `gae:",noindex"`
// State serializes internal Project Manager state.
// The `LuciProject` field isn't set as it duplicates Project.ID.
State *prjpb.PState
// ProjectStateOffload stores rarely-changed project state duplicated from the
// main Project entity for use in transactions creating Runs.
// Although this is already stored in the main Project entity, doing so would
// result in retries of Run creation transactions, since Project entity is
// frequently modified in busy projects.
// On the other hand, ProjectStateOffload is highly likely to remain unchanged
// by the time Run creation transaction commits, thus avoiding needless
// retries.
type ProjectStateOffload struct {
_kind string `gae:"$kind,ProjectRarelyChanged"`
// ID is alaways the same, set/read only by the datastore ORM.
ID string `gae:"$id,const"`
Project *datastore.Key `gae:"$parent"`
// Status of project manager {STARTED, STOPPING, STOPPED (disabled)}.
Status prjpb.Status `gae:",noindex"`
// ConfigHash is the latest processed Project Config hash.
ConfigHash string `gae:",noindex"`
// IncompleteRuns are IDs of Runs which aren't yet completed.
func (p *Project) IncompleteRuns() (ids common.RunIDs) {
return p.State.IncompleteRuns()
// Status returns Project Manager status.
func (p *Project) Status() prjpb.Status {
return p.State.GetStatus()
// ConfigHash returns Project's Config hash.
func (p *Project) ConfigHash() string {
return p.State.GetConfigHash()
// Load loads LUCI project state from Datastore.
// If project doesn't exist in Datastore, returns nil, nil, nil.
func Load(ctx context.Context, luciProject string) (*Project, error) {
p := &Project{ID: luciProject}
switch err := datastore.Get(ctx, p); {
case err == datastore.ErrNoSuchEntity:
return nil, nil
case err != nil:
return nil, errors.Annotate(err, "failed to load Project state").Tag(transient.Tag).Err()
return p, nil