Skip to content

Commit

Permalink
feat(dkron-executor-shell): adjust the input parameters of CollectPro…
Browse files Browse the repository at this point in the history
…cessMetrics to make it more convenient to correct the exit code
  • Loading branch information
cobolbaby committed Nov 8, 2023
1 parent bbeaab2 commit 9e32d46
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
14 changes: 8 additions & 6 deletions builtin/bins/dkron-executor-shell/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"log"
"os/exec"
"time"

"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -51,20 +50,23 @@ var (
[]string{"job_name"})
)

func CollectProcessMetrics(jobname string, cmd *exec.Cmd, quit chan struct{}) {
func CollectProcessMetrics(jobname string, pid int, quit chan int) {
start := time.Now()

for {
select {
case <-quit:
case exitCode, ok := <-quit:
if !ok {
// log.Println("Exit code received and quit channel closed.")
return
}
cpuUsage.WithLabelValues(jobname).Set(0)
memUsage.WithLabelValues(jobname).Set(0)
jobExecutionTime.WithLabelValues(jobname).Set(0)
jobDoneCount.WithLabelValues(jobname).Inc()
jobExitCode.WithLabelValues(jobname).Set(float64(cmd.ProcessState.ExitCode()))
return
jobExitCode.WithLabelValues(jobname).Set(float64(exitCode))
default:
cpu, mem, err := GetTotalCPUMemUsage(cmd.Process.Pid)
cpu, mem, err := GetTotalCPUMemUsage(pid)
if err != nil {
log.Printf("Error getting pid statistics: %v", err)
return
Expand Down
5 changes: 3 additions & 2 deletions builtin/bins/dkron-executor-shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,12 @@ func (s *Shell) ExecuteImpl(args *dktypes.ExecuteRequest, cb dkplugin.StatusHelp
defer slowTimer.Stop()
}

quit := make(chan struct{})
quit := make(chan int)

go CollectProcessMetrics(args.JobName, cmd, quit)
go CollectProcessMetrics(args.JobName, cmd.Process.Pid, quit)

err = cmd.Wait()
quit <- cmd.ProcessState.ExitCode()
close(quit) // exit metric refresh goroutine after job is finished

if jobTimedOut {
Expand Down

0 comments on commit 9e32d46

Please sign in to comment.