From f7b5e1a83ece8a4f7b527179f1e8a41fff5917c0 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Thu, 30 Jan 2025 01:15:15 -0600 Subject: [PATCH] try a way to track the times so they can be throttled --- config.go | 7 +++++++ init.go | 18 ++++++++++++++++-- structs.go | 33 ++++++++++++++++++++++++--------- 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/config.go b/config.go index 3970f8c..a1fc49d 100644 --- a/config.go +++ b/config.go @@ -8,8 +8,10 @@ import ( "fmt" "os" "path/filepath" + "time" "go.wit.com/log" + timestamppb "google.golang.org/protobuf/types/known/timestamppb" ) func (f *Forge) ConfigSave() error { @@ -28,6 +30,11 @@ func (f *Forge) ConfigSave() error { } } if f.Repos != nil { + if f.HasFullScan() { + f.Repos.HasFullScan = true + t := time.Now() + f.Repos.FullScan = timestamppb.New(t) + } if e := f.Repos.ConfigSave(); e != nil { log.Info("forge.Repos.ConfigSave() error", e) err = e diff --git a/init.go b/init.go index 8d3342f..bb98f40 100644 --- a/init.go +++ b/init.go @@ -40,18 +40,29 @@ func Init() *Forge { f.Machine.InitWit() + if f.hasFullScan { + // duplicate time checking below. which one to keep? + if f.FullScanAge() > time.Minute { + log.Log(INFO, "forgepb.Scan() skipping scan. been run a minute ago", f.FullScanAge()) + return f + } + } + now := time.Now() start := f.Repos.Len() f.ScanGoSrc() + f.FullScanRan() end := f.Repos.Len() if (end - start) == 0 { log.Log(INFO, "forgepb.Scan() Scan did not find new git repositories. Total =", end) + if f.FullScanAge() > time.Minute { + f.rillUpdate(20, 10) + } } else { log.Log(INFO, "forgepb.Scan() Scan found", end-start, "new git repositories. Total =", end) + f.rillUpdate(20, 10) } - f.rillUpdate(20, 10) - if f.configSave { // taking this out to debug Marshal() panic // os.Exit(-1) @@ -108,6 +119,9 @@ func (f *Forge) InitPB() { } f.Repos = gitpb.NewRepos() f.Repos.ConfigLoad() + if f.Repos.HasFullScan { + f.hasFullScan = true + } } // only init's the protobuf. intended to not scan or change anything diff --git a/structs.go b/structs.go index 90b84bc..ac10293 100644 --- a/structs.go +++ b/structs.go @@ -2,6 +2,7 @@ package forgepb import ( sync "sync" + "time" "go.wit.com/lib/protobuf/gitpb" "go.wit.com/lib/protobuf/zoopb" @@ -10,15 +11,16 @@ import ( // maybe an interface someday? type Forge struct { // one-time initialized data - initOnce sync.Once - initErr error // init error, if any - - goSrc string // the path to go/src - goWork bool // means the user is currently using a go.work file - Config *ForgeConfigs // config repos for readonly, private, etc - Repos *gitpb.Repos - Machine *zoopb.Machine - configSave bool + initOnce sync.Once + initErr error // init error, if any + goSrc string // the path to go/src + goWork bool // means the user is currently using a go.work file + Config *ForgeConfigs // config repos for readonly, private, etc + Repos *gitpb.Repos // the repo protobufs + Machine *zoopb.Machine // things for virtigo to track vm's + configSave bool // if you need to save the config because things changed + hasFullScan bool // track last scan so it can be throttled + fullscan time.Time // time of the last scan so it can be throttled } func (f *Forge) GetGoSrc() string { @@ -28,3 +30,16 @@ func (f *Forge) GetGoSrc() string { func (f *Forge) IsGoWork() bool { return f.goWork } + +func (f *Forge) HasFullScan() bool { + return f.Repos.HasFullScan +} + +func (f *Forge) FullScanRan() { + f.fullscan = time.Now() +} + +func (f *Forge) FullScanAge() time.Duration { + fs := f.Repos.FullScan.AsTime() + return time.Since(fs) +}