Commit 87c5dba9 authored by dpi's avatar dpi

Added global skip queue option.

Can now choose to force all messages to skip the queue.
Setting added to Courier settings form.
Added tests for skip queue functionality.
Updated TestMessage entity to adding internal message buffer to sendMessage.

Fixed #10
parent 2c8baf14
skip_queue: true
channel_preferences:
user:
- courier_email
......@@ -3,6 +3,9 @@ courier.settings:
type: config_object
label: 'Settings for Courier'
mapping:
skip_queue:
type: boolean
label: 'Whether messages should be tried in the same request'
channel_preferences:
type: sequence
label: 'Default channel preferences for identities'
......
......@@ -4,7 +4,7 @@ services:
parent: default_plugin_manager
courier.manager:
class: Drupal\courier\Service\CourierManager
arguments: ['@entity_type.manager', '@logger.factory', '@plugin.manager.identity_channel']
arguments: ['@entity_type.manager', '@config.factory', '@logger.factory', '@plugin.manager.identity_channel']
courier.manager.global_template_collection:
class: Drupal\courier\Service\GlobalTemplateCollectionManager
arguments: ['@keyvalue', '@courier.manager']
......
......@@ -116,7 +116,7 @@ class CourierSystemTest extends CourierKernelTestBase {
_user_mail_notify($id, $this->createUser());
$this->cron->run();
$this->assertMailString('body', $body, 1, 'Body found in override for ' . $id);
$this->assertMailString('body', $body, 2, 'Body found in override for ' . $id);
}
}
......
......@@ -184,6 +184,17 @@ The first successful message for a channel will be transmitted, all subsequent c
}
}
$form['devel'] = [
'#type' => 'details',
'#open' => TRUE,
];
$form['devel']['skip_queue'] = [
'#type' => 'checkbox',
'#title' => $this->t('Skip queue'),
'#description' => $this->t('Whether messages skip the load balancing queue and process in the same request. Only turn on this setting when debugging, do not use it on production sites.'),
'#default_value' => $config->get('skip_queue'),
];
return $form;
}
......@@ -203,6 +214,7 @@ The first successful message for a channel will be transmitted, all subsequent c
}
$config
->set('skip_queue', $form_state->getValue('skip_queue'))
->set('channel_preferences', $channel_preferences)
->save();
......
......@@ -5,6 +5,7 @@ namespace Drupal\courier\Service;
use Drupal\courier\TemplateCollectionInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\courier\Entity\MessageQueueItem;
use Drupal\courier\Exception\IdentityException;
......@@ -21,6 +22,13 @@ class CourierManager implements CourierManagerInterface {
*/
protected $entityTypeManager;
/**
* The configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The logger for the Courier channel.
*
......@@ -40,13 +48,16 @@ class CourierManager implements CourierManagerInterface {
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* The logger factory service.
* @param \Drupal\courier\Service\IdentityChannelManagerInterface $identity_channel_manager
* The identity channel manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, LoggerChannelFactoryInterface $logger_factory, IdentityChannelManagerInterface $identity_channel_manager) {
public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, LoggerChannelFactoryInterface $logger_factory, IdentityChannelManagerInterface $identity_channel_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->configFactory = $config_factory;
$this->logger = $logger_factory->get('courier');
$this->identityChannelManager = $identity_channel_manager;
}
......@@ -95,7 +106,11 @@ class CourierManager implements CourierManagerInterface {
$t_args = $t_args_base;
$t_args['@template'] = $template->id();
$t_args['%channel'] = $channel;
if ($plugin = $this->identityChannelManager->getCourierIdentity($channel, $identity->getEntityTypeId())) {
$plugin = $this->identityChannelManager
->getCourierIdentity($channel, $identity->getEntityTypeId());
if ($plugin) {
$message = $template->createDuplicate();
if ($message->id()) {
throw new \Exception(sprintf('Failed to clone `%s`', $channel));
......@@ -113,7 +128,7 @@ class CourierManager implements CourierManagerInterface {
}
if ($message->isEmpty()) {
$this->logger->debug('Template @template for collection @template_collection was empty.', $t_args);
$this->logger->debug('Template @template (%channel) for collection @template_collection was empty.', $t_args);
continue;
}
......@@ -141,11 +156,16 @@ class CourierManager implements CourierManagerInterface {
}
if ($message_queue->getMessages()) {
$message_queue->save();
$queue = \Drupal::queue('courier_message');
$queue->createItem([
'id' => $message_queue->id(),
]);
if ($this->getSkipQueue()) {
$message_queue->sendMessage();
}
else {
$message_queue->save();
$queue = \Drupal::queue('courier_message');
$queue->createItem([
'id' => $message_queue->id(),
]);
}
return $message_queue;
}
......@@ -153,4 +173,15 @@ class CourierManager implements CourierManagerInterface {
return FALSE;
}
/**
* Whether message queue item should skip queue.
*
* @return bool
*/
public function getSkipQueue() {
$skip_queue = $this->configFactory->get('courier.settings')
->get('skip_queue');
return !empty($skip_queue);
}
}
......@@ -72,7 +72,12 @@ class TestMessage extends ChannelBase implements TestMessageInterface {
* {@inheritdoc}
*/
static public function sendMessages(array $messages, $options = []) {
$state = \Drupal::state()->get('courier_test_message.messages', []);
/* @var static[] $messages */
foreach ($messages as $message) {
$state[] = $message;
}
\Drupal::state()->set('courier_test_message.messages', $state);
}
/**
......
......@@ -12,11 +12,6 @@ use Drupal\courier\Entity\TemplateCollection;
*/
class CourierGlobalCollectionEntityTest extends CourierKernelTestBase {
/**
* @var \Drupal\courier\Service\GlobalTemplateCollectionManagerInterface
*/
protected $gtcService;
/**
* {@inheritdoc}
*/
......
<?php
namespace Drupal\Tests\courier\Kernel;
use Drupal\Core\Test\AssertMailTrait;
use Drupal\courier\Entity\MessageQueueItem;
use Drupal\courier\Entity\TemplateCollection;
use Drupal\user\Entity\User;
/**
* Tests Courier manager.
*
* @group courier
*/
class CourierManagerTest extends CourierKernelTestBase {
use AssertMailTrait;
public static $modules = ['courier_test_message', 'user', 'system', 'dblog'];
/**
* @var \Drupal\courier\Service\CourierManagerInterface
*/
protected $courierManager;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installConfig(['courier']);
$this->installEntitySchema('courier_message_queue_item');
$this->installEntitySchema('courier_template_collection');
$this->installEntitySchema('courier_email');
$this->installEntitySchema('courier_test_message');
$this->installEntitySchema('user');
$this->courierManager = $this->container->get('courier.manager');
$this->installSchema('dblog', ['watchdog']);
}
/**
* Test skip queue is off.
*/
public function testSkipQueueOff() {
$this->config('courier.settings')
->set('skip_queue', FALSE)
->set('channel_preferences', ['user' => ['courier_test_message']])
->save();
$template_collection = TemplateCollection::create();
$this->courierManager
->addTemplates($template_collection);
$template_collection->save();
$template_collection->getTemplate('courier_test_message')
->setMessage($this->randomString())
->save();
$identity = User::create(['uid' => 1, 'name' => $this->randomMachineName(), 'mail' => 'user@email.tld']);
$identity->save();
$this->courierManager
->sendMessage($template_collection, $identity);
$this->assertEquals(1, count(MessageQueueItem::loadMultiple()));
$this->assertEquals(0, count(\Drupal::state()->get('courier_test_message.messages', [])));
}
/**
* Test skip queue is on.
*/
public function testSkipQueueOn() {
$this->config('courier.settings')
->set('skip_queue', TRUE)
->set('channel_preferences', ['user' => ['courier_test_message']])
->save();
$template_collection = TemplateCollection::create();
$this->courierManager
->addTemplates($template_collection);
$template_collection->save();
$message = $this->randomString();
$template_collection->getTemplate('courier_test_message')
->setMessage($message)
->save();
$identity = User::create(['uid' => 1, 'name' => $this->randomMachineName(), 'mail' => 'user@email.tld']);
$identity->save();
$this->courierManager
->sendMessage($template_collection, $identity);
$this->assertEquals(0, count(MessageQueueItem::loadMultiple()));
$this->assertEquals(1, count(\Drupal::state()->get('courier_test_message.messages', [])));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment