Skip to content

Commit

Permalink
fix: go rutines
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasbacsai committed Jun 20, 2024
1 parent 896295d commit 4e37709
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/sentinel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
20 changes: 15 additions & 5 deletions cmd/sentinel/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sentinel": {
"version": "0.0.10"
"version": "0.0.11"
}
}

0 comments on commit 4e37709

Please sign in to comment.