예제 #1
0
func NewStartStopListener(messageBus yagnats.NATSConn, conf *config.Config) *StartStopListener {
	listener := &StartStopListener{
		messageBus: messageBus,
	}

	messageBus.Subscribe(conf.SenderNatsStartSubject, func(message *nats.Msg) {
		startMessage, err := models.NewStartMessageFromJSON([]byte(message.Data))
		if err != nil {
			panic(err)
		}
		listener.mutex.Lock()
		listener.starts = append(listener.starts, startMessage)
		listener.mutex.Unlock()
	})

	messageBus.Subscribe(conf.SenderNatsStopSubject, func(message *nats.Msg) {
		stopMessage, err := models.NewStopMessageFromJSON([]byte(message.Data))
		if err != nil {
			panic(err)
		}
		listener.mutex.Lock()
		listener.stops = append(listener.stops, stopMessage)
		listener.mutex.Unlock()
	})

	return listener
}
예제 #2
0
func NewStartStopListener(messageBus yagnats.NATSClient, conf *config.Config) *StartStopListener {
	listener := &StartStopListener{
		messageBus: messageBus,
	}

	messageBus.Subscribe(conf.SenderNatsStartSubject, func(message *yagnats.Message) {
		startMessage, err := models.NewStartMessageFromJSON([]byte(message.Payload))
		if err != nil {
			panic(err)
		}
		listener.Starts = append(listener.Starts, startMessage)
	})

	messageBus.Subscribe(conf.SenderNatsStopSubject, func(message *yagnats.Message) {
		stopMessage, err := models.NewStopMessageFromJSON([]byte(message.Payload))
		if err != nil {
			panic(err)
		}
		listener.Stops = append(listener.Stops, stopMessage)
	})

	return listener
}
예제 #3
0
				Ω(metricsAccountant.IncrementedStops).Should(BeEmpty())
			})
		})

		Context("and it is time to send the message", func() {
			BeforeEach(func() {
				timeProvider.TimeToProvide = time.Unix(130, 0)
			})

			It("should not error", func() {
				Ω(err).ShouldNot(HaveOccurred())
			})

			It("should send the message", func() {
				Ω(messageBus.PublishedMessages["hm9000.stop"]).Should(HaveLen(1))
				message, _ := models.NewStopMessageFromJSON([]byte(messageBus.PublishedMessages["hm9000.stop"][0].Payload))
				Ω(message).Should(Equal(models.StopMessage{
					AppGuid:       app.AppGuid,
					AppVersion:    app.AppVersion,
					InstanceIndex: 0,
					InstanceGuid:  app.InstanceAtIndex(0).InstanceGuid,
					IsDuplicate:   false,
					MessageId:     pendingMessage.MessageId,
				}))
			})

			It("should increment the metrics", func() {
				Ω(metricsAccountant.IncrementedStops).Should(ContainElement(pendingMessage))
			})

			Context("when the message should be kept alive", func() {
예제 #4
0
				Ω(metricsAccountant.IncrementedStops).Should(BeEmpty())
			})
		})

		Context("and it is time to send the message", func() {
			BeforeEach(func() {
				timeProvider.TimeToProvide = time.Unix(130, 0)
			})

			It("should not error", func() {
				Ω(err).ShouldNot(HaveOccurred())
			})

			It("should send the message", func() {
				Ω(messageBus.PublishedMessages("hm9000.stop")).Should(HaveLen(1))
				message, _ := models.NewStopMessageFromJSON([]byte(messageBus.PublishedMessages("hm9000.stop")[0].Data))
				Ω(message).Should(Equal(models.StopMessage{
					AppGuid:       app.AppGuid,
					AppVersion:    app.AppVersion,
					InstanceIndex: 0,
					InstanceGuid:  app.InstanceAtIndex(0).InstanceGuid,
					IsDuplicate:   false,
					MessageId:     pendingMessage.MessageId,
				}))
			})

			It("should increment the metrics", func() {
				Ω(metricsAccountant.IncrementedStops).Should(ContainElement(pendingMessage))
			})

			Context("when the message should be kept alive", func() {