Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
adwpc committed Jul 8, 2018
1 parent 247004f commit 141cb60
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion master.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"bytes"
"errors"
"io"
"sync"
"time"
)

type Master struct {
pool map[string]*Worker
pool map[string]*Worker
mutex sync.Mutex
}

func NewMaster() *Master {
Expand All @@ -19,22 +21,27 @@ func NewMaster() *Master {
}

func (m *Master) GetWorker(id string) *Worker {
m.mutex.Lock()
if m.pool == nil {
m.pool = make(map[string]*Worker)
}
if _, ok := m.pool[id]; !ok {
m.pool[id] = NewWorker(id)
m.pool[id].RegMaster(m)
}
m.mutex.Unlock()

return m.pool[id]
}

func (m *Master) RunWorker(id string) error {
m.mutex.Lock()
if m.pool == nil {
m.mutex.Unlock()
return errors.New("m.pool == nil")
}
worker, ok := m.pool[id]
m.mutex.Unlock()
if !ok {
return errors.New("can't find worker id=" + id)
}
Expand Down Expand Up @@ -75,7 +82,9 @@ func (m *Master) RunWorker(id string) error {
}

func (m *Master) DelWorker(id string) error {
m.mutex.Lock()
if worker, ok := m.pool[id]; ok {
m.mutex.Unlock()
worker.Stop()
for {
if !worker.Running {
Expand All @@ -85,6 +94,7 @@ func (m *Master) DelWorker(id string) error {
time.Sleep(20 * time.Millisecond)
}
} else {
m.mutex.Unlock()
return errors.New("can't find worker id=" + id)
}
return nil
Expand Down

0 comments on commit 141cb60

Please sign in to comment.