refactoring

master
Chakib Benziane 5 years ago
parent daef5fbcf0
commit 2a68eb1323

@ -23,7 +23,7 @@ The `Manager` handles communication and synchronized shutdown procedure.
1. Create a unit manager 1. Create a unit manager
2. Implement the `WorkUnit` on your goroutines 2. Implement the `WorkUnit` on your goroutines
3. Add units to the manager 3. Add units to the manager
4. Start the manager and wait on its `Quit` channel 4. Run the manager and wait on its `Quit` channel
```golang ```golang
import ( import (
@ -81,8 +81,8 @@ func main() {
manager.AddUnit(worker) manager.AddUnit(worker)
manager.AddUnit(worker2) manager.AddUnit(worker2)
// Start the manager // Run the manager
go manager.Start() go manager.Run()
// Wait for all units to shutdown gracefully through their `Shutdown` method // Wait for all units to shutdown gracefully through their `Shutdown` method

@ -12,7 +12,7 @@ import (
var idGen = IdGenerator() var idGen = IdGenerator()
type WorkUnit interface { type WorkUnit interface {
Spawn(UnitManager) Run(UnitManager)
Shutdown() Shutdown()
} }
@ -57,12 +57,12 @@ type Manager struct {
panic chan error // Used for panicing goroutines panic chan error // Used for panicing goroutines
} }
func (m *Manager) Start() { func (m *Manager) Run() {
log.Println("Starting manager ...") log.Println("Starting manager ...")
for unitName, w := range m.workers { for unitName, w := range m.workers {
log.Printf("Starting <%s>\n", unitName) log.Printf("Starting <%s>\n", unitName)
go w.unit.Spawn(w) go w.unit.Run(w)
} }
for { for {

@ -13,7 +13,7 @@ var WorkerID int
type Worker struct{} type Worker struct{}
// Example loop, it will be spwaned in a goroutine // Example loop, it will be spwaned in a goroutine
func (w *Worker) Spawn(um UnitManager) { func (w *Worker) Run(um UnitManager) {
ticker := time.NewTicker(time.Second) ticker := time.NewTicker(time.Second)
// Worker's loop // Worker's loop
@ -62,7 +62,7 @@ func DoRunMain(pid chan int, quit chan<- bool) {
manager.AddUnit(worker2) manager.AddUnit(worker2)
// Start the manager // Start the manager
go manager.Start() go manager.Run()
// Wait for all units to shutdown gracefully through their `Shutdown` method // Wait for all units to shutdown gracefully through their `Shutdown` method
quit <- <-manager.Quit quit <- <-manager.Quit

Loading…
Cancel
Save