Пример #1
0
func (listener *ActualStateListener) Start() {
	listener.messageBus.Subscribe("dea.heartbeat", func(messageBody []byte) {
		heartbeat, err := models.NewHeartbeatFromJSON(messageBody)
		if err != nil {
			listener.logger.Info("Could not unmarshal heartbeat",
				map[string]string{
					"Error":       err.Error(),
					"MessageBody": string(messageBody),
				})
			return
		}

		err = listener.store.SaveActualState(heartbeat.InstanceHeartbeats)
		if err != nil {
			listener.logger.Info("Could not put instance heartbeats in store:",
				map[string]string{
					"Error": err.Error(),
				})
			return
		}

		err = listener.store.BumpActualFreshness(listener.timeProvider.Time())
		if err != nil {
			listener.logger.Info("Could not update actual freshness",
				map[string]string{
					"Error": err.Error(),
				})
		}
	})
}
func (listener *ActualStateListener) Start() {
	heartbeatThreshold := time.Duration(listener.config.ActualFreshnessTTL()) * time.Second

	listener.messageBus.Subscribe("dea.advertise", func(message *yagnats.Message) {
		listener.heartbeatMutex.Lock()
		lastReceived := listener.lastReceivedHeartbeat
		listener.heartbeatMutex.Unlock()

		if listener.timeProvider.Time().Sub(lastReceived) >= heartbeatThreshold {
			listener.bumpFreshness()
		}

		listener.logger.Debug("Received dea.advertise")
	})

	listener.messageBus.Subscribe("dea.heartbeat", func(message *yagnats.Message) {
		listener.logger.Debug("Got a heartbeat")
		heartbeat, err := models.NewHeartbeatFromJSON(message.Payload)
		if err != nil {
			listener.logger.Error("Could not unmarshal heartbeat", err,
				map[string]string{
					"MessageBody": string(message.Payload),
				})
			return
		}

		listener.logger.Debug("Decoded the heartbeat")

		listener.heartbeatMutex.Lock()

		listener.lastReceivedHeartbeat = listener.timeProvider.Time()

		listener.totalReceivedHeartbeats++
		listener.heartbeatsToSave = append(listener.heartbeatsToSave, heartbeat)

		totalReceivedHeartbeats := listener.totalReceivedHeartbeats
		numToSave := len(listener.heartbeatsToSave)

		listener.heartbeatMutex.Unlock()

		listener.logger.Info("Received a heartbeat", map[string]string{
			"Heartbeats Pending Save": strconv.Itoa(numToSave),
		})

		listener.metricsAccountant.TrackReceivedHeartbeats(totalReceivedHeartbeats)
	})

	go listener.syncHeartbeats()

	if listener.storeUsageTracker != nil {
		listener.storeUsageTracker.StartTrackingUsage()
		listener.measureStoreUsage()
	}
}
	Describe("Ticking the simulator", func() {
		BeforeEach(func() {
			sim.NumberOfNewAppsStartingPerHB = 0 //turn this off to make these tests simpler
		})

		It("should emit a heartbeat for each DEA by every heartbeat interval", func() {
			Ω(sim.HeartbeatIntervalInSeconds).Should(Equal(10))
			for i := 0; i < sim.HeartbeatIntervalInSeconds; i++ {
				sim.TickOneSecond()
			}
			Ω(messageBus.PublishedMessages["dea.heartbeat"]).Should(HaveLen(8))

			heartbeats := map[string]models.Heartbeat{}
			for _, message := range messageBus.PublishedMessages["dea.heartbeat"] {
				heartbeat, err := models.NewHeartbeatFromJSON(message.Payload)
				Ω(err).ShouldNot(HaveOccured())
				heartbeats[heartbeat.DeaGuid] = heartbeat
			}

			for dea, apps := range sim.DEAs {
				Ω(heartbeats[dea].InstanceHeartbeats).Should(HaveLen(len(apps)))
			}
		})

		It("should emit an advertise for each DEA by every advertise interval", func() {
			Ω(sim.AdvertiseIntervalInSeconds).Should(Equal(5))
			for i := 0; i < sim.AdvertiseIntervalInSeconds; i++ {
				sim.TickOneSecond()
			}
			Ω(messageBus.PublishedMessages["dea.advertise"]).Should(HaveLen(8))