schedule.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. "github.com/lf-edge/ekuiper/pkg/cast"
  20. )
  21. const layout = "2006-01-02 15:04:05"
  22. func IsInScheduleRanges(now time.Time, timeRanges []api.DatetimeRange) (bool, error) {
  23. for _, tRange := range timeRanges {
  24. isIn, err := IsInScheduleRange(now, tRange.Begin, tRange.End)
  25. if err != nil {
  26. return false, err
  27. }
  28. if isIn {
  29. return true, nil
  30. }
  31. }
  32. return false, nil
  33. }
  34. func IsInScheduleRange(now time.Time, start string, end string) (bool, error) {
  35. return isInTimeRange(now, start, end)
  36. }
  37. func isInTimeRange(now time.Time, start string, end string) (bool, error) {
  38. s, err := cast.InterfaceToTime(start, layout)
  39. if err != nil {
  40. return false, err
  41. }
  42. e, err := cast.InterfaceToTime(end, layout)
  43. if err != nil {
  44. return false, err
  45. }
  46. isBefore := s.Before(now)
  47. isAfter := e.After(now)
  48. if isBefore && isAfter {
  49. return true, nil
  50. }
  51. return false, nil
  52. }
  53. func IsAfterTimeRanges(now time.Time, ranges []api.DatetimeRange) bool {
  54. if len(ranges) < 1 {
  55. return false
  56. }
  57. for _, r := range ranges {
  58. isAfter, err := IsAfterTimeRange(now, r.End)
  59. if err != nil || !isAfter {
  60. return false
  61. }
  62. }
  63. return true
  64. }
  65. func IsAfterTimeRange(now time.Time, end string) (bool, error) {
  66. e, err := time.Parse(layout, end)
  67. if err != nil {
  68. return false, err
  69. }
  70. return now.After(e), nil
  71. }
  72. // IsInRunningSchedule checks whether the rule should be running, eg:
  73. // If the duration is 10min, and cron is "0 0 * * *", and the current time is 00:00:02
  74. // And the rule should be started immediately instead of checking it on the next day.
  75. func IsInRunningSchedule(cronExpr string, now time.Time, d time.Duration) (bool, time.Duration, error) {
  76. s, err := cron.ParseStandard(cronExpr)
  77. if err != nil {
  78. return false, 0, err
  79. }
  80. previousSchedule := s.Next(now.Add(-d))
  81. if now.After(previousSchedule) && now.Before(previousSchedule.Add(d)) {
  82. return true, previousSchedule.Add(d).Sub(now), nil
  83. }
  84. return false, 0, nil
  85. }