Skip to content

Instantly share code, notes, and snippets.

@polosaty
Created January 27, 2026 17:41
Show Gist options
  • Select an option

  • Save polosaty/61e1bb9a57a6d160e6b704647249c325 to your computer and use it in GitHub Desktop.

Select an option

Save polosaty/61e1bb9a57a6d160e6b704647249c325 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"log"
"os"
"os/signal"
"sync"
"syscall"
"time"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
err := work(ctx)
if err != nil {
log.Println("worker stopped with err == ", err)
}
}()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
//go func() {
s := <-sigChan
// поймали один из
log.Printf("got %v signal", s)
cancel()
wg.Wait()
log.Println("shutdown complete")
//}()
}
func work(ctx context.Context) error {
log.Println("worker started")
for {
select {
case <-ctx.Done():
log.Println("worker got context done")
return ctx.Err()
default:
//some work
log.Println("worker so some work")
time.Sleep(1 * time.Second)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment