summaryrefslogtreecommitdiff
path: root/internal/app/app.go
blob: 85c348a35b92e41e0d56af1bfd47a224fd94c952 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package app

import (
	"context"
	"fmt"
	"os"
	"strings"
	"sync"
	"time"

	"codeberg.org/snonux/loadbars/internal/collector"
	"codeberg.org/snonux/loadbars/internal/config"
	"codeberg.org/snonux/loadbars/internal/display"
)

// Run starts the loadbars application: collectors and display.
// It blocks until the user quits (e.g. 'q' key).
func Run(cfg *config.Config) error {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	store := NewStore()

	var wg sync.WaitGroup
	for _, host := range cfg.Hosts {
		h := host
		wg.Add(1)
		go func() {
			defer wg.Done()
			runCollectorLoop(ctx, h, cfg, store)
		}()
	}

	err := display.Run(ctx, cfg, store)
	cancel()
	wg.Wait()
	return err
}

func runCollectorLoop(ctx context.Context, host string, cfg *config.Config, store *Store) {
	backoff := time.Second
	for {
		err := collector.Run(ctx, host, cfg, store)
		if err == nil || ctx.Err() != nil {
			return
		}
		fmt.Fprintf(os.Stderr, "!!! collector %s failed: %v\n", host, err)
		if !isRemoteHost(host) {
			return
		}
		timer := time.NewTimer(backoff)
		select {
		case <-ctx.Done():
			timer.Stop()
			return
		case <-timer.C:
		}
		if backoff < 30*time.Second {
			backoff *= 2
			if backoff > 30*time.Second {
				backoff = 30 * time.Second
			}
		}
	}
}

func isRemoteHost(host string) bool {
	host = strings.TrimSpace(host)
	if i := strings.Index(host, ":"); i >= 0 {
		host = strings.TrimSpace(host[:i])
	}
	return host != "localhost" && host != "127.0.0.1"
}