func WatchForDisappearancesUnder(logger lager.Logger, client consuladapter.Client, disappearanceChan chan []string, stop <-chan struct{}, prefix string) { logger = logger.Session("watch-for-disappearances") go func() { logger.Info("starting") defer logger.Info("finished") keys := keySet{} queryOpts := &api.QueryOptions{ WaitIndex: 0, WaitTime: defaultWatchBlockDuration, } for { newPairs, queryMeta, err := client.KV().List(prefix, queryOpts) if err != nil { logger.Error("list-failed", err) select { case <-stop: return case <-time.After(1 * time.Second): } queryOpts.WaitIndex = 0 continue } select { case <-stop: return default: } queryOpts.WaitIndex = queryMeta.LastIndex if newPairs == nil { // key not found _, err = client.KV().Put(&api.KVPair{Key: prefix, Value: emptyBytes}, nil) if err != nil { logger.Error("put-failed", err) continue } } newKeys := newKeySet(newPairs) if missing := difference(keys, newKeys); len(missing) > 0 { select { case disappearanceChan <- missing: case <-stop: return } } keys = newKeys } }() }
func create(se *api.SessionEntry, noChecks bool, client consuladapter.Client) (string, string, error) { session := client.Session() agent := client.Agent() nodeName, err := agent.NodeName() if err != nil { return "", "", err } nodeSessions, _, err := session.Node(nodeName, nil) if err != nil { return "", "", err } sessions := findSessions(se.Name, nodeSessions) if sessions != nil { for _, s := range sessions { _, err = session.Destroy(s.ID, nil) if err != nil { return "", "", err } } } var f func(*api.SessionEntry, *api.WriteOptions) (string, *api.WriteMeta, error) if noChecks { f = session.CreateNoChecks } else { f = session.Create } id, _, err := f(se, nil) if err != nil { return "", "", err } return id, se.TTL, nil }
"github.com/tedsuo/ifrit/ginkgomon" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("Service Registration Integration", func() { const ( serviceID = "test-id" serviceName = "Test-Service" ) var ( consulClient consuladapter.Client logger lager.Logger clock *fakeclock.FakeClock registration *api.AgentServiceRegistration registrationProcess ifrit.Process ) BeforeEach(func() { consulClient = consulRunner.NewClient() logger = lagertest.NewTestLogger("test") clock = fakeclock.NewFakeClock(time.Now()) registration = &api.AgentServiceRegistration{ ID: serviceID, Name: serviceName, Tags: []string{"a", "b", "c"}, Port: 8080, Address: "127.0.0.1",
"code.cloudfoundry.org/lager/lagertest" "github.com/tedsuo/ifrit" "github.com/tedsuo/ifrit/ginkgomon" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" . "github.com/onsi/gomega/gbytes" ) var _ = Describe("Presence", func() { var ( presenceKey string presenceValue []byte consulClient consuladapter.Client presenceRunner ifrit.Runner presenceProcess ifrit.Process retryInterval, presenceTTL time.Duration logger lager.Logger clock *fakeclock.FakeClock ) getPresenceValue := func() ([]byte, error) { kvPair, _, err := consulClient.KV().Get(presenceKey, nil) if err != nil { return nil, err } if kvPair == nil || kvPair.Session == "" { return nil, consuladapter.NewKeyNotFoundError(presenceKey) }
"github.com/tedsuo/ifrit" "github.com/tedsuo/ifrit/ginkgomon" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/onsi/gomega/gbytes" ) var _ = Describe("Disappearance Watcher", func() { const retryInterval = 500 * time.Millisecond var ( consulClient consuladapter.Client watcherRunner ifrit.Runner watcherProcess ifrit.Process disappearChan <-chan []string logger *lagertest.TestLogger ) BeforeEach(func() { consulClient = consulRunner.NewClient() logger = lagertest.NewTestLogger("test") clock := clock.NewClock() watcherRunner, disappearChan = locket.NewDisappearanceWatcher(logger, consulClient, "under", clock) watcherProcess = ifrit.Invoke(watcherRunner) }) AfterEach(func() { ginkgomon.Kill(watcherProcess)