podterminal/pods/garbageCollector.go

49 lines
936 B
Go
Raw Normal View History

package pods
import (
2024-02-03 11:11:10 +01:00
"log"
"time"
2024-02-03 11:11:10 +01:00
"github.com/containers/podman/v4/pkg/bindings/volumes"
"github.com/spf13/viper"
)
2024-02-03 11:11:10 +01:00
// GarbageCollector is a goroutine that cleans up old Containers
func GarbageCollector() error {
2024-02-03 11:11:10 +01:00
if viper.GetBool("timeout_on_restart") {
timeoutExistingContainers()
}
for {
err := Cleanup()
if err != nil {
return err
}
time.Sleep(time.Minute * 10)
}
}
2024-02-03 11:11:10 +01:00
func timeoutExistingContainers() {
var oldContainers []string
for _, container := range containerList() {
oldContainers = append(oldContainers, container.ID)
}
OldContainers = append(OldContainers, oldContainers...)
log.Println("old Containers: ", oldContainers)
}
func pruneVolumes() error {
results, err := volumes.Prune(Socket, nil)
if err != nil {
return err
}
resultLen := len(results)
for i, result := range results {
log.Printf("[%d/%d] %s %d MB", i, resultLen, result.Id, result.Size/1024/1024)
}
return nil
}