eventscheduler.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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/util"
  19. )
  20. var (
  21. eventScheduler *cron.Cron
  22. )
  23. func stopEventScheduler() {
  24. if eventScheduler != nil {
  25. eventScheduler.Stop()
  26. eventScheduler = nil
  27. }
  28. }
  29. func startEventScheduler() {
  30. stopEventScheduler()
  31. eventScheduler = cron.New(cron.WithLocation(time.UTC), cron.WithLogger(cron.DiscardLogger))
  32. eventManager.loadRules()
  33. _, err := eventScheduler.AddFunc("@every 10m", eventManager.loadRules)
  34. util.PanicOnError(err)
  35. eventScheduler.Start()
  36. }