Created
January 27, 2026 17:41
-
-
Save polosaty/61e1bb9a57a6d160e6b704647249c325 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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