github.com/blend/go-sdk@v1.20220411.3/cron/immediately_then.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 // Interface assertions. 17 var ( 18 _ Schedule = (*ImmediateSchedule)(nil) 19 _ fmt.Stringer = (*ImmediateSchedule)(nil) 20 ) 21 22 // Immediately Returns a schedule that causes a job to run immediately on start, 23 // with an optional subsequent schedule. 24 func Immediately() *ImmediateSchedule { 25 return &ImmediateSchedule{} 26 } 27 28 // ImmediateSchedule fires immediately with an optional continuation schedule. 29 type ImmediateSchedule struct { 30 didRun int32 31 then Schedule 32 } 33 34 // String returns a string representation of the schedul.e 35 func (i *ImmediateSchedule) String() string { 36 if i.then != nil { 37 return fmt.Sprintf("%s %v", StringScheduleImmediatelyThen, i.then) 38 } 39 return StringScheduleImmediately 40 } 41 42 // Then allows you to specify a subsequent schedule after the first run. 43 func (i *ImmediateSchedule) Then(then Schedule) Schedule { 44 i.then = then 45 return i 46 } 47 48 // Next implements Schedule. 49 func (i *ImmediateSchedule) Next(after time.Time) time.Time { 50 if atomic.CompareAndSwapInt32(&i.didRun, 0, 1) { 51 return Now() 52 } 53 if i.then != nil { 54 return i.then.Next(after) 55 } 56 return Zero 57 }