Validate checks that the lifecycle and assignment metadata are internally consistent.
()
| 94 | // Validate checks that the lifecycle and assignment metadata are internally |
| 95 | // consistent. |
| 96 | func (s SharedWorkerState) Validate() error { |
| 97 | switch lifecycle := s.NormalizedLifecycle(); lifecycle { |
| 98 | case WorkerLifecycleIdle: |
| 99 | if s.Assignment != nil { |
| 100 | return fmt.Errorf("lifecycle %q cannot have an assignment", lifecycle) |
| 101 | } |
| 102 | return nil |
| 103 | case WorkerLifecycleReserved, WorkerLifecycleActivating, WorkerLifecycleHot, WorkerLifecycleHotIdle: |
| 104 | if err := validateWorkerAssignment(s.Assignment); err != nil { |
| 105 | return fmt.Errorf("lifecycle %q requires a valid assignment: %w", lifecycle, err) |
| 106 | } |
| 107 | return nil |
| 108 | case WorkerLifecycleDraining, WorkerLifecycleRetired: |
| 109 | if s.Assignment == nil { |
| 110 | return nil |
| 111 | } |
| 112 | if err := validateWorkerAssignment(s.Assignment); err != nil { |
| 113 | return fmt.Errorf("lifecycle %q has invalid assignment metadata: %w", lifecycle, err) |
| 114 | } |
| 115 | return nil |
| 116 | default: |
| 117 | return fmt.Errorf("unknown worker lifecycle %q", lifecycle) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // Transition validates a lifecycle change and returns the next worker state. |
| 122 | // When assignment metadata is omitted, the current assignment is carried |