github.com/yandex/pandora@v0.5.32/core/coreutil/schedule.go (about) 1 package coreutil 2 3 import ( 4 "sync" 5 "time" 6 7 "github.com/yandex/pandora/core" 8 ) 9 10 // NewCallbackOnFinishSchedule returns schedule that calls back once onFinish 11 // just before first callee could know, that schedule is finished. 12 // That is, calls onFinish once, first time, whet Next() returns ok == false 13 // or Left() returns 0. 14 func NewCallbackOnFinishSchedule(s core.Schedule, onFinish func()) core.Schedule { 15 return &callbackOnFinishSchedule{ 16 Schedule: s, 17 onFinish: onFinish, 18 } 19 } 20 21 type callbackOnFinishSchedule struct { 22 core.Schedule 23 onFinishOnce sync.Once 24 onFinish func() 25 } 26 27 func (s *callbackOnFinishSchedule) Next() (ts time.Time, ok bool) { 28 ts, ok = s.Schedule.Next() 29 if !ok { 30 s.onFinishOnce.Do(s.onFinish) 31 } 32 return 33 } 34 35 func (s *callbackOnFinishSchedule) Left() int { 36 left := s.Schedule.Left() 37 if left == 0 { 38 s.onFinishOnce.Do(s.onFinish) 39 } 40 return left 41 }