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.

83 lines
1.4 KiB

package gum
import (
var WorkerID int
type Worker struct{}
// Example loop, it will be spwaned in a goroutine
func (w *Worker) Run(um UnitManager) {
ticker := time.NewTicker(time.Second)
// Worker's loop
for {
select {
case <-ticker.C:
// Read from channel if this worker unit should stop
case <-um.ShouldStop():
// Shutdown work for current unit
// Notify manager that this unit is done.
func (w *Worker) Shutdown() {
// Do shutdown procedure for worker
func NewWorker() *Worker {
return &Worker{}
func DoRunMain(pid chan int, quit chan<- bool) {
pid <- os.Getpid()
// Create a unit manager
manager := NewManager()
// Shutdown all units on SIGINT
// NewWorker returns a type implementing WorkUnit interface unit :=
worker1 := NewWorker()
worker2 := NewWorker()
// Register the unit with the manager
// Start the manager
go manager.Run()
// Wait for all units to shutdown gracefully through their `Shutdown` method
quit <- <-manager.Quit
func TestRunMain(t *testing.T) {
mainPid := make(chan int, 1)
quit := make(chan bool)
go DoRunMain(mainPid, quit)
time.Sleep(3 * time.Second)
syscall.Kill(<-mainPid, syscall.SIGINT)