Skip to content

Commit

Permalink
Merge 81c6c36 into 5b8ec3a
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-rose committed Jan 16, 2019
2 parents 5b8ec3a + 81c6c36 commit ad2eefa
Show file tree
Hide file tree
Showing 13 changed files with 261 additions and 68 deletions.
Expand Up @@ -7,6 +7,7 @@
use Jellyfish\Event\EventQueueConsumerInterface;
use Jellyfish\Lock\LockFactoryInterface;
use Jellyfish\Lock\LockTrait;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -29,21 +30,29 @@ class EventQueueConsumeCommand extends Command
*/
protected $eventQueueConsumer;

/**
* @var \Psr\Log\LoggerInterface
*/
protected $logger;

/**
* @param \Jellyfish\Event\EventDispatcherInterface $eventDispatcher
* @param \Jellyfish\Event\EventQueueConsumerInterface $eventQueueConsumer
* @param \Jellyfish\Lock\LockFactoryInterface $lockFactory
* @param \Psr\Log\LoggerInterface $logger
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
EventQueueConsumerInterface $eventQueueConsumer,
LockFactoryInterface $lockFactory
LockFactoryInterface $lockFactory,
LoggerInterface $logger
) {
parent::__construct();

$this->eventDispatcher = $eventDispatcher;
$this->eventQueueConsumer = $eventQueueConsumer;
$this->lockFactory = $lockFactory;
$this->logger = $logger;
}

/**
Expand Down Expand Up @@ -76,9 +85,15 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
return null;
}

$result = $this->executeLockablePart($eventName, $listenerIdentifier);
$result = null;

$this->release();
try {
$result = $this->executeLockablePart($eventName, $listenerIdentifier);
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
} finally {
$this->release();
}

return $result;
}
Expand Down
15 changes: 7 additions & 8 deletions packages/event/src/Jellyfish/Event/EventDispatcher.php
Expand Up @@ -69,28 +69,27 @@ public function removeListener(string $eventName, EventListenerInterface $listen
}

/**
* @param string $eventName
* @param \Jellyfish\Event\EventInterface $event
*
* @return \Jellyfish\Event\EventDispatcherInterface
*/
public function dispatch(string $eventName, EventInterface $event): EventDispatcherInterface
public function dispatch(EventInterface $event): EventDispatcherInterface
{
$this->dispatchSync($eventName, $event);
$this->dispatchAsync($eventName, $event);
$this->dispatchSync($event);
$this->dispatchAsync($event);

return $this;
}

/**
* @param string $eventName
* @param \Jellyfish\Event\EventInterface $event
*
* @return \Jellyfish\Event\EventDispatcherInterface
*/
protected function dispatchSync(string $eventName, EventInterface $event): EventDispatcherInterface
protected function dispatchSync(EventInterface $event): EventDispatcherInterface
{
$type = EventListenerInterface::TYPE_SYNC;
$eventName = $event->getName();

if (!\array_key_exists($eventName, $this->listeners[$type])) {
return $this;
Expand All @@ -105,14 +104,14 @@ protected function dispatchSync(string $eventName, EventInterface $event): Event
}

/**
* @param string $eventName
* @param \Jellyfish\Event\EventInterface $event
*
* @return \Jellyfish\Event\EventDispatcherInterface
*/
protected function dispatchAsync(string $eventName, EventInterface $event): EventDispatcherInterface
protected function dispatchAsync(EventInterface $event): EventDispatcherInterface
{
$type = EventListenerInterface::TYPE_ASYNC;
$eventName = $event->getName();

if (!\array_key_exists($eventName, $this->listeners[$type])) {
return $this;
Expand Down
Expand Up @@ -21,12 +21,11 @@ public function addListener(string $eventName, EventListenerInterface $listener)
public function removeListener(string $eventName, EventListenerInterface $listener): EventDispatcherInterface;

/**
* @param string $eventName
* @param \Jellyfish\Event\EventInterface $event
*
* @return \Jellyfish\Event\EventDispatcherInterface
*/
public function dispatch(string $eventName, EventInterface $event): EventDispatcherInterface;
public function dispatch(EventInterface $event): EventDispatcherInterface;

/**
* @param string|null $type
Expand Down
3 changes: 2 additions & 1 deletion packages/event/src/Jellyfish/Event/EventServiceProvider.php
Expand Up @@ -155,7 +155,8 @@ protected function createCommands(Container $container): ServiceProviderInterfac
$commands[] = new EventQueueConsumeCommand(
$container->offsetGet('event_dispatcher'),
$container->offsetGet('event_queue_consumer'),
$container->offsetGet('lock_factory')
$container->offsetGet('lock_factory'),
$container->offsetGet('logger')
);

$commands[] = new EventQueueWorkerStartCommand(
Expand Down
Expand Up @@ -9,6 +9,7 @@
use Jellyfish\Event\EventQueueConsumerInterface;
use Jellyfish\Lock\LockFactoryInterface;
use Jellyfish\Lock\LockInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

Expand Down Expand Up @@ -54,6 +55,11 @@ class EventQueueConsumeCommandTest extends Unit
*/
protected $lockMock;

/**
* @var \Psr\Log\LoggerInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $loggerMock;

/**
* @var \Jellyfish\Event\Command\EventQueueConsumeCommand
*/
Expand Down Expand Up @@ -114,6 +120,10 @@ protected function _before(): void
->disableOriginalConstructor()
->getMock();

$this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
->disableOriginalConstructor()
->getMock();

$this->eventName = 'test';
$this->listenerIdentifier = 'testListener';

Expand All @@ -123,7 +133,8 @@ protected function _before(): void
$this->eventQueueConsumeCommand = new EventQueueConsumeCommand(
$this->eventDispatcherMock,
$this->eventQueueConsumerMock,
$this->lockFactoryMock
$this->lockFactoryMock,
$this->loggerMock
);
}

Expand Down Expand Up @@ -279,6 +290,59 @@ public function testRun(): void
->method('handle')
->with($this->eventMock);

$this->loggerMock->expects($this->never())
->method('error');

$this->lockMock->expects($this->atLeastOnce())
->method('release')
->willReturn($this->lockMock);

$exitCode = $this->eventQueueConsumeCommand->run($this->inputMock, $this->outputMock);

$this->assertEquals(0, $exitCode);
}

/**
* @return void
*
* @throws \Exception
*/
public function testRunWithHandlerException(): void
{
$exceptionMessage = 'Test exception';

$this->inputMock->expects($this->atLeastOnce())
->method('getArgument')
->withConsecutive(['eventName'], ['listenerIdentifier'])
->willReturnOnConsecutiveCalls($this->eventName, $this->listenerIdentifier);

$this->lockFactoryMock->expects($this->atLeastOnce())
->method('create')
->with($this->lockIdentifier, 360.0)
->willReturn($this->lockMock);

$this->lockMock->expects($this->atLeastOnce())
->method('acquire')
->willReturn(true);

$this->eventQueueConsumerMock->expects($this->atLeastOnce())
->method('dequeueEvent')
->with($this->eventName, $this->listenerIdentifier)
->willReturn($this->eventMock);

$this->eventDispatcherMock->expects($this->atLeastOnce())
->method('getListener')
->with(EventListenerInterface::TYPE_ASYNC, $this->eventName, $this->listenerIdentifier)
->willReturn($this->eventListenerMock);

$this->eventListenerMock->expects($this->atLeastOnce())
->method('handle')
->willThrowException(new \Exception($exceptionMessage));

$this->loggerMock->expects($this->atLeastOnce())
->method('error')
->with($exceptionMessage);

$this->lockMock->expects($this->atLeastOnce())
->method('release')
->willReturn($this->lockMock);
Expand Down
18 changes: 15 additions & 3 deletions packages/event/tests/Jellyfish/Event/EventDispatcherTest.php
Expand Up @@ -204,7 +204,11 @@ public function testGetXListeners(): void
*/
public function testDispatchWithoutListeners(): void
{
$result = $this->eventDispatcher->dispatch($this->eventName, $this->eventMock);
$this->eventMock->expects($this->atLeastOnce())
->method('getName')
->willReturn($this->eventName);

$result = $this->eventDispatcher->dispatch($this->eventMock);

$this->assertEquals($this->eventDispatcher, $result);
}
Expand All @@ -222,6 +226,10 @@ public function testDispatchSyncListeners(): void
->method('getType')
->willReturn(EventListenerInterface::TYPE_SYNC);

$this->eventMock->expects($this->atLeastOnce())
->method('getName')
->willReturn($this->eventName);

$this->assertEquals(
$this->eventDispatcher,
$this->eventDispatcher->addListener($this->eventName, $this->eventListenerMock)
Expand All @@ -233,7 +241,7 @@ public function testDispatchSyncListeners(): void

$this->assertEquals(
$this->eventDispatcher,
$this->eventDispatcher->dispatch($this->eventName, $this->eventMock)
$this->eventDispatcher->dispatch($this->eventMock)
);
}

Expand All @@ -247,6 +255,10 @@ public function testDispatchAsyncListeners(): void
->method('getType')
->willReturn(EventListenerInterface::TYPE_ASYNC);

$this->eventMock->expects($this->atLeastOnce())
->method('getName')
->willReturn($this->eventName);

$this->assertEquals(
$this->eventDispatcher,
$this->eventDispatcher->addListener($this->eventName, $this->eventListenerMock)
Expand All @@ -258,7 +270,7 @@ public function testDispatchAsyncListeners(): void

$this->assertEquals(
$this->eventDispatcher,
$this->eventDispatcher->dispatch($this->eventName, $this->eventMock)
$this->eventDispatcher->dispatch($this->eventMock)
);
}
}
Expand Up @@ -11,6 +11,7 @@
use Jellyfish\Queue\QueueClientInterface;
use Pimple\Container;
use Jellyfish\Serializer\SerializerInterface;
use Psr\Log\LoggerInterface;

class EventServiceProviderTest extends Unit
{
Expand Down Expand Up @@ -62,6 +63,12 @@ protected function _before(): void
->getMock();
});

$this->container->offsetSet('logger', function () use ($self) {
return $self->getMockBuilder(LoggerInterface::class)
->disableOriginalConstructor()
->getMock();
});

$this->container->offsetSet('message_factory', function () use ($self) {
return $self->getMockBuilder(MessageFactoryInterface::class)
->disableOriginalConstructor()
Expand Down
51 changes: 34 additions & 17 deletions packages/log/src/Jellyfish/Log/LogServiceProvider.php
Expand Up @@ -2,13 +2,12 @@

namespace Jellyfish\Log;

use Jellyfish\Config\ConfigInterface;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Psr\Log\LoggerInterface;

class LogServiceProvider implements ServiceProviderInterface
{
Expand All @@ -19,40 +18,58 @@ class LogServiceProvider implements ServiceProviderInterface
*/
public function register(Container $pimple): void
{
$self = $this;

$pimple->offsetSet('logger', function ($container) use ($self) {
return $self->createLogger($container['config']);
});
$this->createLogger($pimple);
}

/**
* @param \Jellyfish\Config\ConfigInterface $config
* @param \Pimple\Container $container
*
* @return \Psr\Log\LoggerInterface
* @return \Pimple\ServiceProviderInterface
*
* @throws \Exception
*/
protected function createLogger(ConfigInterface $config): LoggerInterface
protected function createLogger(Container $container): ServiceProviderInterface
{
$logger = new Logger('jellyfish');
$self = $this;

$logger->pushHandler($this->createStreamHandler($config));
$container->offsetSet('logger', function (Container $container) use ($self) {
$logger = new Logger('jellyfish');

return $logger;
$logger->pushHandler($self->createStreamHandler($container));
$logger->pushHandler($self->createRotatingFileHandler($container));

return $logger;
});

return $this;
}

/**
* @param \Jellyfish\Config\ConfigInterface $config
* @param \Pimple\Container $container
*
* @return \Monolog\Handler\AbstractProcessingHandler
*
* @throws \Exception
*/
protected function createStreamHandler(ConfigInterface $config): AbstractProcessingHandler
protected function createStreamHandler(Container $container): AbstractProcessingHandler
{
$logLevel = $config->get(LogConstants::LOG_LEVEL, (string) LogConstants::DEFAULT_LOG_LEVEL);
$logLevel = $container->offsetGet('config')
->get(LogConstants::LOG_LEVEL, (string) LogConstants::DEFAULT_LOG_LEVEL);

return new StreamHandler('php://stdout', (int) $logLevel);
}

/**
* @param \Pimple\Container $container
*
* @return \Monolog\Handler\AbstractProcessingHandler
*/
protected function createRotatingFileHandler(Container $container): AbstractProcessingHandler
{
$logLevel = $container->offsetGet('config')
->get(LogConstants::LOG_LEVEL, (string) LogConstants::DEFAULT_LOG_LEVEL);

$filename = $container->offsetGet('root_dir') . 'var/log/jellyfish.log';

return new RotatingFileHandler($filename, 0, (int) $logLevel);
}
}

0 comments on commit ad2eefa

Please sign in to comment.