Skip to content
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

Make app work #3

Merged
merged 1 commit into from
May 20, 2024
Merged
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
37 changes: 30 additions & 7 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Mammatus\Queue;

use Interop\Queue\Consumer;
use Interop\Queue\Context;
use Mammatus\LifeCycleEvents\Shutdown;
use Mammatus\Queue\Contracts\Worker;
use Mammatus\Queue\Contracts\Worker as WorkerContract;
use Mammatus\Queue\Generated\AbstractList_;
Expand All @@ -12,21 +15,30 @@
use React\EventLoop\Loop;
use RuntimeException;
use Throwable;
use WyriHaximus\Broadcast\Contracts\Listener;
use WyriHaximus\PSR3\ContextLogger\ContextLogger;

use function React\Async\async;
use function React\Async\await;
use function React\Promise\all;
use function WyriHaximus\React\futurePromise;

final class App extends AbstractList_
final class App extends AbstractList_ implements Listener
{
private bool $running = true;

public function __construct(
private readonly ContainerInterface $container,
private readonly Context $context,
private readonly LoggerInterface $logger,
) {
}

public function stop(Shutdown $event): void
{
$this->running = false;
}

public function run(string $className): int
{
$promises = [];
Expand All @@ -35,22 +47,33 @@
continue;
}

for ($i = 0; $i < $worker->concurrency; $i++) {
$this->logger->info('Starting consumer ' . $i . ' of ' . $worker->concurrency . ' for ' . $worker->class);
$promises[] = async(fn () => $this->consume($worker))();
}
$promises[] = async(fn () => $this->setupConsumer($worker))();
}

await(all($promises));

return 0;
}

private function consume(\Mammatus\Queue\Worker $worker): void
private function setupConsumer(\Mammatus\Queue\Worker $worker): int
{
$consumer = $this->context->createConsumer(new Queue($worker->queue));
$workerInstance = $this->container->get($worker->class);
assert($workerInstance instanceof WorkerContract);

$promises = [];
for ($i = 0; $i < $worker->concurrency; $i++) {
$this->logger->info('Starting consumer ' . $i . ' of ' . $worker->concurrency . ' for ' . $worker->class);
$promises[] = async(fn () => $this->consume($consumer, $workerInstance))();
}

await(all($promises));

return 0;
}

private function consume(Consumer $consumer, WorkerContract $worker): void
{
while ($this->running) {
$message = $consumer->receiveNoWait();
if ($message === null) {
Expand All @@ -59,9 +82,9 @@
}

try {
$workerInstance->perform($message);
$worker->perform($message);
$consumer->acknowledge($message);
} catch (Throwable) {

Check failure on line 87 in src/App.php

View workflow job for this annotation

GitHub Actions / Continuous Integration / Continuous Integration / Run stan on PHP 8.2 (nts) with highest dependency preference

In method "Mammatus\Queue\App::consume", caught "Throwable" must be rethrown. Either catch a more specific exception or add a "throw" clause in the "catch" block to propagate the exception. More info: http://bit.ly/failloud

Check failure on line 87 in src/App.php

View workflow job for this annotation

GitHub Actions / Continuous Integration / Continuous Integration / Run stan on PHP 8.2 (nts) with locked dependency preference

In method "Mammatus\Queue\App::consume", caught "Throwable" must be rethrown. Either catch a more specific exception or add a "throw" clause in the "catch" block to propagate the exception. More info: http://bit.ly/failloud

Check failure on line 87 in src/App.php

View workflow job for this annotation

GitHub Actions / Continuous Integration / Continuous Integration / Run stan on PHP 8.2 (zts) with highest dependency preference

In method "Mammatus\Queue\App::consume", caught "Throwable" must be rethrown. Either catch a more specific exception or add a "throw" clause in the "catch" block to propagate the exception. More info: http://bit.ly/failloud

Check failure on line 87 in src/App.php

View workflow job for this annotation

GitHub Actions / Continuous Integration / Continuous Integration / Run stan on PHP 8.2 (nts) with lowest dependency preference

In method "Mammatus\Queue\App::consume", caught "Throwable" must be rethrown. Either catch a more specific exception or add a "throw" clause in the "catch" block to propagate the exception. More info: http://bit.ly/failloud

Check failure on line 87 in src/App.php

View workflow job for this annotation

GitHub Actions / Continuous Integration / Continuous Integration / Run stan on PHP 8.2 (zts) with locked dependency preference

In method "Mammatus\Queue\App::consume", caught "Throwable" must be rethrown. Either catch a more specific exception or add a "throw" clause in the "catch" block to propagate the exception. More info: http://bit.ly/failloud

Check failure on line 87 in src/App.php

View workflow job for this annotation

GitHub Actions / Continuous Integration / Continuous Integration / Run stan on PHP 8.3 (nts) with highest dependency preference

In method "Mammatus\Queue\App::consume", caught "Throwable" must be rethrown. Either catch a more specific exception or add a "throw" clause in the "catch" block to propagate the exception. More info: http://bit.ly/failloud

Check failure on line 87 in src/App.php

View workflow job for this annotation

GitHub Actions / Continuous Integration / Continuous Integration / Run stan on PHP 8.2 (zts) with lowest dependency preference

In method "Mammatus\Queue\App::consume", caught "Throwable" must be rethrown. Either catch a more specific exception or add a "throw" clause in the "catch" block to propagate the exception. More info: http://bit.ly/failloud

Check failure on line 87 in src/App.php

View workflow job for this annotation

GitHub Actions / Continuous Integration / Continuous Integration / Run stan on PHP 8.3 (nts) with locked dependency preference

In method "Mammatus\Queue\App::consume", caught "Throwable" must be rethrown. Either catch a more specific exception or add a "throw" clause in the "catch" block to propagate the exception. More info: http://bit.ly/failloud

Check failure on line 87 in src/App.php

View workflow job for this annotation

GitHub Actions / Continuous Integration / Continuous Integration / Run stan on PHP 8.3 (nts) with lowest dependency preference

In method "Mammatus\Queue\App::consume", caught "Throwable" must be rethrown. Either catch a more specific exception or add a "throw" clause in the "catch" block to propagate the exception. More info: http://bit.ly/failloud

Check failure on line 87 in src/App.php

View workflow job for this annotation

GitHub Actions / Continuous Integration / Continuous Integration / Run stan on PHP 8.3 (zts) with highest dependency preference

In method "Mammatus\Queue\App::consume", caught "Throwable" must be rethrown. Either catch a more specific exception or add a "throw" clause in the "catch" block to propagate the exception. More info: http://bit.ly/failloud

Check failure on line 87 in src/App.php

View workflow job for this annotation

GitHub Actions / Continuous Integration / Continuous Integration / Run stan on PHP 8.3 (zts) with locked dependency preference

In method "Mammatus\Queue\App::consume", caught "Throwable" must be rethrown. Either catch a more specific exception or add a "throw" clause in the "catch" block to propagate the exception. More info: http://bit.ly/failloud

Check failure on line 87 in src/App.php

View workflow job for this annotation

GitHub Actions / Continuous Integration / Continuous Integration / Run stan on PHP 8.3 (zts) with lowest dependency preference

In method "Mammatus\Queue\App::consume", caught "Throwable" must be rethrown. Either catch a more specific exception or add a "throw" clause in the "catch" block to propagate the exception. More info: http://bit.ly/failloud
$consumer->reject($message);
}
}
Expand Down
Loading