summaryrefslogtreecommitdiff
path: root/internal/dependency.go
blob: 4e7b56a2d41d59acbf671235bad56272c3f1ef36 (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
package internal

import (
	"context"
	"fmt"
)

type dependency struct {
	okMap  map[string]chan struct{}
	nokMap map[string]chan struct{}
}

func newDependency(conf config) dependency {
	d := dependency{
		okMap:  make(map[string]chan struct{}, len(conf.Checks)),
		nokMap: make(map[string]chan struct{}, len(conf.Checks)),
	}

	for name := range conf.Checks {
		d.okMap[name] = make(chan struct{})
		d.nokMap[name] = make(chan struct{})
	}

	return d
}

func (d dependency) ok(name string) {
	close(d.okMap[name])
}

func (d dependency) notOk(name string) {
	close(d.nokMap[name])
}

// Wait for all dependant checks to be executed!
func (d dependency) wait(ctx context.Context, dependencies []string) error {
	for _, dep := range dependencies {
		if _, ok := d.okMap[dep]; !ok {
			// We sent an error mail already via config.sanityCheck for this case.
			continue
		}
		select {
		case <-d.okMap[dep]:
		case <-d.nokMap[dep]:
			return fmt.Errorf("dependency '%s' is not OK!", dep)
		case <-ctx.Done():
			return fmt.Errorf("waited for too long for dependency '%s': %s", dep, ctx.Err().Error())
		}
	}
	return nil
}