blob: 8f7d939b94bace37f28e00af03d59529f6c52ceb (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
package internal
import (
"fmt"
"sync/atomic"
)
// State represents WorkerProcess status and updated time.
type State interface {
fmt.Stringer
// Value returns WorkerState value
Value() int64
// Set sets the WorkerState
Set(value int64)
// NumJobs shows how many times WorkerProcess was invoked
NumExecs() int64
// IsActive returns true if WorkerProcess not Inactive or Stopped
IsActive() bool
// RegisterExec using to registering php executions
RegisterExec()
// SetLastUsed sets worker last used time
SetLastUsed(lu uint64)
// LastUsed return worker last used time
LastUsed() uint64
}
const (
// StateInactive - no associated process
StateInactive int64 = iota
// StateReady - ready for job.
StateReady
// StateWorking - working on given payload.
StateWorking
// StateInvalid - indicates that WorkerProcess is being disabled and will be removed.
StateInvalid
// StateStopping - process is being softly stopped.
StateStopping
StateKilling
// State of worker, when no need to allocate new one
StateDestroyed
// StateStopped - process has been terminated.
StateStopped
// StateErrored - error WorkerState (can't be used).
StateErrored
StateRemove
)
type WorkerState struct {
value int64
numExecs int64
// to be lightweight, use UnixNano
lastUsed uint64
}
// Thread safe
func NewWorkerState(value int64) *WorkerState {
return &WorkerState{value: value}
}
// String returns current WorkerState as string.
func (s *WorkerState) String() string {
switch s.Value() {
case StateInactive:
return "inactive"
case StateReady:
return "ready"
case StateWorking:
return "working"
case StateInvalid:
return "invalid"
case StateStopped:
return "stopped"
case StateErrored:
return "errored"
}
return "undefined"
}
// NumExecs returns number of registered WorkerProcess execs.
func (s *WorkerState) NumExecs() int64 {
return atomic.LoadInt64(&s.numExecs)
}
// Value WorkerState returns WorkerState value
func (s *WorkerState) Value() int64 {
return atomic.LoadInt64(&s.value)
}
// IsActive returns true if WorkerProcess not Inactive or Stopped
func (s *WorkerState) IsActive() bool {
val := s.Value()
return val == StateWorking || val == StateReady
}
// change WorkerState value (status)
func (s *WorkerState) Set(value int64) {
atomic.StoreInt64(&s.value, value)
}
// register new execution atomically
func (s *WorkerState) RegisterExec() {
atomic.AddInt64(&s.numExecs, 1)
}
// Update last used time
func (s *WorkerState) SetLastUsed(lu uint64) {
atomic.StoreUint64(&s.lastUsed, lu)
}
func (s *WorkerState) LastUsed() uint64 {
return atomic.LoadUint64(&s.lastUsed)
}
|