Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(800)

Unified Diff: scheduler/appengine/presentation/state.go

Issue 2945843002: scheduler WIP: add GetAllJobs api. (Closed)
Patch Set: update test Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: scheduler/appengine/presentation/state.go
diff --git a/scheduler/appengine/presentation/state.go b/scheduler/appengine/presentation/state.go
new file mode 100644
index 0000000000000000000000000000000000000000..5a4824007354f0e7360610fc42bd5d5c7e53877b
--- /dev/null
+++ b/scheduler/appengine/presentation/state.go
@@ -0,0 +1,76 @@
+// Copyright 2017 The LUCI Authors. All rights reserved.
+// Use of this source code is governed under the Apache License, Version 2.0
+// that can be found in the LICENSE file.
+
+package presentation
+
+import (
+ "fmt"
+
+ "github.com/luci/luci-go/scheduler/appengine/catalog"
+ "github.com/luci/luci-go/scheduler/appengine/engine"
+ "github.com/luci/luci-go/scheduler/appengine/task"
+)
+
+// PublicStateKind defines state of the job which is exposed in UI and API
+// instead of internal engine.StateKind which is kept as an implementation
+// detail.
+type PublicStateKind string
+
+const (
+ PublicStateDisabled PublicStateKind = "DISABLED"
+ PublicStateOverrun PublicStateKind = "OVERRUN"
+ PublicStatePaused PublicStateKind = "PAUSED"
+ PublicStateRetrying PublicStateKind = "RETRYING"
+ PublicStateRunning PublicStateKind = "RUNNING"
+ PublicStateScheduled PublicStateKind = "SCHEDULED"
+ PublicStateStarting PublicStateKind = "STARTING"
+ PublicStateSuspended PublicStateKind = "SUSPENDED"
+ PublicStateWaiting PublicStateKind = "WAITING"
+)
+
+// GetPublicStateKind returns user-friendly state and labelClass.
+func GetPublicStateKind(j *engine.Job, traits task.Traits) PublicStateKind {
+ switch {
+ case j.State.IsRetrying():
+ // Retries happen when invocation fails to launch (move from "STARTING" to
+ // "RUNNING" state). Such invocation is retried (as new invocation). When
+ // a retry is enqueued, we display the job state as "RETRYING" (even though
+ // technically it is still "QUEUED").
+ return PublicStateRetrying
+ case !traits.Multistage && j.State.InvocationID != 0:
+ // The job has an active invocation and the engine has called LaunchTask for
+ // it already. Jobs with Multistage == false trait do all their work in
+ // LaunchTask, so we display them as "RUNNING" (instead of "STARTING").
+ return PublicStateRunning
+ case j.State.State == engine.JobStateQueued:
+ // An invocation has been added to the task queue, and the engine hasn't
+ // attempted to launch it yet.
+ return PublicStateStarting
+ case j.State.State == engine.JobStateSlowQueue:
+ // Job invocation is still in the task queue, but new invocation should be
+ // starting now (so the queue is lagging for some reason).
+ return PublicStateStarting
+ case j.Paused && j.State.State == engine.JobStateSuspended:
+ // Paused jobs don't have a schedule, so they are always in "SUSPENDED"
+ // state. Make it clearer that they are just paused. This applies to both
+ // triggered and periodic jobs.
+ return PublicStatePaused
+ case j.State.State == engine.JobStateSuspended && j.Flavor == catalog.JobFlavorTriggered:
+ // Triggered jobs don't run on a schedule. They are in "SUSPENDED" state
+ // between triggering events, rename it to "WAITING" for clarity.
+ return PublicStateWaiting
+ default:
+ if r, ok := map[engine.StateKind]PublicStateKind{
Vadim Sh. 2017/06/20 17:48:54 please make this map package-level global var
tandrii(chromium) 2017/06/20 19:48:36 Done.
+ engine.JobStateDisabled: PublicStateDisabled,
+ engine.JobStateScheduled: PublicStateScheduled,
+ engine.JobStateSuspended: PublicStateSuspended,
+ engine.JobStateRunning: PublicStateRunning,
+ engine.JobStateOverrun: PublicStateOverrun,
+ }[j.State.State]; !ok {
+ panic(fmt.Errorf("unknown state: %q", j.State.State))
+ } else {
+ return r
+ }
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698