| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 //go:generate stringer -type=State | 5 //go:generate stringer -type=State |
| 6 | 6 |
| 7 package execution | 7 package execution |
| 8 | 8 |
| 9 import ( | 9 import ( |
| 10 "fmt" | 10 "fmt" |
| 11 ) | 11 ) |
| 12 | 12 |
| 13 // State is the state that a single Execution can be in. | 13 // State is the state that a single Execution can be in. |
| 14 type State int8 | 14 type State int8 |
| 15 | 15 |
| 16 // State values | 16 // State values |
| 17 const ( | 17 const ( |
| 18 » UnknownState State = iota | 18 » Unknown State = iota |
| 19 | 19 |
| 20 // The execution has been accepted by the distributor, but is not runnin
g yet |
| 20 Scheduled | 21 Scheduled |
| 21 Running | |
| 22 | 22 |
| 23 // The execution is running |
| 24 Started |
| 25 |
| 26 // The execution was unable to be accepted by the distributor |
| 23 Rejected | 27 Rejected |
| 28 |
| 29 // The execution was accepted by the distributor, but couldn't run in ti
me. |
| 30 TimedOut |
| 31 |
| 32 // The execution ran and completed |
| 24 Finished | 33 Finished |
| 25 » Crashed | 34 |
| 35 » // The execution ran, but the distributor claims it did not complete |
| 36 » Failed |
| 37 |
| 38 » // The distributor claims to not know anything about this execution |
| 39 » Missing |
| 40 |
| 41 » // Some entity (DM, Human, Distributor) requested that this execution no
t run. |
| 42 » Cancelled |
| 26 ) | 43 ) |
| 27 | 44 |
| 28 // validStateEvolution defines all valid {From -> []To} state transitions. The | 45 // validStateEvolution defines all valid {From -> []To} state transitions. The |
| 29 // identity transition (X -> X) is implied, as long as X has an entry in this | 46 // identity transition (X -> X) is implied, as long as X has an entry in this |
| 30 // mapping. | 47 // mapping. |
| 31 var validStateEvolution = map[State][]State{ | 48 var validStateEvolution = map[State][]State{ |
| 32 » Scheduled: {Running, Rejected, Crashed, Finished}, | 49 » Scheduled: {Started, Finished, Missing, Cancelled, TimedOut}, |
| 33 » Running: {Crashed, Finished, Rejected}, | 50 » Started: {Finished, Failed, Missing, Cancelled}, |
| 34 | 51 |
| 35 » Crashed: {}, | 52 » Cancelled: {}, |
| 36 » Finished: {}, | 53 » Finished: {}, |
| 37 » Rejected: {}, | 54 » Failed: {}, |
| 55 » Missing: {}, |
| 56 » Rejected: {}, |
| 57 » TimedOut: {}, |
| 58 } |
| 59 |
| 60 // FromString returns the State for it's equivalent string representation. |
| 61 // |
| 62 // All unknown state vals are converted to Unknown. |
| 63 func FromString(val string) State { |
| 64 » switch val { |
| 65 » case "Scheduled": |
| 66 » » return Scheduled |
| 67 » case "Started": |
| 68 » » return Started |
| 69 » case "Rejected": |
| 70 » » return Rejected |
| 71 » case "TimedOut": |
| 72 » » return TimedOut |
| 73 » case "Finished": |
| 74 » » return Finished |
| 75 » case "Failed": |
| 76 » » return Failed |
| 77 » case "Missing": |
| 78 » » return Missing |
| 79 » case "Cancelled": |
| 80 » » return Cancelled |
| 81 » } |
| 82 » return Unknown |
| 83 } |
| 84 |
| 85 // IsTerminal returns True if State has no more valid evolutions. |
| 86 func (s State) IsTerminal() bool { |
| 87 » return len(validStateEvolution[s]) == 0 |
| 38 } | 88 } |
| 39 | 89 |
| 40 // Evolve attempts to evolve the state of this Attempt. If the state | 90 // Evolve attempts to evolve the state of this Attempt. If the state |
| 41 // evolution is not allowed (e.g. invalid state transition), this returns an | 91 // evolution is not allowed (e.g. invalid state transition), this returns an |
| 42 // error. | 92 // error. |
| 43 func (s *State) Evolve(newState State) error { | 93 func (s *State) Evolve(newState State) error { |
| 44 nextStates := validStateEvolution[*s] | 94 nextStates := validStateEvolution[*s] |
| 45 if nextStates == nil { | 95 if nextStates == nil { |
| 46 return fmt.Errorf("invalid state transition: no transitions defi
ned for %s", *s) | 96 return fmt.Errorf("invalid state transition: no transitions defi
ned for %s", *s) |
| 47 } | 97 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 60 return fmt.Errorf("invalid state transition %v -> %v", *s, newState) | 110 return fmt.Errorf("invalid state transition %v -> %v", *s, newState) |
| 61 } | 111 } |
| 62 | 112 |
| 63 // MustEvolve is a panic'ing version of Evolve. | 113 // MustEvolve is a panic'ing version of Evolve. |
| 64 func (s *State) MustEvolve(newState State) { | 114 func (s *State) MustEvolve(newState State) { |
| 65 err := s.Evolve(newState) | 115 err := s.Evolve(newState) |
| 66 if err != nil { | 116 if err != nil { |
| 67 panic(err) | 117 panic(err) |
| 68 } | 118 } |
| 69 } | 119 } |
| OLD | NEW |