Containerizing things.
ci/woodpecker/push/woodpecker Pipeline was successful
Details
ci/woodpecker/push/woodpecker Pipeline was successful
Details
This commit is contained in:
parent
557d326340
commit
ed73f3bf12
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
41
main.go
|
@ -57,14 +57,24 @@ func main() {
|
||||||
log.Fatalf("Error creating diffs directory: %v", err)
|
log.Fatalf("Error creating diffs directory: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ticker := time.NewTicker(1 * time.Minute)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
log.Println("Fetching and monitoring containers...")
|
log.Println("Fetching and monitoring containers...")
|
||||||
err := fetchAndMonitorContainers()
|
err := fetchAndMonitorContainers()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error in fetching and monitoring containers: %v", err)
|
log.Printf("Error in fetching and monitoring containers: %v", err)
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
time.Sleep(1 * time.Second)
|
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))
|
log.Printf("Fetched %d containers\n", len(containers))
|
||||||
|
|
||||||
for _, container := range containers {
|
|
||||||
mu.Lock()
|
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 {
|
if _, exists := monitoredContainers[container.ID]; !exists {
|
||||||
interval, err := time.ParseDuration(container.Interval)
|
interval, err := time.ParseDuration(container.Interval)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Invalid interval for container %s (%s): %v", container.Name, container.ID, err)
|
log.Printf("Invalid interval for container %s (%s): %v", container.Name, container.ID, err)
|
||||||
mu.Unlock()
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,10 +117,15 @@ func fetchAndMonitorContainers() error {
|
||||||
LastChecked: time.Now().Add(-interval),
|
LastChecked: time.Now().Add(-interval),
|
||||||
}
|
}
|
||||||
log.Printf("Started monitoring container %s (%s) with interval %s\n", container.Name, container.ID, container.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
|
var wg sync.WaitGroup
|
||||||
for _, monitoredContainer := range monitoredContainers {
|
for _, monitoredContainer := range monitoredContainers {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
|
@ -117,7 +149,6 @@ func fetchAndMonitorContainers() error {
|
||||||
}(monitoredContainer)
|
}(monitoredContainer)
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue