14360: Add "idle" state.
[arvados.git] / lib / dispatchcloud / worker / worker.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package worker
6
7 import (
8         "time"
9 )
10
11 // State indicates whether a worker is available to do work, and (if
12 // not) whether/when it is expected to become ready.
13 type State int
14
15 const (
16         StateUnknown  State = iota // might be running a container already
17         StateBooting               // instance is booting
18         StateIdle                  // instance booted, no containers are running
19         StateRunning               // instance is running one or more containers
20         StateShutdown              // worker has stopped monitoring the instance
21         StateHold                  // running, but not available to run new containers
22 )
23
24 const (
25         // TODO: configurable
26         maxPingFailTime = 10 * time.Minute
27 )
28
29 var stateString = map[State]string{
30         StateUnknown:  "unknown",
31         StateBooting:  "booting",
32         StateIdle:     "idle",
33         StateRunning:  "running",
34         StateShutdown: "shutdown",
35         StateHold:     "hold",
36 }
37
38 // String implements fmt.Stringer.
39 func (s State) String() string {
40         return stateString[s]
41 }
42
43 // MarshalText implements encoding.TextMarshaler so a JSON encoding of
44 // map[State]anything uses the state's string representation.
45 func (s State) MarshalText() ([]byte, error) {
46         return []byte(stateString[s]), nil
47 }