diff --git a/cmd/sentinel/main.go b/cmd/sentinel/main.go index 26bfd0a..97ed4ba 100644 --- a/cmd/sentinel/main.go +++ b/cmd/sentinel/main.go @@ -11,7 +11,7 @@ import ( "github.com/gin-gonic/gin" ) -var version string = "0.0.10" +var version string = "0.0.11" var logsDir string = "/app/logs" var metricsDir string = "/app/metrics" var cpuMetricsFile string = metricsDir + "/cpu.csv" diff --git a/cmd/sentinel/scheduler.go b/cmd/sentinel/scheduler.go index f98e51f..3b29e3a 100644 --- a/cmd/sentinel/scheduler.go +++ b/cmd/sentinel/scheduler.go @@ -163,34 +163,44 @@ func containerMetrics() { continue } go func(cont types.Container) { + defer func() { + if r := recover(); r != nil { + fmt.Printf("Recovered from panic: %v\n", r) + } + }() + metrics, err := getOneContainerMetrics(container.ID, true) if err != nil { - fmt.Printf("Error getting container metrics: %s", err) + fmt.Printf("Error getting container metrics: %s\n", err) return } + containerNameFromLabel := container.Labels["coolify.name"] if containerNameFromLabel == "" { containerNameFromLabel = container.Names[0][1:] } containerName := "container-" + containerNameFromLabel containerMetricsFile := fmt.Sprintf("%s/%s.csv", metricsDir, containerName) + _, err = os.Stat(containerMetricsFile) - if err != nil { + if err != nil && os.IsNotExist(err) { err := os.WriteFile(containerMetricsFile, []byte(containerMetricsCsvHeader), 0644) if err != nil { - fmt.Printf("Error writing file: %s", err) + fmt.Printf("Error writing file: %s\n", err) return } } + f, err := os.OpenFile(containerMetricsFile, os.O_APPEND|os.O_WRONLY, 0644) if err != nil { - fmt.Printf("Error opening file: %s", err) + fmt.Printf("Error opening file: %s\n", err) return } defer f.Close() + _, err = f.WriteString(metrics) if err != nil { - fmt.Printf("Error writing to file: %s", err) + fmt.Printf("Error writing to file: %s\n", err) return } }(container) diff --git a/versions.json b/versions.json index 0169429..efe3435 100644 --- a/versions.json +++ b/versions.json @@ -1,5 +1,5 @@ { "sentinel": { - "version": "0.0.10" + "version": "0.0.11" } } \ No newline at end of file