|
| 1 | +package rabbit |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "time" |
| 8 | + |
| 9 | + log "github.com/Sirupsen/logrus" |
| 10 | + "github.com/hellofresh/goengine" |
| 11 | + "github.com/hellofresh/goengine/reflection" |
| 12 | + "github.com/streadway/amqp" |
| 13 | +) |
| 14 | + |
| 15 | +// RawVersionedEvent represents the event that goes into rabbitmq |
| 16 | +type RawVersionedEvent struct { |
| 17 | + ID string `bson:"aggregate_id,omitempty"` |
| 18 | + Version int `bson:"version"` |
| 19 | + Type string `bson:"type"` |
| 20 | + Payload json.RawMessage `bson:"payload"` |
| 21 | + RecordedOn time.Time `bson:"recorded_on"` |
| 22 | +} |
| 23 | + |
| 24 | +// EventBus ... |
| 25 | +type EventBus struct { |
| 26 | + brokerDSN string |
| 27 | + name string |
| 28 | + exchange string |
| 29 | +} |
| 30 | + |
| 31 | +// NewEventBus ... |
| 32 | +func NewEventBus(brokerDSN string, name string, exchange string) *EventBus { |
| 33 | + return &EventBus{brokerDSN, name, exchange} |
| 34 | +} |
| 35 | + |
| 36 | +// PublishEvents will publish events |
| 37 | +func (bus *EventBus) PublishEvents(events []*goengine.DomainMessage) error { |
| 38 | + // Connects opens an AMQP connection from the credentials in the URL. |
| 39 | + conn, err := amqp.Dial(bus.brokerDSN) |
| 40 | + if err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + |
| 44 | + // This waits for a server acknowledgment which means the sockets will have |
| 45 | + // flushed all outbound publishings prior to returning. It's important to |
| 46 | + // block on Close to not lose any publishings. |
| 47 | + defer conn.Close() |
| 48 | + |
| 49 | + c, err := conn.Channel() |
| 50 | + if err != nil { |
| 51 | + return fmt.Errorf("channel.open: %s", err) |
| 52 | + } |
| 53 | + |
| 54 | + // We declare our topology on both the publisher and consumer to ensure they |
| 55 | + // are the same. This is part of AMQP being a programmable messaging model. |
| 56 | + // |
| 57 | + // See the Channel.Consume example for the complimentary declare. |
| 58 | + err = c.ExchangeDeclare(bus.exchange, "fanout", true, false, false, false, nil) |
| 59 | + if err != nil { |
| 60 | + return fmt.Errorf("exchange.declare: %v", err) |
| 61 | + } |
| 62 | + |
| 63 | + for _, event := range events { |
| 64 | + rabbitEvent, err := bus.toRawEvent(event) |
| 65 | + if nil != err { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + encodedEvent, err := json.Marshal(rabbitEvent) |
| 70 | + if nil != err { |
| 71 | + return err |
| 72 | + } |
| 73 | + |
| 74 | + // Prepare this message to be persistent. Your publishing requirements may |
| 75 | + // be different. |
| 76 | + msg := amqp.Publishing{ |
| 77 | + DeliveryMode: amqp.Persistent, |
| 78 | + Timestamp: time.Now(), |
| 79 | + ContentEncoding: "UTF-8", |
| 80 | + ContentType: "application/json", |
| 81 | + Body: encodedEvent, |
| 82 | + } |
| 83 | + |
| 84 | + err = c.Publish(bus.exchange, "", true, false, msg) |
| 85 | + if err != nil { |
| 86 | + // Since publish is asynchronous this can happen if the network connection |
| 87 | + // is reset or if the server has run out of resources. |
| 88 | + return fmt.Errorf("basic.publish: %v", err) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +// ReceiveEvents will receive events |
| 96 | +func (bus *EventBus) ReceiveEvents(options goengine.VersionedEventReceiverOptions) error { |
| 97 | + conn, c, events, err := bus.consumeEventsQueue(options.Exclusive) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + |
| 102 | + go func() { |
| 103 | + for { |
| 104 | + select { |
| 105 | + case ch := <-options.Close: |
| 106 | + defer conn.Close() |
| 107 | + ch <- c.Cancel(bus.name, false) |
| 108 | + return |
| 109 | + |
| 110 | + case message, more := <-events: |
| 111 | + if more { |
| 112 | + var raw RawVersionedEvent |
| 113 | + if err := json.Unmarshal(message.Body, &raw); err != nil { |
| 114 | + options.Error <- fmt.Errorf("json.Unmarshal received event: %v", err) |
| 115 | + } else { |
| 116 | + domainEvent, err := bus.fromRawEvent(options.TypeRegistry, &raw) |
| 117 | + if nil != err { |
| 118 | + log.Debugf("EventBus.Cannot find event type %s", raw.Type) |
| 119 | + options.Error <- errors.New("Cannot find event type " + raw.Type) |
| 120 | + message.Ack(true) |
| 121 | + } else { |
| 122 | + ackCh := make(chan bool) |
| 123 | + |
| 124 | + log.Debug("EventBus.Dispatching Message") |
| 125 | + options.ReceiveEvent <- goengine.VersionedEventTransactedAccept{Event: domainEvent, ProcessedSuccessfully: ackCh} |
| 126 | + result := <-ackCh |
| 127 | + message.Ack(result) |
| 128 | + } |
| 129 | + } |
| 130 | + } else { |
| 131 | + log.Debug("RabbitMQ: Could have been disconnected") |
| 132 | + for { |
| 133 | + retryError := exponential(func() error { |
| 134 | + connR, cR, eventsR, errR := bus.consumeEventsQueue(options.Exclusive) |
| 135 | + if errR == nil { |
| 136 | + conn, c, events, err = connR, cR, eventsR, errR |
| 137 | + } |
| 138 | + |
| 139 | + log.Debug(err) |
| 140 | + |
| 141 | + return errR |
| 142 | + }, 5) |
| 143 | + |
| 144 | + if retryError == nil { |
| 145 | + break |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + }() |
| 152 | + |
| 153 | + return nil |
| 154 | +} |
| 155 | + |
| 156 | +// DeleteQueue will delete a queue |
| 157 | +func (bus *EventBus) DeleteQueue(name string) error { |
| 158 | + conn, err := amqp.Dial(bus.brokerDSN) |
| 159 | + if err != nil { |
| 160 | + return err |
| 161 | + } |
| 162 | + |
| 163 | + c, err := conn.Channel() |
| 164 | + if err != nil { |
| 165 | + return fmt.Errorf("channel.open: %s", err) |
| 166 | + } |
| 167 | + |
| 168 | + _, err = c.QueueDelete(name, false, false, true) |
| 169 | + return err |
| 170 | +} |
| 171 | + |
| 172 | +func (bus *EventBus) consumeEventsQueue(exclusive bool) (*amqp.Connection, *amqp.Channel, <-chan amqp.Delivery, error) { |
| 173 | + conn, err := amqp.Dial(bus.brokerDSN) |
| 174 | + if err != nil { |
| 175 | + return nil, nil, nil, err |
| 176 | + } |
| 177 | + |
| 178 | + c, err := conn.Channel() |
| 179 | + if err != nil { |
| 180 | + return nil, nil, nil, fmt.Errorf("channel.open: %s", err) |
| 181 | + } |
| 182 | + |
| 183 | + // We declare our topology on both the publisher and consumer to ensure they |
| 184 | + // are the same. This is part of AMQP being a programmable messaging model. |
| 185 | + // |
| 186 | + // See the Channel.Consume example for the complimentary declare. |
| 187 | + err = c.ExchangeDeclare(bus.exchange, "fanout", true, false, false, false, nil) |
| 188 | + if err != nil { |
| 189 | + return nil, nil, nil, fmt.Errorf("exchange.declare: %v", err) |
| 190 | + } |
| 191 | + |
| 192 | + if _, err = c.QueueDeclare(bus.name, true, false, false, false, nil); err != nil { |
| 193 | + return nil, nil, nil, fmt.Errorf("queue.declare: %v", err) |
| 194 | + } |
| 195 | + |
| 196 | + if err = c.QueueBind(bus.name, bus.name, bus.exchange, false, nil); err != nil { |
| 197 | + return nil, nil, nil, fmt.Errorf("queue.bind: %v", err) |
| 198 | + } |
| 199 | + |
| 200 | + events, err := c.Consume(bus.name, bus.name, false, exclusive, false, false, nil) |
| 201 | + if err != nil { |
| 202 | + return nil, nil, nil, fmt.Errorf("basic.consume: %v", err) |
| 203 | + } |
| 204 | + |
| 205 | + if err := c.Qos(1, 0, false); err != nil { |
| 206 | + return nil, nil, nil, fmt.Errorf("Qos: %v", err) |
| 207 | + } |
| 208 | + |
| 209 | + return conn, c, events, nil |
| 210 | +} |
| 211 | + |
| 212 | +func (bus *EventBus) toRawEvent(event *goengine.DomainMessage) (*RawVersionedEvent, error) { |
| 213 | + serializedPayload, err := json.Marshal(event.Payload) |
| 214 | + if nil != err { |
| 215 | + return nil, err |
| 216 | + } |
| 217 | + |
| 218 | + typeName := reflection.TypeOf(event.Payload) |
| 219 | + return &RawVersionedEvent{ |
| 220 | + ID: event.ID, |
| 221 | + Version: event.Version, |
| 222 | + Type: typeName.String(), |
| 223 | + Payload: serializedPayload, |
| 224 | + RecordedOn: event.RecordedOn, |
| 225 | + }, nil |
| 226 | +} |
| 227 | + |
| 228 | +func (bus *EventBus) fromRawEvent(typeRegistry goengine.TypeRegistry, rabbitEvent *RawVersionedEvent) (*goengine.DomainMessage, error) { |
| 229 | + event, err := typeRegistry.Get(rabbitEvent.Type) |
| 230 | + if nil != err { |
| 231 | + return nil, err |
| 232 | + } |
| 233 | + |
| 234 | + err = json.Unmarshal(rabbitEvent.Payload, event) |
| 235 | + if nil != err { |
| 236 | + return nil, err |
| 237 | + } |
| 238 | + |
| 239 | + return goengine.NewDomainMessage( |
| 240 | + rabbitEvent.ID, |
| 241 | + rabbitEvent.Version, |
| 242 | + event.(goengine.DomainEvent), |
| 243 | + rabbitEvent.RecordedOn, |
| 244 | + ), nil |
| 245 | +} |
0 commit comments