Browse Source

Ensure we can connect to the JMS Broker everytime (#868)

pull/871/head
Jared Whiklo 2 years ago committed by GitHub
parent
commit
d8d101e571
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 50
      src/EventGenerator/EmitEvent.php
  2. 12
      src/Plugin/Action/AbstractGenerateDerivativeBase.php

50
src/EventGenerator/EmitEvent.php

@ -5,6 +5,7 @@ namespace Drupal\islandora\EventGenerator;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Action\ConfigurableActionBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
@ -67,6 +68,13 @@ abstract class EmitEvent extends ConfigurableActionBase implements ContainerFact
*/
protected $messenger;
/**
* The logger.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected $logger;
/**
* Constructs a EmitEvent action.
*
@ -88,6 +96,8 @@ abstract class EmitEvent extends ConfigurableActionBase implements ContainerFact
* The messenger.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* Event dispatcher service.
* @param \Drupal\Core\Logger\LoggerChannelInterface $channel
* Logger channel.
*/
public function __construct(
array $configuration,
@ -98,7 +108,8 @@ abstract class EmitEvent extends ConfigurableActionBase implements ContainerFact
EventGeneratorInterface $event_generator,
StatefulStomp $stomp,
MessengerInterface $messenger,
EventDispatcherInterface $event_dispatcher
EventDispatcherInterface $event_dispatcher,
LoggerChannelInterface $channel
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->account = $account;
@ -107,6 +118,7 @@ abstract class EmitEvent extends ConfigurableActionBase implements ContainerFact
$this->stomp = $stomp;
$this->messenger = $messenger;
$this->eventDispatcher = $event_dispatcher;
$this->logger = $channel;
}
/**
@ -122,7 +134,8 @@ abstract class EmitEvent extends ConfigurableActionBase implements ContainerFact
$container->get('islandora.eventgenerator'),
$container->get('islandora.stomp'),
$container->get('messenger'),
$container->get('event_dispatcher')
$container->get('event_dispatcher'),
$container->get('logger.channel.islandora')
);
}
@ -132,6 +145,16 @@ abstract class EmitEvent extends ConfigurableActionBase implements ContainerFact
public function execute($entity = NULL) {
// Generate event as stomp message.
try {
if (is_null($this->stomp->getClient()->getProtocol())) {
// getProtocol() can return NULL but that causes a larger problem.
// So attempt to disconnect + connect to re-establish the connection or
// throw a StompException.
// @see https://github.com/stomp-php/stomp-php/issues/167
// @see https://github.com/stomp-php/stomp-php/blob/3a9347a11743d0b79fd60564f356bc3efe40e615/src/Client.php#L429-L434
$this->stomp->getClient()->disconnect();
$this->stomp->getClient()->connect();
}
$user = $this->entityTypeManager->getStorage('user')->load($this->account->id());
$data = $this->generateData($entity);
@ -146,18 +169,22 @@ abstract class EmitEvent extends ConfigurableActionBase implements ContainerFact
);
}
catch (StompHeaderEventException $e) {
\Drupal::logger('islandora')->error($e->getMessage());
$this->messenger->addMessage($e->getMessage(), 'error');
$this->logger->error($e->getMessage());
$this->messenger->addError($e->getMessage());
return;
}
catch (StompException $e) {
$this->logger->error("Unable to connect to JMS Broker: @msg", ["@msg" => $e->getMessage()]);
$this->messenger->addWarning("Unable to connect to JMS Broker, items might not be synchronized to external services.");
return;
}
catch (\RuntimeException $e) {
// Notify the user the event couldn't be generated and abort.
\Drupal::logger('islandora')->error(
$this->logger->error(
$this->t('Error generating event: @msg', ['@msg' => $e->getMessage()])
);
$this->messenger->addMessage(
$this->t('Error generating event: @msg', ['@msg' => $e->getMessage()]),
'error'
$this->messenger->addError(
$this->t('Error generating event: @msg', ['@msg' => $e->getMessage()])
);
return;
}
@ -170,17 +197,16 @@ abstract class EmitEvent extends ConfigurableActionBase implements ContainerFact
}
catch (StompException $e) {
// Log it.
\Drupal::logger('islandora')->error(
$this->logger->error(
'Error publishing message: @msg',
['@msg' => $e->getMessage()]
);
// Notify user.
$this->messenger->addMessage(
$this->messenger->addError(
$this->t('Error publishing message: @msg',
['@msg' => $e->getMessage()]
),
'error'
)
);
}
}

12
src/Plugin/Action/AbstractGenerateDerivativeBase.php

@ -5,6 +5,7 @@ namespace Drupal\islandora\Plugin\Action;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\islandora\IslandoraUtils;
@ -94,6 +95,8 @@ class AbstractGenerateDerivativeBase extends EmitEvent {
* Field Manager service.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* Event dispatcher service.
* @param \Drupal\Core\Logger\LoggerChannelInterface $channel
* The logger channel.
*/
public function __construct(
array $configuration,
@ -109,7 +112,8 @@ class AbstractGenerateDerivativeBase extends EmitEvent {
MessengerInterface $messenger,
ConfigFactoryInterface $config,
EntityFieldManagerInterface $entity_field_manager,
EventDispatcherInterface $event_dispatcher
EventDispatcherInterface $event_dispatcher,
LoggerChannelInterface $channel
) {
$this->utils = $utils;
$this->mediaSource = $media_source;
@ -126,7 +130,8 @@ class AbstractGenerateDerivativeBase extends EmitEvent {
$event_generator,
$stomp,
$messenger,
$event_dispatcher
$event_dispatcher,
$channel
);
}
@ -148,7 +153,8 @@ class AbstractGenerateDerivativeBase extends EmitEvent {
$container->get('messenger'),
$container->get('config.factory'),
$container->get('entity_field.manager'),
$container->get('event_dispatcher')
$container->get('event_dispatcher'),
$container->get('logger.channel.islandora')
);
}

Loading…
Cancel
Save