Skip to content

[consumption] Add ability to change process exit status from within queue consumer extension #766

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions pkg/enqueue/Consumption/Context/End.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,23 @@ final class End
*/
private $logger;

public function __construct(Context $context, int $startTime, int $endTime, LoggerInterface $logger)
{
/**
* @var int
*/
private $exitStatus;

public function __construct(
Context $context,
int $startTime,
int $endTime,
LoggerInterface $logger,
?int $exitStatus = null
) {
$this->context = $context;
$this->logger = $logger;
$this->startTime = $startTime;
$this->endTime = $endTime;
$this->exitStatus = $exitStatus;
}

public function getContext(): Context
Expand Down Expand Up @@ -60,4 +71,9 @@ public function getEndTime(): int
{
return $this->startTime;
}

public function getExitStatus(): ?int
{
return $this->exitStatus;
}
}
13 changes: 12 additions & 1 deletion pkg/enqueue/Consumption/Context/PostConsume.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ final class PostConsume
*/
private $executionInterrupted;

/**
* @var int
*/
private $exitStatus;

public function __construct(Context $context, SubscriptionConsumer $subscriptionConsumer, int $receivedMessagesCount, int $cycle, int $startTime, LoggerInterface $logger)
{
$this->context = $context;
Expand Down Expand Up @@ -85,13 +90,19 @@ public function getLogger(): LoggerInterface
return $this->logger;
}

public function getExitStatus(): ?int
{
return $this->exitStatus;
}

public function isExecutionInterrupted(): bool
{
return $this->executionInterrupted;
}

public function interruptExecution(): void
public function interruptExecution(?int $exitStatus = null): void
{
$this->exitStatus = $exitStatus;
$this->executionInterrupted = true;
}
}
15 changes: 13 additions & 2 deletions pkg/enqueue/Consumption/Context/PostMessageReceived.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ final class PostMessageReceived
*/
private $executionInterrupted;

/**
* @var int
*/
private $exitStatus;

public function __construct(
Context $context,
Consumer $consumer,
Expand Down Expand Up @@ -89,20 +94,26 @@ public function getReceivedAt(): int
}

/**
* @return Result|null|object|string
* @return Result|object|string|null
*/
public function getResult()
{
return $this->result;
}

public function getExitStatus(): ?int
{
return $this->exitStatus;
}

public function isExecutionInterrupted(): bool
{
return $this->executionInterrupted;
}

public function interruptExecution(): void
public function interruptExecution(?int $exitStatus = null): void
{
$this->exitStatus = $exitStatus;
$this->executionInterrupted = true;
}
}
13 changes: 12 additions & 1 deletion pkg/enqueue/Consumption/Context/PreConsume.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ final class PreConsume
*/
private $executionInterrupted;

/**
* @var int
*/
private $exitStatus;

public function __construct(Context $context, SubscriptionConsumer $subscriptionConsumer, LoggerInterface $logger, int $cycle, int $receiveTimeout, int $startTime)
{
$this->context = $context;
Expand Down Expand Up @@ -85,13 +90,19 @@ public function getStartTime(): int
return $this->startTime;
}

public function getExitStatus(): ?int
{
return $this->exitStatus;
}

public function isExecutionInterrupted(): bool
{
return $this->executionInterrupted;
}

public function interruptExecution(): void
public function interruptExecution(?int $exitStatus = null): void
{
$this->exitStatus = $exitStatus;
$this->executionInterrupted = true;
}
}
13 changes: 12 additions & 1 deletion pkg/enqueue/Consumption/Context/Start.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ final class Start
*/
private $executionInterrupted;

/**
* @var int
*/
private $exitStatus;

/**
* @param BoundProcessor[] $processors
*/
Expand Down Expand Up @@ -105,13 +110,19 @@ public function changeBoundProcessors(array $processors): void
});
}

public function getExitStatus(): ?int
{
return $this->exitStatus;
}

public function isExecutionInterrupted(): bool
{
return $this->executionInterrupted;
}

public function interruptExecution(): void
public function interruptExecution(?int $exitStatus = null): void
{
$this->exitStatus = $exitStatus;
$this->executionInterrupted = true;
}
}
24 changes: 24 additions & 0 deletions pkg/enqueue/Consumption/Extension/ExitStatusExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Enqueue\Consumption\Extension;

use Enqueue\Consumption\Context\End;
use Enqueue\Consumption\EndExtensionInterface;

class ExitStatusExtension implements EndExtensionInterface
{
/**
* @var int
*/
private $exitStatus;

public function onEnd(End $context): void
{
$this->exitStatus = $context->getExitStatus();
}

public function getExitStatus(): ?int
{
return $this->exitStatus;
}
}
11 changes: 6 additions & 5 deletions pkg/enqueue/Consumption/QueueConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function consume(ExtensionInterface $runtimeExtension = null): void
$extension->onStart($start);

if ($start->isExecutionInterrupted()) {
$this->onEnd($extension, $startTime);
$this->onEnd($extension, $startTime, $start->getExitStatus());

return;
}
Expand Down Expand Up @@ -256,7 +256,7 @@ public function consume(ExtensionInterface $runtimeExtension = null): void
$extension->onPreConsume($preConsume);

if ($preConsume->isExecutionInterrupted()) {
$this->onEnd($extension, $startTime, $subscriptionConsumer);
$this->onEnd($extension, $startTime, $preConsume->getExitStatus(), $subscriptionConsumer);

return;
}
Expand All @@ -267,7 +267,7 @@ public function consume(ExtensionInterface $runtimeExtension = null): void
$extension->onPostConsume($postConsume);

if ($interruptExecution || $postConsume->isExecutionInterrupted()) {
$this->onEnd($extension, $startTime, $subscriptionConsumer);
$this->onEnd($extension, $startTime, $postConsume->getExitStatus(), $subscriptionConsumer);

return;
}
Expand All @@ -286,11 +286,12 @@ public function setFallbackSubscriptionConsumer(SubscriptionConsumer $fallbackSu
$this->fallbackSubscriptionConsumer = $fallbackSubscriptionConsumer;
}

private function onEnd(ExtensionInterface $extension, int $startTime, SubscriptionConsumer $subscriptionConsumer = null): void
private function onEnd(ExtensionInterface $extension, int $startTime, ?int $exitStatus = null, SubscriptionConsumer $subscriptionConsumer = null): void
{
$endTime = (int) (microtime(true) * 1000);

$extension->onEnd(new End($this->interopContext, $startTime, $endTime, $this->logger));
$endContext = new End($this->interopContext, $startTime, $endTime, $this->logger, $exitStatus);
$extension->onEnd($endContext);

if ($subscriptionConsumer) {
$subscriptionConsumer->unsubscribeAll();
Expand Down
8 changes: 6 additions & 2 deletions pkg/enqueue/Symfony/Client/ConsumeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Enqueue\Client\DriverInterface;
use Enqueue\Consumption\ChainExtension;
use Enqueue\Consumption\Extension\ExitStatusExtension;
use Enqueue\Consumption\Extension\LoggerExtension;
use Enqueue\Consumption\ExtensionInterface;
use Enqueue\Consumption\QueueConsumerInterface;
Expand Down Expand Up @@ -143,9 +144,12 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
$consumer->bind($queue, $processor);
}

$consumer->consume($this->getRuntimeExtensions($input, $output));
$runtimeExtensionChain = $this->getRuntimeExtensions($input, $output);
$exitStatusExtension = new ExitStatusExtension();

return null;
$consumer->consume(new ChainExtension([$runtimeExtensionChain, $exitStatusExtension]));

return $exitStatusExtension->getExitStatus();
}

protected function getRuntimeExtensions(InputInterface $input, OutputInterface $output): ExtensionInterface
Expand Down
6 changes: 5 additions & 1 deletion pkg/enqueue/Symfony/Consumption/ConsumeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Enqueue\Symfony\Consumption;

use Enqueue\Consumption\ChainExtension;
use Enqueue\Consumption\Extension\ExitStatusExtension;
use Enqueue\Consumption\QueueConsumerInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
Expand Down Expand Up @@ -75,9 +76,12 @@ protected function execute(InputInterface $input, OutputInterface $output): ?int
array_unshift($extensions, $loggerExtension);
}

$exitStatusExtension = new ExitStatusExtension();
array_unshift($extensions, $exitStatusExtension);

$consumer->consume(new ChainExtension($extensions));

return null;
return $exitStatusExtension->getExitStatus();
}

private function getQueueConsumer(string $name): QueueConsumerInterface
Expand Down
26 changes: 25 additions & 1 deletion pkg/enqueue/Tests/Consumption/QueueConsumerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Enqueue\Consumption\Context\ProcessorException;
use Enqueue\Consumption\Context\Start;
use Enqueue\Consumption\Exception\InvalidArgumentException;
use Enqueue\Consumption\Extension\ExitStatusExtension;
use Enqueue\Consumption\ExtensionInterface;
use Enqueue\Consumption\QueueConsumer;
use Enqueue\Consumption\Result;
Expand Down Expand Up @@ -1429,6 +1430,29 @@ public function testShouldCallProcessorAsMessageComeAlong()
$this->assertSame($fooConsumerStub, $actualContexts[2]->getConsumer());
}

public function testCaptureExitStatus()
{
$testExitCode = 5;

$stubExtension = $this->createExtension();

$stubExtension
->expects($this->once())
->method('onStart')
->with($this->isInstanceOf(Start::class))
->willReturnCallback(function (Start $context) use ($testExitCode) {
$context->interruptExecution($testExitCode);
})
;

$exitExtension = new ExitStatusExtension();

$consumer = new QueueConsumer($this->createContextStub(), $stubExtension);
$consumer->consume(new ChainExtension([$exitExtension]));

$this->assertEquals($testExitCode, $exitExtension->getExitStatus());
}

/**
* @return \PHPUnit_Framework_MockObject_MockObject
*/
Expand Down Expand Up @@ -1508,7 +1532,7 @@ private function createExtension()
}

/**
* @param null|mixed $queue
* @param mixed|null $queue
*
* @return \PHPUnit_Framework_MockObject_MockObject|Consumer
*/
Expand Down
Loading