this package provides an easy way to schedule functions using an event trigger.
A timeout trigger can be defined as follows:
trigger := scheduler.NewTimeoutTrigger(time.Second)
which can then be used to schedule functions as follows:
group, ctx := scheduler.New(ctx, trigger)
where, ctx
in input is the parent context and one in output is to be used to pass during scheduling functions.
group.Go(ctx, func() error {
// your logic
return nil
})
Finally wait for all functions to return using Wait
:
// wait for func executions to be over.
err := group.Wait()
triggers can be of following types:
// a timeout trigger allows execution on a timeout
timeoutTrigger := scheduler.NewTimeoutTrigger(timeDuration)
// trigger now causes functions to execute immediately
triggerNow := scheduler.TriggerNow()
// similarly, an event can used to trigger functions, event being ctx.Done()
triggerOnEvent := scheduler.NewContextTrigger(ctx)