github.com/wangyougui/gf/v2@v2.6.5/os/gcron/gcron_schedule_check.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package gcron
     8  
     9  import (
    10  	"context"
    11  	"time"
    12  
    13  	"github.com/wangyougui/gf/v2/os/gtime"
    14  )
    15  
    16  // checkMeetAndUpdateLastSeconds checks if the given time `t` meets the runnable point for the job.
    17  // This function is called every second.
    18  func (s *cronSchedule) checkMeetAndUpdateLastSeconds(ctx context.Context, currentTime time.Time) (ok bool) {
    19  	var (
    20  		lastCheckTimestamp = s.getAndUpdateLastCheckTimestamp(ctx, currentTime)
    21  		lastCheckTime      = gtime.NewFromTimeStamp(lastCheckTimestamp)
    22  		lastMeetTime       = gtime.NewFromTimeStamp(s.lastMeetTimestamp.Val())
    23  	)
    24  	defer func() {
    25  		if ok {
    26  			s.lastMeetTimestamp.Set(currentTime.Unix())
    27  		}
    28  	}()
    29  	if !s.checkMinIntervalAndItemMapMeet(lastMeetTime.Time, lastCheckTime.Time, currentTime) {
    30  		return false
    31  	}
    32  	return true
    33  }
    34  
    35  func (s *cronSchedule) checkMinIntervalAndItemMapMeet(
    36  	lastMeetTime, lastCheckTime, currentTime time.Time,
    37  ) (ok bool) {
    38  	if s.everySeconds != 0 {
    39  		// It checks using interval.
    40  		secondsAfterCreated := lastCheckTime.UnixNano()/1e9 - s.createTimestamp
    41  		if secondsAfterCreated > 0 {
    42  			return secondsAfterCreated%s.everySeconds == 0
    43  		}
    44  		return false
    45  	}
    46  	if !s.checkMeetSecond(lastMeetTime, currentTime) {
    47  		return false
    48  	}
    49  	if !s.checkMeetMinute(currentTime) {
    50  		return false
    51  	}
    52  	if !s.checkMeetHour(currentTime) {
    53  		return false
    54  	}
    55  	if !s.checkMeetDay(currentTime) {
    56  		return false
    57  	}
    58  	if !s.checkMeetMonth(currentTime) {
    59  		return false
    60  	}
    61  	if !s.checkMeetWeek(currentTime) {
    62  		return false
    63  	}
    64  	return true
    65  }
    66  
    67  func (s *cronSchedule) checkMeetSecond(lastMeetTime, currentTime time.Time) (ok bool) {
    68  	if s.ignoreSeconds {
    69  		if currentTime.Unix()-lastMeetTime.Unix() < 60 {
    70  			return false
    71  		}
    72  	} else {
    73  		if !s.keyMatch(s.secondMap, currentTime.Second()) {
    74  			return false
    75  		}
    76  	}
    77  	return true
    78  }
    79  
    80  func (s *cronSchedule) checkMeetMinute(currentTime time.Time) (ok bool) {
    81  	if !s.keyMatch(s.minuteMap, currentTime.Minute()) {
    82  		return false
    83  	}
    84  	return true
    85  }
    86  
    87  func (s *cronSchedule) checkMeetHour(currentTime time.Time) (ok bool) {
    88  	if !s.keyMatch(s.hourMap, currentTime.Hour()) {
    89  		return false
    90  	}
    91  	return true
    92  }
    93  
    94  func (s *cronSchedule) checkMeetDay(currentTime time.Time) (ok bool) {
    95  	if !s.keyMatch(s.dayMap, currentTime.Day()) {
    96  		return false
    97  	}
    98  	return true
    99  }
   100  
   101  func (s *cronSchedule) checkMeetMonth(currentTime time.Time) (ok bool) {
   102  	if !s.keyMatch(s.monthMap, int(currentTime.Month())) {
   103  		return false
   104  	}
   105  	return true
   106  }
   107  
   108  func (s *cronSchedule) checkMeetWeek(currentTime time.Time) (ok bool) {
   109  	if !s.keyMatch(s.weekMap, int(currentTime.Weekday())) {
   110  		return false
   111  	}
   112  	return true
   113  }
   114  
   115  func (s *cronSchedule) keyMatch(m map[int]struct{}, key int) bool {
   116  	_, ok := m[key]
   117  	return ok
   118  }
   119  
   120  func (s *cronSchedule) checkItemMapMeet(lastMeetTime, currentTime time.Time) (ok bool) {
   121  	// second.
   122  	if s.ignoreSeconds {
   123  		if currentTime.Unix()-lastMeetTime.Unix() < 60 {
   124  			return false
   125  		}
   126  	} else {
   127  		if !s.keyMatch(s.secondMap, currentTime.Second()) {
   128  			return false
   129  		}
   130  	}
   131  	// minute.
   132  	if !s.keyMatch(s.minuteMap, currentTime.Minute()) {
   133  		return false
   134  	}
   135  	// hour.
   136  	if !s.keyMatch(s.hourMap, currentTime.Hour()) {
   137  		return false
   138  	}
   139  	// day.
   140  	if !s.keyMatch(s.dayMap, currentTime.Day()) {
   141  		return false
   142  	}
   143  	// month.
   144  	if !s.keyMatch(s.monthMap, int(currentTime.Month())) {
   145  		return false
   146  	}
   147  	// week.
   148  	if !s.keyMatch(s.weekMap, int(currentTime.Weekday())) {
   149  		return false
   150  	}
   151  	return true
   152  }