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.
53
main.go
53
main.go
|
@ -57,14 +57,24 @@ func main() {
|
|||
log.Fatalf("Error creating diffs directory: %v", err)
|
||||
}
|
||||
|
||||
for {
|
||||
log.Println("Fetching and monitoring containers...")
|
||||
err := fetchAndMonitorContainers()
|
||||
if err != nil {
|
||||
log.Printf("Error in fetching and monitoring containers: %v", err)
|
||||
}
|
||||
ticker := time.NewTicker(1 * time.Minute)
|
||||
defer ticker.Stop()
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
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))
|
||||
|
||||
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 {
|
||||
mu.Lock()
|
||||
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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue