eventscheduler.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright (C) 2019 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package common
  15. import (
  16. "time"
  17. "github.com/robfig/cron/v3"
  18. "github.com/drakkan/sftpgo/v2/internal/dataprovider"
  19. "github.com/drakkan/sftpgo/v2/internal/logger"
  20. "github.com/drakkan/sftpgo/v2/internal/util"
  21. )
  22. var (
  23. eventScheduler *cron.Cron
  24. )
  25. func stopEventScheduler() {
  26. if eventScheduler != nil {
  27. eventScheduler.Stop()
  28. eventScheduler = nil
  29. }
  30. }
  31. func startEventScheduler() {
  32. stopEventScheduler()
  33. options := []cron.Option{
  34. cron.WithLogger(cron.DiscardLogger),
  35. }
  36. if !dataprovider.UseLocalTime() {
  37. eventManagerLog(logger.LevelDebug, "use UTC time for the scheduler")
  38. options = append(options, cron.WithLocation(time.UTC))
  39. }
  40. eventScheduler = cron.New(options...)
  41. eventManager.loadRules()
  42. _, err := eventScheduler.AddFunc("@every 10m", eventManager.loadRules)
  43. util.PanicOnError(err)
  44. eventScheduler.Start()
  45. }