Containerizing things.
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Colin 2024-06-12 17:29:19 -04:00
parent 557d326340
commit ed73f3bf12
8 changed files with 42 additions and 11 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

41
main.go
View File

@ -57,14 +57,24 @@ func main() {
log.Fatalf("Error creating diffs directory: %v", err)
}
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()
for {
select {
case <-ticker.C:
log.Println("Fetching and monitoring containers...")
err := fetchAndMonitorContainers()
if err != nil {
log.Printf("Error in fetching and monitoring containers: %v", err)
}
default:
time.Sleep(1 * time.Second)
err := monitorContainers()
if err != nil {
log.Printf("Error in monitoring containers: %v", err)
}
}
}
}
@ -75,13 +85,30 @@ func fetchAndMonitorContainers() error {
}
log.Printf("Fetched %d containers\n", len(containers))
for _, container := range containers {
mu.Lock()
defer mu.Unlock()
// Remove containers that are no longer present
for id := range monitoredContainers {
found := false
for _, container := range containers {
if container.ID == id {
found = true
break
}
}
if !found {
delete(monitoredContainers, id)
log.Printf("Stopped monitoring container %s\n", id)
}
}
// Add or update containers
for _, container := range containers {
if _, exists := monitoredContainers[container.ID]; !exists {
interval, err := time.ParseDuration(container.Interval)
if err != nil {
log.Printf("Invalid interval for container %s (%s): %v", container.Name, container.ID, err)
mu.Unlock()
continue
}
@ -90,10 +117,15 @@ func fetchAndMonitorContainers() error {
LastChecked: time.Now().Add(-interval),
}
log.Printf("Started monitoring container %s (%s) with interval %s\n", container.Name, container.ID, container.Interval)
} else {
monitoredContainers[container.ID].Info = container
}
mu.Unlock()
}
return nil
}
func monitorContainers() error {
var wg sync.WaitGroup
for _, monitoredContainer := range monitoredContainers {
wg.Add(1)
@ -117,7 +149,6 @@ func fetchAndMonitorContainers() error {
}(monitoredContainer)
}
wg.Wait()
return nil
}