You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.0 KiB
Markdown

5 years ago
# gum
Go Unit Manager is a simple Goroutine unit manager for GoLang.
Features:
- Scheduling of multiple goroutines.
5 years ago
- Shutdown on `os.Signal` events.
5 years ago
- Gracefull shutdown of units
## Overview
7 months ago
A unit is a type that implements `WorkUnit` interface. The `Run()` method
5 years ago
of each registered unit is spawned in its own goroutine.
5 years ago
The `Manager` handles communication and synchronized shutdown procedure.
## Usage
1. Create a unit manager
2. Implement the `WorkUnit` on your goroutines
3. Add units to the manager
5 years ago
4. Run the manager and wait on its `Quit` channel
5 years ago
```golang
import (
"os"
"log"
"time"
5 years ago
gum "git.sp4ke.com/sp4ke/gum.git"
5 years ago
)
type Worker struct{}
// Example loop, will be spwaned inside a goroutine
7 months ago
func (w *Worker) Run(um UnitManager) {
5 years ago
ticker := time.NewTicker(time.Second)
5 years ago
// Worker's loop
5 years ago
for {
select {
case <-ticker.C:
log.Println("tick")
5 years ago
// Read from channel if this worker unit should stop
5 years ago
case <-um.ShouldStop():
5 years ago
// Shutdown work for current unit
5 years ago
w.Shutdown()
5 years ago
// Notify manager that this unit is done.
5 years ago
um.Done()
}
}
5 years ago
}
func (w *Worker) Shutdown() {
7 months ago
// shutdown procedure for worker
5 years ago
return
}
func NewWorker() *Worker {
return &Worker{}
}
func main() {
// Create a unit manager
manager := gum.NewManager()
5 years ago
// Shutdown all units on SIGINT
manager.ShutdownOn(os.Interrupt)
5 years ago
// NewWorker returns a type implementing WorkUnit interface unit :=
worker := NewWorker()
worker2 := NewWorker()
5 years ago
// Register the unit with the manager
7 months ago
manager.AddUnit(worker, "work1")
manager.AddUnit(worker2, "work2")
5 years ago
5 years ago
// Run the manager
go manager.Run()
5 years ago
// Wait for all units to shutdown gracefully through their `Shutdown` method
<-manager.Quit
}
```
## Issues and Comments
5 years ago
This repo is a mirror. For any question or issues use the repo hosted at
[https://git.sp4ke.com/sp4ke/gum.git](https://git.sp4ke.com/sp4ke/gum.git)
5 years ago