Skip to content

feat: add concurrency manager #24

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
60 changes: 30 additions & 30 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
<testsuite name="E2E">
<directory>./tests/Queue/E2E/Adapter</directory>
</testsuite>
<testsuite name="Unit">
<directory>./tests/Queue/Unit</directory>
</testsuite>
</testsuites>
</phpunit>
3 changes: 2 additions & 1 deletion src/Queue/Adapter/Swoole/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
use Swoole\Database\RedisConfig;
use Swoole\Database\RedisPool;
use Utopia\Queue\Connection\Redis as ConnectionRedis;
use Utopia\Queue\Concurrency\Adapter;

class Redis extends ConnectionRedis
class Redis extends ConnectionRedis implements Adapter
{
protected RedisPool $pool;

Expand Down
11 changes: 11 additions & 0 deletions src/Queue/Concurrency/Adapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Utopia\Queue\Concurrency;

interface Adapter
{
public function get(string $key): ?string;
public function set(string $key, string $value): void;
public function increment(string $key): int;
public function decrement(string $key): int;
}
37 changes: 37 additions & 0 deletions src/Queue/Concurrency/Manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Utopia\Queue\Concurrency;

use Utopia\Queue\Message;

abstract class Manager
{
public function __construct(protected Adapter $adapter, protected int $limit = 1)
{
}

public function canProcessJob(Message $message): bool
{
$key = $this->getConcurrencyKey($message);
$value = $this->adapter->get($key);
if ($value === null) {
$this->adapter->set($key, "0");
$value = 0;
}
return \intval($value) < $this->limit;
}

public function startJob(Message $message): void
{
$key = $this->getConcurrencyKey($message);
$this->adapter->increment($key);
}

public function finishJob(Message $message): void
{
$key = $this->getConcurrencyKey($message);
$this->adapter->decrement($key);
}

abstract public function getConcurrencyKey(Message $message): string;
}
43 changes: 28 additions & 15 deletions src/Queue/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Utopia\DI\Container;
use Utopia\DI\Dependency;
use Utopia\Servers\Base;
use Utopia\Queue\Concurrency\Manager;

class Worker extends Base
{
Expand Down Expand Up @@ -146,6 +147,20 @@ public function start(): self
return $this;
}

protected ?Manager $concurrencyManager = null;

/**
* Set the concurrency manager
*
* @param Manager $manager
* @return self
*/
public function setConcurrencyManager(Manager $manager): self
{
$this->concurrencyManager = $manager;
return $this;
}

protected function lifecycle(Job $job, Message $message, array $nextMessage, Container $context, Connection $connection): static
{
Console::info("[Job] Received Job ({$message->getPid()}).");
Expand All @@ -154,21 +169,15 @@ protected function lifecycle(Job $job, Message $message, array $nextMessage, Con

$connection->getConnection();

/**
* Move Job to Jobs and it's PID to the processing list.
*/
$connection->setArray("{$this->adapter->namespace}.jobs.{$this->adapter->queue}.{$message->getPid()}", $nextMessage);
$connection->leftPush("{$this->adapter->namespace}.processing.{$this->adapter->queue}", $message->getPid());

/**
* Increment Total Jobs Received from Stats.
*/
$connection->increment("{$this->adapter->namespace}.stats.{$this->adapter->queue}.total");

/**
* Increment Processing Jobs from Stats.
*/
$connection->increment("{$this->adapter->namespace}.stats.{$this->adapter->queue}.processing");
// if ($this->concurrencyManager) {
// if (!$this->concurrencyManager->canProcessJob($message)) {
// // If we can't process the job due to concurrency limits, put it back in the queue
// $connection->leftPush("{$this->adapter->namespace}.queue.{$this->adapter->queue}", json_encode($nextMessage));
// Console::info("[Job] ({$message->getPid()}) postponed due to concurrency limits.");
// return $this;
// }
// $this->concurrencyManager->startJob($message);
// }

try {
foreach (self::$init as $hook) { // Global init hooks
Expand Down Expand Up @@ -257,6 +266,10 @@ protected function lifecycle(Job $job, Message $message, array $nextMessage, Con
}
}
} finally {
// if ($this->concurrencyManager) {
// $this->concurrencyManager->finishJob($message);
// }

/**
* Remove Job from Processing.
*/
Expand Down
Loading
Loading