Beispiel #1
0
 /**
  * @param EventMessageInterface $event
  */
 private function delay(EventMessageInterface $event)
 {
     $eventTime = $event->getTimestamp()->getTimestamp();
     $dispatchTime = $eventTime + $this->delaySeconds;
     $waitSeconds = $dispatchTime - time();
     while ($waitSeconds > 0) {
         sleep($waitSeconds);
         // Make sure it's time to dispatch, even if sleep has been interrupted by the process control signal
         $waitSeconds = $dispatchTime - time();
     }
 }
Beispiel #2
0
 /**
  * @param EventMessageInterface $event
  * @param SerializerInterface $serializer
  * @return RedisEventRecord
  */
 public static function fromMessage(EventMessageInterface $event, SerializerInterface $serializer)
 {
     $data = ['id' => $event->getId(), 'timestamp' => $event->getTimestamp(), 'payload_type' => $event->getPayloadType(), 'payload' => $serializer->serialize($event->getPayload()), 'metadata' => $serializer->serialize($event->getMetadata())];
     if ($event instanceof DomainEventMessageInterface) {
         $data['aggregate'] = ['type' => $event->getAggregateType(), 'id' => $event->getAggregateId(), 'seq' => $event->getSequenceNumber()];
     }
     return new self(json_encode($data));
 }
 /**
  * @param EventMessageInterface $event
  * @return string
  */
 private function getEventName(EventMessageInterface $event)
 {
     $name = $event->getPayloadType();
     $pos = strrpos($name, '\\');
     if ($pos !== false) {
         $name = substr($name, $pos + 1);
     }
     if (substr($name, -5) === 'Event') {
         $name = substr($name, 0, -5);
     }
     return $name;
 }
Beispiel #4
0
 public function isValid(EventMessageInterface $event)
 {
     $meta = $event->getMetadata();
     return isset($meta['valid']) ? (bool) $meta['valid'] : false;
 }
Beispiel #5
0
 private function trackEvent(EventMessageInterface $event)
 {
     $this->eventsCount++;
     $this->lastEventId = $event->getId();
     $this->lastEventTime = $event->getTimestamp();
     $this->lastUpdate = new DateTime();
 }
Beispiel #6
0
 /**
  * @param Callable $handler
  * @param EventMessageInterface $event
  * @throws \Exception
  */
 private function invokeEventHandler(callable $handler, EventMessageInterface $event)
 {
     $handlerName = $this->getHandlerName($handler);
     $this->logger->debug(sprintf('Invoking event handler %s', $handlerName), ['event_id' => $event->getId(), 'event_type' => $event->getPayloadType()]);
     try {
         if ($event instanceof DomainEventMessageInterface) {
             $handler($event->getPayload(), $event->getMetadata(), $event->getTimestamp(), $event->getSequenceNumber(), $event->getAggregateId());
         } else {
             $handler($event->getPayload(), $event->getMetadata(), $event->getTimestamp());
         }
     } catch (\Exception $e) {
         $this->logger->error(sprintf('Uncaught Exception %s while handling Event %s: "%s" at %s line %s', get_class($e), $event->getPayloadType(), $e->getMessage(), $e->getFile(), $e->getLine()), ['exception' => $e, 'event_id' => $event->getId()]);
         if ($event->getPayload() instanceof EventExecutionFailed) {
             return;
         }
         $this->publish(new GenericEventMessage(new EventExecutionFailed($event, $e), $event->getMetadata()));
         throw $e;
     }
 }
Beispiel #7
0
 /**
  * @param EventMessageInterface $event
  * @return array
  */
 private function toArray(EventMessageInterface $event)
 {
     $dateTimeFormat = $this->connection->getDatabasePlatform()->getDateTimeFormatString();
     $timestamp = $event->getTimestamp();
     $data = ['event_id' => (string) $event->getId(), 'event_date' => $timestamp->format($dateTimeFormat), 'event_date_u' => $timestamp->format('u'), 'payload_type' => $event->getPayloadType(), 'payload' => $this->serializer->serialize($event->getPayload()), 'metadata' => $this->serializer->serialize($event->getMetadata())];
     if ($event instanceof DomainEventMessageInterface) {
         $aggregateId = $event->getAggregateId();
         if (!is_int($aggregateId)) {
             $aggregateId = (string) $aggregateId;
         }
         $data = array_merge($data, ['aggregate_type' => $event->getAggregateType(), 'aggregate_id' => $aggregateId, 'sequence_number' => $event->getSequenceNumber()]);
     }
     return $data;
 }