|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/openshift-online/async-routine" |
| 6 | + "github.com/openshift-online/async-routine/opid" |
| 7 | + "log/slog" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +var _ async.RoutinesObserver = (*exampleRoutineObserver)(nil) |
| 12 | + |
| 13 | +func main() { |
| 14 | + slog.Info("Program started") |
| 15 | + |
| 16 | + // Setup the AsyncRoutineManager |
| 17 | + async.Manager( |
| 18 | + // Take a snapshot every 2 seconds |
| 19 | + async.WithSnapshottingInterval(2 * time.Second)). |
| 20 | + // Start the routine monitor |
| 21 | + Monitor().Start() |
| 22 | + |
| 23 | + // Add our custom observer to the list of routine observers |
| 24 | + _ = async.Manager().AddObserver(&exampleRoutineObserver{}) |
| 25 | + |
| 26 | + for i := 0; i < 10; i++ { |
| 27 | + foo(opid.NewContext()) |
| 28 | + } |
| 29 | + |
| 30 | + // Wait enough time to have some routine started |
| 31 | + time.Sleep(4 * time.Second) |
| 32 | +} |
| 33 | + |
| 34 | +func foo(ctx context.Context) { |
| 35 | + slog.Info("foo() started", |
| 36 | + "opid", opid.FromContext(ctx)) |
| 37 | + bar(ctx) |
| 38 | + slog.Info("foo() ended", |
| 39 | + "opid", opid.FromContext(ctx)) |
| 40 | +} |
| 41 | + |
| 42 | +func bar(ctx context.Context) { |
| 43 | + slog.Info("bar() started", |
| 44 | + "opid", opid.FromContext(ctx)) |
| 45 | + async.NewAsyncRoutine("parent go routine", ctx, |
| 46 | + func() { |
| 47 | + parentGoroutine(ctx) |
| 48 | + }). |
| 49 | + Timebox(2 * time.Second). |
| 50 | + Run() |
| 51 | + slog.Info("bar() ended", |
| 52 | + "opid", opid.FromContext(ctx)) |
| 53 | +} |
| 54 | + |
| 55 | +func parentGoroutine(ctx context.Context) { |
| 56 | + slog.Info("parentGoroutine() started", |
| 57 | + "opid", opid.FromContext(ctx)) |
| 58 | + |
| 59 | + async.NewAsyncRoutine("stuck in select", ctx, stuckInSelect). |
| 60 | + Timebox(2 * time.Second). |
| 61 | + Run() |
| 62 | + time.Sleep(500 * time.Millisecond) |
| 63 | + |
| 64 | + slog.Info("parentGoroutine() ended", |
| 65 | + "opid", opid.FromContext(ctx)) |
| 66 | +} |
| 67 | + |
| 68 | +func stuckInSelect() { |
| 69 | + slog.Info("parentGoroutine() started") |
| 70 | + select {} |
| 71 | + slog.Info("parentGoroutine() ended") |
| 72 | +} |
| 73 | + |
| 74 | +type exampleRoutineObserver struct{} |
| 75 | + |
| 76 | +func (e exampleRoutineObserver) RoutineStarted(routine async.AsyncRoutine) { |
| 77 | + slog.Info("Routine started", |
| 78 | + "name", routine.Name(), |
| 79 | + "opid", routine.OpId(), |
| 80 | + "parent-opid", routine.OriginatorOpId(), |
| 81 | + ) |
| 82 | +} |
| 83 | + |
| 84 | +func (e exampleRoutineObserver) RoutineFinished(routine async.AsyncRoutine) { |
| 85 | + slog.Info("Routine finished", |
| 86 | + "name", routine.Name(), |
| 87 | + "opid", routine.OpId(), |
| 88 | + "parent-opid", routine.OriginatorOpId(), |
| 89 | + ) |
| 90 | +} |
| 91 | + |
| 92 | +func (e exampleRoutineObserver) RoutineExceededTimebox(routine async.AsyncRoutine) { |
| 93 | + slog.Warn("Routine exceeded timebox", |
| 94 | + "name", routine.Name(), |
| 95 | + "opid", routine.OpId(), |
| 96 | + "parent-opid", routine.OriginatorOpId(), |
| 97 | + "startedAt", routine.StartedAt(), |
| 98 | + ) |
| 99 | +} |
| 100 | + |
| 101 | +func (e exampleRoutineObserver) RunningRoutineCount(count int) { |
| 102 | + // nothing to do in this example |
| 103 | +} |
| 104 | + |
| 105 | +func (e exampleRoutineObserver) RunningRoutineByNameCount(name string, count int) { |
| 106 | + // nothing to do in this example |
| 107 | +} |
0 commit comments