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

Side by Side Diff: scheduler/appengine/presentation/state.go

Issue 2945843002: scheduler WIP: add GetAllJobs api. (Closed)
Patch Set: meh 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 unified diff | Download patch
« no previous file with comments | « scheduler/appengine/presentation/doc.go ('k') | scheduler/appengine/presentation/state_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file.
4
5 package presentation
6
7 import (
8 "fmt"
9
10 "golang.org/x/net/context"
11
12 "github.com/luci/luci-go/common/logging"
13 "github.com/luci/luci-go/scheduler/appengine/catalog"
14 "github.com/luci/luci-go/scheduler/appengine/engine"
15 "github.com/luci/luci-go/scheduler/appengine/task"
16 )
17
18 // PublicStateKind defines state of the job which is exposed in UI and API
19 // instead of internal engine.StateKind which is kept as an implementation
20 // detail.
21 type PublicStateKind string
22
23 const (
24 PublicStateDisabled PublicStateKind = "DISABLED"
25 PublicStateOverrun PublicStateKind = "OVERRUN"
26 PublicStatePaused PublicStateKind = "PAUSED"
27 PublicStateRetrying PublicStateKind = "RETRYING"
28 PublicStateRunning PublicStateKind = "RUNNING"
29 PublicStateScheduled PublicStateKind = "SCHEDULED"
30 PublicStateStarting PublicStateKind = "STARTING"
31 PublicStateSuspended PublicStateKind = "SUSPENDED"
32 PublicStateWaiting PublicStateKind = "WAITING"
33 )
34
35 // jobStateInternalToPublic translates some internal states to public ones.
36 // However, this map is not sufficient, so see and use GetPublicStateKind
37 // function to handle the translation.
38 var jobStateInternalToPublic = map[engine.StateKind]PublicStateKind{
39 engine.JobStateDisabled: PublicStateDisabled,
40 engine.JobStateScheduled: PublicStateScheduled,
41 engine.JobStateSuspended: PublicStateSuspended,
42 engine.JobStateRunning: PublicStateRunning,
43 engine.JobStateOverrun: PublicStateOverrun,
44 }
45
46 // GetPublicStateKind returns user-friendly state and labelClass.
47 func GetPublicStateKind(j *engine.Job, traits task.Traits) PublicStateKind {
48 switch {
49 case j.State.IsRetrying():
50 // Retries happen when invocation fails to launch (move from "ST ARTING" to
51 // "RUNNING" state). Such invocation is retried (as new invocati on). When
52 // a retry is enqueued, we display the job state as "RETRYING" ( even though
53 // technically it is still "QUEUED").
54 return PublicStateRetrying
55 case !traits.Multistage && j.State.InvocationID != 0:
56 // The job has an active invocation and the engine has called La unchTask for
57 // it already. Jobs with Multistage == false trait do all their work in
58 // LaunchTask, so we display them as "RUNNING" (instead of "STAR TING").
59 return PublicStateRunning
60 case j.State.State == engine.JobStateQueued:
61 // An invocation has been added to the task queue, and the engin e hasn't
62 // attempted to launch it yet.
63 return PublicStateStarting
64 case j.State.State == engine.JobStateSlowQueue:
65 // Job invocation is still in the task queue, but new invocation should be
66 // starting now (so the queue is lagging for some reason).
67 return PublicStateStarting
68 case j.Paused && j.State.State == engine.JobStateSuspended:
69 // Paused jobs don't have a schedule, so they are always in "SUS PENDED"
70 // state. Make it clearer that they are just paused. This applie s to both
71 // triggered and periodic jobs.
72 return PublicStatePaused
73 case j.State.State == engine.JobStateSuspended && j.Flavor == catalog.Jo bFlavorTriggered:
74 // Triggered jobs don't run on a schedule. They are in "SUSPENDE D" state
75 // between triggering events, rename it to "WAITING" for clarity .
76 return PublicStateWaiting
77 default:
78 if r, ok := jobStateInternalToPublic[j.State.State]; !ok {
79 panic(fmt.Errorf("unknown state: %q", j.State.State))
80 } else {
81 return r
82 }
83 }
84 }
85
86 func GetJobTraits(ctx context.Context, cat catalog.Catalog, j *engine.Job) (task .Traits, error) {
87 // trais = task.Traits{}
88 taskDef, err := cat.UnmarshalTask(j.Task)
89 if err != nil {
90 logging.WithError(err).Warningf(ctx, "Failed to unmarshal task p roto for %s", j.JobID)
91 return task.Traits{}, err
92 }
93 if manager := cat.GetTaskManager(taskDef); manager != nil {
94 return manager.Traits(), nil
95 }
96 return task.Traits{}, nil
97 }
OLDNEW
« no previous file with comments | « scheduler/appengine/presentation/doc.go ('k') | scheduler/appengine/presentation/state_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698