From 902f4c957a3d79f93a3ddf26571ba0dccea8092e Mon Sep 17 00:00:00 2001 From: James Shubin Date: Fri, 26 Jan 2024 19:09:36 -0500 Subject: [PATCH] lang: Print stats for debugging if function engine takes too long If we don't startup fast enough, print some debugging information. We should eventually change this to print the list of functions that aren't started yet, and also to give each function entry a better String method so that we have a better idea of what everything is. --- lang/lang.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lang/lang.go b/lang/lang.go index 3f526eb6..da4c5184 100644 --- a/lang/lang.go +++ b/lang/lang.go @@ -24,6 +24,7 @@ import ( "context" "fmt" "sync" + "time" "github.com/purpleidea/mgmt/engine" "github.com/purpleidea/mgmt/engine/local" @@ -42,6 +43,14 @@ import ( "github.com/purpleidea/mgmt/util/errwrap" ) +const ( + // EngineStartupStatsTimeout is the amount of time in seconds to wait + // between engine startup, and loaded event before printing some + // debugging stats. This is useful for finding bugs in the function + // engine. Set to zero to disable. + EngineStartupStatsTimeout = 10 +) + // Lang is the main language lexer/parser object. type Lang struct { Fs engine.Fs // connected fs where input dir or metadata exists @@ -313,6 +322,21 @@ func (obj *Lang) Run(ctx context.Context) (reterr error) { // wait for some activity obj.Logf("stream...") + // print some stats if the engine takes too long to startup + if EngineStartupStatsTimeout > 0 { + wg.Add(1) + go func() { + defer wg.Done() + select { + case <-obj.funcs.Loaded(): // funcs are now loaded! + case <-time.After(time.Duration(EngineStartupStatsTimeout) * time.Second): + obj.Logf("stats...") + obj.Logf("%s", obj.funcs.Stats()) + case <-ctx.Done(): + } + }() + } + select { case <-ctx.Done(): }