Skip to content

Instantly share code, notes, and snippets.

@fabiosussetto
Last active December 20, 2018 18:19
Show Gist options
  • Save fabiosussetto/cb3ed57f92062978815448bd7e1ba632 to your computer and use it in GitHub Desktop.
Save fabiosussetto/cb3ed57f92062978815448bd7e1ba632 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math/rand"
"os"
"os/signal"
"sync"
"syscall"
"time"
)
type runner struct {
}
func main() {
var gracefulStop = make(chan os.Signal)
signal.Notify(gracefulStop, syscall.SIGTERM)
signal.Notify(gracefulStop, syscall.SIGINT)
shutdownChan := make(chan int)
go func() {
sig := <-gracefulStop
fmt.Printf("caught sig: %+v \n", sig)
// used to stop processing goroutines (infinite for loop below) to do work while we clean up
close(shutdownChan)
fmt.Println("Wait for 3 second to finish processing")
time.Sleep(3 * time.Second)
os.Exit(0)
}()
var wg sync.WaitGroup
parallel := 4
wg.Add(parallel)
for i := 0; i < parallel; i++ {
go func(workerIndex int) {
defer wg.Done()
for {
select {
default:
secsToWait := rand.Intn(5)
fmt.Printf("[worker %d] Doing some work for %d seconds...\n", workerIndex, secsToWait)
time.Sleep(time.Duration(secsToWait) * time.Second)
fmt.Printf("[worker %d] ... done! \n", workerIndex)
case <-shutdownChan:
break
}
}
}(i)
}
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment