Skip to content
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
27 changes: 27 additions & 0 deletions .github/workflows/rector-cs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Rector + PHP CS Fixer

on:
pull_request:
paths:
- 'migrations/**'
- 'src/**'
- 'tests/**'
- 'rector.php'
- '.php-cs-fixer.dist.php'
- 'composer.json'
- '.github/workflows/rector-cs.yml'

permissions:
contents: read

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
rector-cs:
permissions:
contents: write # Required to commit formatting fixes back to the PR
uses: yiisoft/actions/.github/workflows/rector-cs.yml@master
with:
php: '8.1'
23 changes: 0 additions & 23 deletions .github/workflows/rector.yml

This file was deleted.

23 changes: 23 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use PhpCsFixer\Finder;
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
use Yiisoft\CodeStyle\ConfigBuilder;

$finder = (new Finder())->in([
__DIR__ . '/migrations',
__DIR__ . '/src',
__DIR__ . '/tests',
]);

return ConfigBuilder::build()
->setCacheFile(__DIR__ . '/runtime/.php-cs-fixer.cache')
->setRiskyAllowed(true)
->setParallelConfig(ParallelConfigFactory::detect())
->setRules([
'@Yiisoft/Core' => true,
'@Yiisoft/Core:risky' => true,
])
->setFinder($finder);
85 changes: 0 additions & 85 deletions .styleci.yml

This file was deleted.

6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@
"yiisoft/queue": "dev-master"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.95",
"maglnet/composer-require-checker": "^4.7",
"phpunit/phpunit": "^10.5",
"rector/rector": "^2.0.3",
"roave/infection-static-analysis-plugin": "^1.34",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^5.20",
"yiisoft/cache": "^3.2",
"yiisoft/code-style": "^1.1",
"yiisoft/db-sqlite": "^2.0"
},
"suggest": {
Expand Down Expand Up @@ -78,6 +80,8 @@
},
"scripts": {
"test": "phpunit --testdox",
"test-watch": "phpunit-watcher watch"
"test-watch": "phpunit-watcher watch",
"rector": "rector",
"cs-fix": "php-cs-fixer fix"
}
}
30 changes: 8 additions & 22 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,16 @@

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
use Rector\Php81\Rector\Property\ReadOnlyPropertyRector;
use Rector\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector;
use Rector\Set\ValueObject\LevelSetList;
use Yiisoft\CodeStyle\Rector\SetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
return RectorConfig::configure()
->withPaths([
__DIR__ . '/migrations',
__DIR__ . '/src',
__DIR__ . '/tests',
])
->withPhpSets(php81: true)
->withSets([
SetList::YII_CORE,
]);

// register a single rule
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);

// define sets of rules
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_81,
]);

$rectorConfig->skip([
ClosureToArrowFunctionRector::class,
ReadOnlyPropertyRector::class,
NullToStrictStringFuncCallArgRector::class,
]);
};
2 changes: 2 additions & 0 deletions runtime/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
79 changes: 43 additions & 36 deletions src/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
use Yiisoft\Mutex\MutexFactoryInterface;
use Yiisoft\Mutex\MutexInterface;
use Yiisoft\Queue\Provider\QueueProviderInterface;
use Exception;

use function is_array;
use function is_resource;
use function is_string;

use const SORT_ASC;

final class Adapter implements AdapterInterface
{
Expand All @@ -39,6 +46,11 @@ final class Adapter implements AdapterInterface
*/
public $deleteReleased = true;

/**
* @var int reserve time
*/
private $reserveTime = 0;

public function __construct(
private ConnectionInterface $db,
private MessageSerializerInterface $serializer,
Expand Down Expand Up @@ -123,17 +135,42 @@ public function withChannel(BackedEnum|string $channel): self
return $new;
}

/**
* Listens queue and runs each job.
*
* @param callable(MessageInterface): bool $handlerCallback The handler which will handle messages. Returns false if it cannot continue handling messages
* @param bool $repeat whether to continue listening when queue is empty.
* @param non-negative-int $timeout number of seconds to sleep before next iteration.
*/
public function run(callable $handlerCallback, bool $repeat, int $timeout = 0): void
{
while ($this->loop->canContinue()) {
if ($payload = $this->reserve()) {
if ($handlerCallback($this->serializer->unserialize($payload['job']))) {
$this->release($payload);
}
continue;
}
if (!$repeat) {
break;
}
if ($timeout > 0) {
sleep($timeout);
}
}
}

/**
* Takes one message from waiting list and reserves it for handling.
*
* @throws \Exception in case it hasn't waited the lock
* @throws Exception in case it hasn't waited the lock
* @return array|null payload
*/
protected function reserve(): array|null
protected function reserve(): ?array
{
// TWK TODO what is useMaster in Yii3 return $this->db->useMaster(function () {
if (!$this->mutex->acquire($this->mutexTimeout)) {
throw new \Exception('Has not waited the lock.');
throw new Exception('Has not waited the lock.');
}

try {
Expand Down Expand Up @@ -182,13 +219,13 @@ protected function release($payload): void
if ($this->deleteReleased) {
$this->db->createCommand()->delete(
$this->tableName,
['id' => $payload['id']]
['id' => $payload['id']],
)->execute();
} else {
$this->db->createCommand()->update(
$this->tableName,
['done_at' => time()],
['id' => $payload['id']]
['id' => $payload['id']],
)->execute();
}
}
Expand All @@ -205,38 +242,8 @@ private function moveExpired(): void
['reserved_at' => null],
'[[reserved_at]] < :time - [[ttr]] and [[reserved_at]] is not null and [[done_at]] is null',
null,
[':time' => $this->reserveTime]
[':time' => $this->reserveTime],
)->execute();
}
}

/**
* @var int reserve time
*/
private $reserveTime = 0;

/**
* Listens queue and runs each job.
*
* @param callable(MessageInterface): bool $handlerCallback The handler which will handle messages. Returns false if it cannot continue handling messages
* @param bool $repeat whether to continue listening when queue is empty.
* @param non-negative-int $timeout number of seconds to sleep before next iteration.
*/
public function run(callable $handlerCallback, bool $repeat, int $timeout = 0): void
{
while ($this->loop->canContinue()) {
if ($payload = $this->reserve()) {
if ($handlerCallback($this->serializer->unserialize($payload['job']))) {
$this->release($payload);
}
continue;
}
if (!$repeat) {
break;
}
if ($timeout > 0) {
sleep($timeout);
}
}
}
}
2 changes: 2 additions & 0 deletions tests/AdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use Yiisoft\Queue\Message\Serializer\MessageSerializerInterface;
use Yiisoft\Queue\Provider\QueueProviderInterface;

use function in_array;

final class AdapterTest extends TestCase
{
public function testPushUsesMessageMetaAndReturnsIdEnvelope(): void
Expand Down
Loading