func NewEventFromBBS(bbsEvent models.Event) (receptor.Event, error) {
	switch bbsEvent := bbsEvent.(type) {
	case *models.ActualLRPCreatedEvent:
		actualLRP, evacuating := bbsEvent.ActualLrpGroup.Resolve()
		return receptor.NewActualLRPCreatedEvent(serialization.ActualLRPProtoToResponse(actualLRP, evacuating)), nil
	case *models.ActualLRPChangedEvent:
		before, evacuating := bbsEvent.Before.Resolve()
		after, evacuating := bbsEvent.After.Resolve()
		return receptor.NewActualLRPChangedEvent(
			serialization.ActualLRPProtoToResponse(before, evacuating),
			serialization.ActualLRPProtoToResponse(after, evacuating),
		), nil
	case *models.ActualLRPRemovedEvent:
		actualLRP, evacuating := bbsEvent.ActualLrpGroup.Resolve()
		return receptor.NewActualLRPRemovedEvent(serialization.ActualLRPProtoToResponse(actualLRP, evacuating)), nil
	case *models.DesiredLRPCreatedEvent:
		return receptor.NewDesiredLRPCreatedEvent(serialization.DesiredLRPProtoToResponse(bbsEvent.DesiredLrp)), nil
	case *models.DesiredLRPChangedEvent:
		return receptor.NewDesiredLRPChangedEvent(
			serialization.DesiredLRPProtoToResponse(bbsEvent.Before),
			serialization.DesiredLRPProtoToResponse(bbsEvent.After),
		), nil
	case *models.DesiredLRPRemovedEvent:
		return receptor.NewDesiredLRPRemovedEvent(serialization.DesiredLRPProtoToResponse(bbsEvent.DesiredLrp)), nil
	}
	return nil, fmt.Errorf("unknown event type: %#v", bbsEvent)
}
Esempio n. 2
0
func (w *watcher) watchDesired(logger lager.Logger) (chan<- bool, <-chan error) {
	return w.bbs.WatchForDesiredLRPChanges(logger,
		func(created models.DesiredLRP) {
			logger.Debug("handling-desired-create")
			w.hub.Emit(receptor.NewDesiredLRPCreatedEvent(serialization.DesiredLRPToResponse(created)))
		},
		func(changed models.DesiredLRPChange) {
			logger.Debug("handling-desired-change")
			w.hub.Emit(receptor.NewDesiredLRPChangedEvent(
				serialization.DesiredLRPToResponse(changed.Before),
				serialization.DesiredLRPToResponse(changed.After),
			))
		},
		func(deleted models.DesiredLRP) {
			logger.Debug("handling-desired-delete")
			w.hub.Emit(receptor.NewDesiredLRPRemovedEvent(serialization.DesiredLRPToResponse(deleted)))
		})
}
						ProcessGuid: "some-guid",
						Domain:      "some-domain",
						RootFs:      "some-rootfs",
						Action: models.WrapAction(&models.RunAction{
							Path: "true",
							User: "******",
						}),
					},
				)
			})

			Context("when receiving a DesiredLRPCreatedEvent", func() {
				var expectedEvent receptor.DesiredLRPCreatedEvent

				BeforeEach(func() {
					expectedEvent = receptor.NewDesiredLRPCreatedEvent(desiredLRPResponse)
					payload, err := json.Marshal(expectedEvent)
					Expect(err).NotTo(HaveOccurred())

					fakeRawEventSource.NextReturns(
						sse.Event{
							ID:   "hi",
							Name: string(expectedEvent.EventType()),
							Data: payload,
						},
						nil,
					)
				})

				It("returns the event", func() {
					event, err := eventSource.Next()
				response := &http.Response{}
				Eventually(responseChan).Should(Receive(&response))
				reader := sse.NewReadCloser(response.Body)

				desiredLRP := &models.DesiredLRP{
					ProcessGuid: "some-guid",
					Domain:      "some-domain",
					RootFs:      "some-rootfs",
					Action: models.WrapAction(&models.RunAction{
						Path: "true",
						User: "******",
					}),
				}
				eventChannel <- models.NewDesiredLRPCreatedEvent(desiredLRP)

				data, err := json.Marshal(receptor.NewDesiredLRPCreatedEvent(serialization.DesiredLRPProtoToResponse(desiredLRP)))
				Expect(err).NotTo(HaveOccurred())

				event, err := reader.Next()
				Expect(err).NotTo(HaveOccurred())
				Expect(event.ID).To(Equal("0"))
				Expect(event.Name).To(Equal(string(receptor.EventTypeDesiredLRPCreated)))
				Expect(event.Data).To(MatchJSON(data))

				actualLRP := models.NewUnclaimedActualLRP(models.NewActualLRPKey("some-guid", 3, "some-domain"), 0)
				actualLRPGroup := &models.ActualLRPGroup{Instance: actualLRP}
				eventChannel <- models.NewActualLRPCreatedEvent(actualLRPGroup)

				data, err = json.Marshal(receptor.NewActualLRPCreatedEvent(serialization.ActualLRPProtoToResponse(actualLRP, false)))
				Expect(err).NotTo(HaveOccurred())