From 12299885355aaf4829a8570581f8da4267e1491a Mon Sep 17 00:00:00 2001 From: wsfuyibing Date: Mon, 16 Dec 2024 17:58:12 +0800 Subject: [PATCH] support counter --- go.mod | 2 +- go.sum | 4 ++-- src/process.go | 24 +++++++++++++----------- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index 7e79504..64aab56 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,6 @@ module gitee.com/go-libs/process go 1.18 require ( - gitee.com/go-libs/runtime v1.0.2 + gitee.com/go-libs/runtime v1.0.7 github.com/google/uuid v1.6.0 ) diff --git a/go.sum b/go.sum index fe900fa..e5a6ec4 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,4 @@ -gitee.com/go-libs/runtime v1.0.2 h1:P/aGA9RDoNJ2XoLTYf+WnVvXuZEuBy9nM+IitV6LTnE= -gitee.com/go-libs/runtime v1.0.2/go.mod h1:0VkttMm+poNBbk8C37VphnUOkteJYaRUzj84RSproKI= +gitee.com/go-libs/runtime v1.0.7 h1:XWP0BNkQBZV+XySGZr+SrU72KP5dbpiLlTawdfG7Y4U= +gitee.com/go-libs/runtime v1.0.7/go.mod h1:0VkttMm+poNBbk8C37VphnUOkteJYaRUzj84RSproKI= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= diff --git a/src/process.go b/src/process.go index 65487a1..9e99317 100644 --- a/src/process.go +++ b/src/process.go @@ -78,10 +78,11 @@ type ( children map[string]Process mu *sync.RWMutex - cancel context.CancelFunc - ctx context.Context - retryCount int32 - started, quiter, prevented bool + cancel context.CancelFunc + ctx context.Context + retryCount int32 + started, quit, prevented bool + startedTime time.Time } ) @@ -205,7 +206,7 @@ func (o *process) Restart() { defer o.mu.Unlock() if o.ctx != nil && o.ctx.Err() == nil { - o.quiter = false + o.quit = false o.cancel() } } @@ -234,9 +235,10 @@ func (o *process) Start(ctx context.Context) (err error) { } // Update process state to avoid concurrency operations. - o.started, o.quiter = true, false + o.started, o.quit = true, false + o.startedTime = time.Now() o.mu.Unlock() - runtime.GetCounter().ProcessCounter().Add(o.uid, time.Now()) + runtime.GetCounter().ProcessCounter().Add(o.uid, o.name, o.startedTime) // Clean state when stopped. defer func() { @@ -246,7 +248,7 @@ func (o *process) Start(ctx context.Context) (err error) { o.mu.Lock() parent, prevented := o.parent, o.prevented o.ctx, o.cancel, o.parent = nil, nil, nil - o.started, o.quiter = false, true + o.started, o.quit = false, true o.mu.Unlock() // Notify the parent process to delete the current process. @@ -263,14 +265,14 @@ func (o *process) Start(ctx context.Context) (err error) { } // Return for stop signal. - if o.quiter { + if o.quit { return } // Build process context. o.mu.Lock() o.ctx, o.cancel = context.WithCancel(ctx) - o.quiter = true + o.quit = true o.mu.Unlock() // Invoke before hook of the provider, Return if error returned. @@ -316,7 +318,7 @@ func (o *process) Stop() { defer o.mu.Unlock() if o.ctx != nil && o.ctx.Err() == nil { - o.quiter = true + o.quit = true o.cancel() } } -- Gitee