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.
This commit is contained in:
James Shubin
2024-01-26 19:09:36 -05:00
parent 7e1a4dea6c
commit 902f4c957a

View File

@@ -24,6 +24,7 @@ import (
"context" "context"
"fmt" "fmt"
"sync" "sync"
"time"
"github.com/purpleidea/mgmt/engine" "github.com/purpleidea/mgmt/engine"
"github.com/purpleidea/mgmt/engine/local" "github.com/purpleidea/mgmt/engine/local"
@@ -42,6 +43,14 @@ import (
"github.com/purpleidea/mgmt/util/errwrap" "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. // Lang is the main language lexer/parser object.
type Lang struct { type Lang struct {
Fs engine.Fs // connected fs where input dir or metadata exists 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 // wait for some activity
obj.Logf("stream...") 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 { select {
case <-ctx.Done(): case <-ctx.Done():
} }