schedule.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package schedule
  15. import (
  16. "time"
  17. "github.com/robfig/cron/v3"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. )
  20. const layout = "2006-01-02 15:04:05"
  21. func IsInScheduleRanges(now time.Time, timeRanges []api.DatetimeRange) (bool, error) {
  22. for _, tRange := range timeRanges {
  23. isIn, err := IsInScheduleRange(now, tRange.Begin, tRange.End)
  24. if err != nil {
  25. return false, err
  26. }
  27. if isIn {
  28. return true, nil
  29. }
  30. }
  31. return false, nil
  32. }
  33. func IsInScheduleRange(now time.Time, start string, end string) (bool, error) {
  34. s, err := time.Parse(layout, start)
  35. if err != nil {
  36. return false, err
  37. }
  38. e, err := time.Parse(layout, end)
  39. if err != nil {
  40. return false, err
  41. }
  42. isBefore := s.Before(now)
  43. isAfter := e.After(now)
  44. if isBefore && isAfter {
  45. return true, nil
  46. }
  47. return false, nil
  48. }
  49. func IsAfterTimeRanges(now time.Time, ranges []api.DatetimeRange) bool {
  50. if len(ranges) < 1 {
  51. return false
  52. }
  53. for _, r := range ranges {
  54. isAfter, err := IsAfterTimeRange(now, r.End)
  55. if err != nil || !isAfter {
  56. return false
  57. }
  58. }
  59. return true
  60. }
  61. func IsAfterTimeRange(now time.Time, end string) (bool, error) {
  62. e, err := time.Parse(layout, end)
  63. if err != nil {
  64. return false, err
  65. }
  66. return now.After(e), nil
  67. }
  68. // IsInRunningSchedule checks whether the rule should be running, eg:
  69. // If the duration is 10min, and cron is "0 0 * * *", and the current time is 00:00:02
  70. // And the rule should be started immediately instead of checking it on the next day.
  71. func IsInRunningSchedule(cronExpr string, now time.Time, d time.Duration) (bool, time.Duration, error) {
  72. s, err := cron.ParseStandard(cronExpr)
  73. if err != nil {
  74. return false, 0, err
  75. }
  76. previousSchedule := s.Next(now.Add(-d))
  77. if now.After(previousSchedule) && now.Before(previousSchedule.Add(d)) {
  78. return true, previousSchedule.Add(d).Sub(now), nil
  79. }
  80. return false, 0, nil
  81. }