2
0
mirror of https://github.com/hibiken/asynq.git synced 2025-08-19 15:08:55 +08:00

Change Server API

* Rename ServerStatus to ServerState internally

* Rename terminate to shutdown internally

* Update Scheduler API to match Server API
This commit is contained in:
Ken Hibino
2021-03-23 06:20:54 -07:00
parent 476812475e
commit 9c95c41651
26 changed files with 175 additions and 169 deletions

View File

@@ -215,52 +215,55 @@ type Z struct {
Score int64
}
// ServerStatus represents status of a server.
// ServerStatus methods are concurrency safe.
type ServerStatus struct {
// ServerState represents state of a server.
// ServerState methods are concurrency safe.
type ServerState struct {
mu sync.Mutex
val ServerStatusValue
val ServerStateValue
}
// NewServerStatus returns a new status instance given an initial value.
func NewServerStatus(v ServerStatusValue) *ServerStatus {
return &ServerStatus{val: v}
// NewServerState returns a new state instance.
// Initial state is set to StateNew.
func NewServerState() *ServerState {
return &ServerState{val: StateNew}
}
type ServerStatusValue int
type ServerStateValue int
const (
// StatusIdle indicates the server is in idle state.
StatusIdle ServerStatusValue = iota
// StateNew represents a new server. Server begins in
// this state and then transition to StatusActive when
// Start or Run is callled.
StateNew ServerStateValue = iota
// StatusRunning indicates the server is up and active.
StatusRunning
// StateActive indicates the server is up and active.
StateActive
// StatusQuiet indicates the server is up but not active.
StatusQuiet
// StateStopped indicates the server is up but no longer processing new tasks.
StateStopped
// StatusStopped indicates the server server has been stopped.
StatusStopped
// StateClosed indicates the server has been shutdown.
StateClosed
)
var statuses = []string{
"idle",
"running",
"quiet",
var serverStates = []string{
"new",
"active",
"stopped",
"closed",
}
func (s *ServerStatus) String() string {
func (s *ServerState) String() string {
s.mu.Lock()
defer s.mu.Unlock()
if StatusIdle <= s.val && s.val <= StatusStopped {
return statuses[s.val]
if StateNew <= s.val && s.val <= StateClosed {
return serverStates[s.val]
}
return "unknown status"
}
// Get returns the status value.
func (s *ServerStatus) Get() ServerStatusValue {
func (s *ServerState) Get() ServerStateValue {
s.mu.Lock()
v := s.val
s.mu.Unlock()
@@ -268,7 +271,7 @@ func (s *ServerStatus) Get() ServerStatusValue {
}
// Set sets the status value.
func (s *ServerStatus) Set(v ServerStatusValue) {
func (s *ServerState) Set(v ServerStateValue) {
s.mu.Lock()
s.val = v
s.mu.Unlock()

View File

@@ -400,7 +400,7 @@ func TestServerInfoEncoding(t *testing.T) {
Concurrency: 10,
Queues: map[string]int{"default": 1, "critical": 2},
StrictPriority: false,
Status: "running",
Status: "active",
Started: time.Now().Add(-3 * time.Hour),
ActiveWorkerCount: 8,
},
@@ -530,7 +530,7 @@ func TestSchedulerEnqueueEventEncoding(t *testing.T) {
// Test for status being accessed by multiple goroutines.
// Run with -race flag to check for data race.
func TestStatusConcurrentAccess(t *testing.T) {
status := NewServerStatus(StatusIdle)
status := NewServerState()
var wg sync.WaitGroup
@@ -544,7 +544,7 @@ func TestStatusConcurrentAccess(t *testing.T) {
wg.Add(1)
go func() {
defer wg.Done()
status.Set(StatusStopped)
status.Set(StateClosed)
_ = status.String()
}()