diff --git a/api/runner/runner.go b/api/runner/runner.go index 465329771..5ef5c50aa 100644 --- a/api/runner/runner.go +++ b/api/runner/runner.go @@ -30,6 +30,8 @@ type Runner struct { availableMem int64 usedMem int64 usedMemMutex sync.RWMutex + + stats } var ( diff --git a/api/runner/stats.go b/api/runner/stats.go new file mode 100644 index 000000000..5c9a4351f --- /dev/null +++ b/api/runner/stats.go @@ -0,0 +1,46 @@ +package runner + +import "sync" + +type stats struct { + mu sync.Mutex + queue uint64 + running uint64 + complete uint64 +} + +type Stats struct { + Queue uint64 + Running uint64 + Complete uint64 +} + +func (s *stats) Enqueue() { + s.mu.Lock() + s.queue++ + s.mu.Unlock() +} + +func (s *stats) Start() { + s.mu.Lock() + s.queue-- + s.running++ + s.mu.Unlock() +} + +func (s *stats) Complete() { + s.mu.Lock() + s.running-- + s.complete++ + s.mu.Unlock() +} + +func (s *stats) Stats() Stats { + var stats Stats + s.mu.Lock() + stats.Running = s.running + stats.Complete = s.complete + stats.Queue = s.queue + s.mu.Unlock() + return stats +} diff --git a/api/runner/worker.go b/api/runner/worker.go index e97db5461..1a151c6da 100644 --- a/api/runner/worker.go +++ b/api/runner/worker.go @@ -96,10 +96,12 @@ func StartWorkers(ctx context.Context, rnr *Runner, tasks <-chan task.Request) { continue } + rnr.Start() select { case <-ctx.Done(): return case p <- task: + rnr.Complete() } } } @@ -344,6 +346,8 @@ func (hc *htfn) serve(ctx context.Context) { func runTaskReq(rnr *Runner, wg *sync.WaitGroup, t task.Request) { defer wg.Done() + rnr.Start() + defer rnr.Complete() result, err := rnr.Run(t.Ctx, t.Config) select { case t.Response <- task.Response{result, err}: diff --git a/api/server/runner.go b/api/server/runner.go index 2622c8656..44c99b478 100644 --- a/api/server/runner.go +++ b/api/server/runner.go @@ -180,6 +180,7 @@ func (s *Server) serve(ctx context.Context, c *gin.Context, appName string, foun Timeout: time.Duration(found.Timeout) * time.Second, } + s.Runner.Enqueue() switch found.Type { case "async": // Read payload diff --git a/api/server/server.go b/api/server/server.go index e261fbdf9..0627f1e78 100644 --- a/api/server/server.go +++ b/api/server/server.go @@ -133,6 +133,7 @@ func (s *Server) bindHandlers() { engine.GET("/", handlePing) engine.GET("/version", handleVersion) + engine.GET("/stats", s.handleStats) v1 := engine.Group("/v1") { diff --git a/api/server/stats.go b/api/server/stats.go new file mode 100644 index 000000000..06f0fb0d7 --- /dev/null +++ b/api/server/stats.go @@ -0,0 +1,11 @@ +package server + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +func (s *Server) handleStats(c *gin.Context) { + c.JSON(http.StatusOK, s.Runner.Stats()) +}