worker_reloader.go 1013 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package worker_reloader
  2. import (
  3. "context"
  4. "time"
  5. "github.com/vladimirok5959/golang-ip2location/internal/client"
  6. "github.com/vladimirok5959/golang-ip2location/internal/consts"
  7. "github.com/vladimirok5959/golang-utils/utils/http/logger"
  8. "github.com/vladimirok5959/golang-worker/worker"
  9. )
  10. func New(cl *client.Client) *worker.Worker {
  11. return worker.New(func(ctx context.Context, w *worker.Worker, o *[]worker.Iface) {
  12. select {
  13. case <-ctx.Done():
  14. return
  15. case <-time.After(time.Duration(consts.Config.DbUpdateTime) * time.Minute):
  16. }
  17. if cl, ok := (*o)[0].(*client.Client); ok {
  18. Run(ctx, cl)
  19. }
  20. }, &[]worker.Iface{
  21. cl,
  22. })
  23. }
  24. func Run(ctx context.Context, cl *client.Client) {
  25. logger.LogInfo("worker reloader: trying to reload database\n")
  26. var err error
  27. if err = cl.ReloadDatabase(ctx); err != nil {
  28. logger.LogInternalError(err)
  29. }
  30. if err == nil {
  31. logger.LogInfo("worker reloader: reloading done\n")
  32. } else {
  33. logger.LogInfo("worker reloader: reloading done with errors\n")
  34. }
  35. }