diff --git a/README.md b/README.md index 55461b2..eb1f11d 100644 --- a/README.md +++ b/README.md @@ -7,28 +7,39 @@ cycle-scheduler is a simple scheduler lib, handling tasks and executes them at r ## Examples * Init a new scheduler with 4 workers ```go +package main + import ( "context" + "time" scheduler "gitea.thegux.fr/rmanach/cycle-scheduler.git" ) -ctx := context.Background() -s := scheduler.NewSchedulerCycle(ctx, 4) +func main() { + ctx, fnCancel := context.WithCancel(context.Background()) + s := scheduler.NewSchedulerCycle(ctx, 4) -// add a task with an execution interval of 2 ms (executed every 2 ms) -// and a maximum duration of 30 second. -taskID := s.Delay( - func(ctx context.Context) (any, error) { - // ... - return any, nil - }, - scheduler.WithExecInterval(2*time.Millisecond), - scheduler.WithMaxDuration(30*time.Second) -) + // add a task with an execution interval of 2 ms (executed every 2 ms) + // and a maximum duration of 30 second. + s.Delay( + func(ctx context.Context) (any, error) { + // ... + return nil, nil + }, + scheduler.WithExecInterval(2*time.Millisecond), + scheduler.WithMaxDuration(30*time.Second), + ) -<-ctx.Done() -<-s.Done() + // stop the program after 5 seconds + go func() { + time.Sleep(5 * time.Second) + fnCancel() + }() + + <-ctx.Done() + <-s.Done() +} ``` **NOTE**: for `Delay` optionals arguments, check the `NewTask` method documentation for more details.