diff options
author | Wolfy-J <[email protected]> | 2018-01-23 19:51:15 -0500 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2018-01-23 19:51:15 -0500 |
commit | 78a42de837928cf7d10a1ae04d7e82e56d66e1e2 (patch) | |
tree | 8882b9a051bcc9c42328df583c0bb8c39a89591e /state.go | |
parent | fa4bd78d9f7c5f74e8445374370927c742fc4e78 (diff) |
API update
Diffstat (limited to 'state.go')
-rw-r--r-- | state.go | 88 |
1 files changed, 75 insertions, 13 deletions
@@ -1,39 +1,101 @@ package roadrunner -// State is current state int. -type State int +import ( + "sync" + "sync/atomic" + "time" +) + +// State represents worker status and updated time. +type State interface { + // Value returns state value + Value() int64 //todo: change to state value + + // NumExecs shows how many times worker was invoked + NumExecs() uint64 + + // Updated indicates a moment updated last state change + Updated() time.Time +} const ( // StateInactive - no associated process - StateInactive State = iota - // StateBooting - relay attached but w.Start() not executed - StateBooting + StateInactive int64 = iota // StateReady - ready for job. StateReady // StateWorking - working on given payload. StateWorking // StateStopped - process has been terminated StateStopped - // StateError - error State (can't be used) - StateError + // StateErrored - error state (can't be used) + StateErrored ) +type state struct { + mu sync.RWMutex + value int64 + numExecs uint64 + updated time.Time +} + +func newState(value int64) *state { + return &state{value: value, updated: time.Now()} +} + // String returns current state as string. -func (s State) String() string { - switch s { +func (s *state) String() string { + switch s.value { case StateInactive: return "inactive" - case StateBooting: - return "booting" case StateReady: return "ready" case StateWorking: return "working" case StateStopped: return "stopped" - case StateError: - return "error" + case StateErrored: + return "errored" } return "undefined" } + +// Value state returns state value +func (s *state) Value() int64 { + s.mu.RLock() + defer s.mu.RUnlock() + + return s.value +} + +// IsActive returns true if worker not Inactive or Stopped +func (s *state) IsActive() bool { + state := s.Value() + return state == StateWorking || state == StateReady +} + +// Updated indicates a moment updated last state change +func (s *state) Updated() time.Time { + s.mu.RLock() + defer s.mu.RUnlock() + + return s.updated +} + +func (s *state) NumExecs() uint64 { + return atomic.LoadUint64(&s.numExecs) +} + +// change state value (status) +func (s *state) set(value int64) { + s.mu.Lock() + defer s.mu.Unlock() + + s.value = value + s.updated = time.Now() +} + +// register new execution atomically +func (s *state) registerExec() { + atomic.AddUint64(&s.numExecs, 1) +} |