github.com/blend/go-sdk@v1.20220411.3/cron/delay_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 "sync/atomic" 13 "time" 14 ) 15 16 // Delay returns a composite schedule that delays 17 // a given schedule by a given duration. 18 func Delay(d time.Duration, then Schedule) *DelaySchedule { 19 return &DelaySchedule{ 20 delay: d, 21 then: then, 22 } 23 } 24 25 var ( 26 _ Schedule = (*DelaySchedule)(nil) 27 _ fmt.Stringer = (*DelaySchedule)(nil) 28 ) 29 30 // DelaySchedule wraps a schedule with a delay. 31 type DelaySchedule struct { 32 didRun int32 33 delay time.Duration 34 then Schedule 35 } 36 37 // Next implements Schedule. 38 func (ds *DelaySchedule) Next(after time.Time) time.Time { 39 if atomic.CompareAndSwapInt32(&ds.didRun, 0, 1) { 40 return ds.then.Next(after).Add(ds.delay) 41 } 42 return ds.then.Next(after) 43 } 44 45 // String implements a string schedule. 46 func (ds *DelaySchedule) String() string { 47 return fmt.Sprintf("%s %v %v", StringScheduleDelay, ds.delay, ds.then) 48 }