-
Notifications
You must be signed in to change notification settings - Fork 1
/
watchservices.go
46 lines (38 loc) · 885 Bytes
/
watchservices.go
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
package caddyconsul
import (
"fmt"
"time"
"github.com/hashicorp/consul/api"
)
type service struct {
Name string
Instances []*api.CatalogService
}
func (s *caddyfile) WatchServices(reload bool) {
opts := api.QueryOptions{
WaitIndex: s.lastService,
WaitTime: 5 * time.Minute,
}
if !reload {
opts.WaitTime = time.Second
}
fmt.Println("Watching for new service with index", s.lastService, "or better")
services, meta, err := catalog.Services(&opts)
if err != nil {
return
}
if meta.LastIndex > s.lastService {
s.lastService = meta.LastIndex
}
myservices := make(map[string][]*api.CatalogService)
for servicename := range services {
// Get all instances for this service
instances, _, _ := catalog.Service(servicename, "", nil)
myservices[servicename] = instances
}
s.services = myservices
s.buildConfig()
if reload {
reloadCaddy()
}
}