Example #1
0
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
}
Example #2
0
					Eventually(logger).Should(Say("failed-setting-presence"))
					clock.WaitForWatcherAndIncrement(6 * time.Second)
					Eventually(logger).Should(Say("recreating-session"))
				})
			})

			Context("and the session is destroyed", func() {
				It("should recreate the session and continue to retry", func() {
					var err error
					presenceProcess = ifrit.Background(presenceRunner)
					Consistently(presenceProcess.Ready()).ShouldNot(BeClosed())

					var sessions []*api.SessionEntry
					Eventually(func() int {
						sessions, _, err = consulClient.Session().List(nil)
						Expect(err).NotTo(HaveOccurred())
						return len(sessions)
					}).Should(Equal(2))

					sessions, _, err = consulClient.Session().List(nil)
					Expect(err).NotTo(HaveOccurred())
					var mostRecentSession *api.SessionEntry
					for _, session := range sessions {
						if mostRecentSession == nil {
							mostRecentSession = session
						} else if session.CreateIndex > mostRecentSession.CreateIndex {
							mostRecentSession = session
						}
					}
		logger = lagertest.NewTestLogger("test")
		clock := clock.NewClock()
		watcherRunner, disappearChan = locket.NewDisappearanceWatcher(logger, consulClient, "under", clock)
		watcherProcess = ifrit.Invoke(watcherRunner)
	})

	AfterEach(func() {
		ginkgomon.Kill(watcherProcess)
	})

	var addThenRemovePresence = func(presenceKey string) {
		presenceRunner := locket.NewPresence(logger, consulClient, presenceKey, []byte("value"), clock.NewClock(), retryInterval, 10*time.Second)

		presenceProcess := ifrit.Invoke(presenceRunner)
		Eventually(func() int {
			sessions, _, err := consulClient.Session().List(nil)
			Expect(err).NotTo(HaveOccurred())
			return len(sessions)
		}).Should(Equal(1))
		Eventually(logger).Should(gbytes.Say("presence.succeeded-setting-presence"))

		ginkgomon.Kill(presenceProcess)
	}

	Context("when the watch starts first", func() {
		Context("when there are keys", func() {
			It("detects removals of keys", func() {
				addThenRemovePresence("under/here")

				Eventually(disappearChan, 10*time.Second).Should(Receive(Equal([]string{"under/here"})))
			})