github.com/blend/go-sdk@v1.20220411.3/cron/interval_schedule.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package cron
     9  
    10  import (
    11  	"fmt"
    12  	"time"
    13  )
    14  
    15  var (
    16  	_ Schedule     = (*IntervalSchedule)(nil)
    17  	_ fmt.Stringer = (*IntervalSchedule)(nil)
    18  )
    19  
    20  // EverySecond returns a schedule that fires every second.
    21  func EverySecond() IntervalSchedule {
    22  	return IntervalSchedule{Every: time.Second}
    23  }
    24  
    25  // EveryMinute returns a schedule that fires every minute.
    26  func EveryMinute() IntervalSchedule {
    27  	return IntervalSchedule{Every: time.Minute}
    28  }
    29  
    30  // EveryHour returns a schedule that fire every hour.
    31  func EveryHour() IntervalSchedule {
    32  	return IntervalSchedule{Every: time.Hour}
    33  }
    34  
    35  // Every returns a schedule that fires every given interval.
    36  func Every(interval time.Duration) IntervalSchedule {
    37  	return IntervalSchedule{Every: interval}
    38  }
    39  
    40  // IntervalSchedule is as chedule that fires every given interval with an optional start delay.
    41  type IntervalSchedule struct {
    42  	Every time.Duration
    43  }
    44  
    45  // String returns a string representation of the schedule.
    46  func (i IntervalSchedule) String() string {
    47  	return fmt.Sprintf("%s %v", StringScheduleEvery, i.Every)
    48  }
    49  
    50  // Next implements Schedule.
    51  func (i IntervalSchedule) Next(after time.Time) time.Time {
    52  	if after.IsZero() {
    53  		return Now().Add(i.Every)
    54  	}
    55  	return after.Add(i.Every)
    56  }