go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/scheduler/appengine/presentation/state.go (about)

     1  // Copyright 2017 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package presentation
    16  
    17  import (
    18  	"context"
    19  
    20  	"go.chromium.org/luci/common/logging"
    21  
    22  	"go.chromium.org/luci/scheduler/appengine/catalog"
    23  	"go.chromium.org/luci/scheduler/appengine/engine"
    24  	"go.chromium.org/luci/scheduler/appengine/schedule"
    25  	"go.chromium.org/luci/scheduler/appengine/task"
    26  )
    27  
    28  // PublicStateKind defines state of the job which is exposed in UI and API
    29  // instead of internal states which are kept as an implementation detail.
    30  type PublicStateKind string
    31  
    32  // When a PublicStateKind is added/removed/updated, update scheduler api proto
    33  // doc for `JobState`.
    34  const (
    35  	PublicStateDisabled  PublicStateKind = "DISABLED"
    36  	PublicStatePaused    PublicStateKind = "PAUSED"
    37  	PublicStateRunning   PublicStateKind = "RUNNING"
    38  	PublicStateScheduled PublicStateKind = "SCHEDULED"
    39  	PublicStateWaiting   PublicStateKind = "WAITING"
    40  )
    41  
    42  // GetPublicStateKind returns user-friendly state for a job.
    43  func GetPublicStateKind(j *engine.Job, traits task.Traits) PublicStateKind {
    44  	// TODO(vadimsh): Expose more states.
    45  	cronTick := j.CronTickTime()
    46  	switch {
    47  	case len(j.ActiveInvocations) != 0:
    48  		return PublicStateRunning
    49  	case !j.Enabled:
    50  		return PublicStateDisabled
    51  	case j.Paused:
    52  		return PublicStatePaused
    53  	case !cronTick.IsZero() && cronTick != schedule.DistantFuture:
    54  		return PublicStateScheduled
    55  	default:
    56  		return PublicStateWaiting
    57  	}
    58  }
    59  
    60  // GetJobTraits asks the corresponding task manager for a traits struct.
    61  func GetJobTraits(ctx context.Context, cat catalog.Catalog, j *engine.Job) (task.Traits, error) {
    62  	taskDef, err := cat.UnmarshalTask(ctx, j.Task, j.RealmID)
    63  	if err != nil {
    64  		logging.WithError(err).Warningf(ctx, "Failed to unmarshal task proto for %s", j.JobID)
    65  		return task.Traits{}, err
    66  	}
    67  	if manager := cat.GetTaskManager(taskDef); manager != nil {
    68  		return manager.Traits(), nil
    69  	}
    70  	return task.Traits{}, nil
    71  }