main.go 928 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "github.com/vladimirok5959/golang-worker/worker"
  7. )
  8. func main() {
  9. fmt.Printf("Start!\n")
  10. w1 := worker.New(func(ctx context.Context, w *worker.Worker) {
  11. fmt.Printf("Worker #1 one tick\n")
  12. for i := 0; i < 1000; i++ {
  13. select {
  14. case <-ctx.Done():
  15. fmt.Printf("Worker #1 fine I will shutdown!\n")
  16. return
  17. default:
  18. time.Sleep(1 * time.Millisecond)
  19. }
  20. }
  21. })
  22. w2 := worker.New(func(ctx context.Context, w *worker.Worker) {
  23. fmt.Printf("Worker #2 one tick\n")
  24. for i := 0; i < 1000; i++ {
  25. select {
  26. case <-ctx.Done():
  27. fmt.Printf("Worker #2 fine I will shutdown!\n")
  28. return
  29. default:
  30. time.Sleep(1 * time.Millisecond)
  31. }
  32. }
  33. })
  34. time.Sleep(3 * time.Second)
  35. w1.Finish()
  36. if err := w2.Shutdown(nil); err != nil {
  37. fmt.Printf("Worker #2 shutdown error: %s\n", err.Error())
  38. }
  39. time.Sleep(1 * time.Second)
  40. fmt.Printf("End!\n")
  41. }