generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
Add Config and Migration plus review changes #13
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
b7e4a2d
Add Config and Migration, Update Adapter.php
aphraoh b5fe7aa
Update composer.json
aphraoh dd25305
Update Adapter.php
aphraoh c30e377
Update Adapter.php
aphraoh 3655908
Update M240327150317Queue.php
aphraoh 63bdc76
Update composer.json after review
aphraoh 7fd3788
Use LoopInterface
aphraoh 219b3ea
Use MutexInterface and MutexFactoryInterface
aphraoh 2757ef1
Update src/Adapter.php
aphraoh 98d1613
Update composer.json
aphraoh 5c917f1
Update composer.json
aphraoh dfffc04
Update Adapter.php
aphraoh 7e05e5f
Update composer.json
aphraoh 6c8f0dd
Update composer.json due to invalid json
aphraoh 2d04ff1
Delete config directory due to empty di
aphraoh 70a3798
Update src/Adapter.php
aphraoh 992d64c
Delete src/Migrations directory
aphraoh 1a9e228
Update composer.json
aphraoh c2b137c
Update Adapter.php
aphraoh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,43 +6,50 @@ | |
|
||
use InvalidArgumentException; | ||
use Yiisoft\Queue\Adapter\AdapterInterface; | ||
use Yiisoft\Queue\Cli\LoopInterface; | ||
use Yiisoft\Queue\Enum\JobStatus; | ||
use Yiisoft\Queue\Message\MessageInterface; | ||
use Yiisoft\Queue\Message\MessageSerializerInterface; | ||
use Yiisoft\Queue\QueueFactory; | ||
use Yiisoft\Queue\Message\IdEnvelope; | ||
use Yiisoft\Db\Connection\ConnectionInterface; | ||
use Yiisoft\Db\Query\Query; | ||
use Yiisoft\Mutex\MutexFactoryInterface; | ||
use Yiisoft\Mutex\MutexInterface; | ||
|
||
final class Adapter implements AdapterInterface | ||
{ | ||
/** | ||
* @var int timeout | ||
* @var MutexInterface Mutex interface. | ||
*/ | ||
public MutexInterface $mutex; | ||
/** | ||
* @var int Mutex timeout. | ||
*/ | ||
public $mutexTimeout = 3; | ||
/** | ||
* @var string table name | ||
* @var string Table name. | ||
*/ | ||
public $tableName = '{{%queue}}'; | ||
/** | ||
* @var bool ability to delete released messages from table | ||
* @var bool Ability to delete released messages from table. | ||
*/ | ||
public $deleteReleased = true; | ||
|
||
|
||
public function __construct( | ||
private ConnectionInterface $db, | ||
private MessageSerializerInterface $serializer, | ||
private LoopInterface $loop, | ||
private MutexFactoryInterface $mutexFactory, | ||
private string $channel = QueueFactory::DEFAULT_CHANNEL_NAME, | ||
) { | ||
$this->mutex = $this->mutexFactory->create(__CLASS__ . $this->channel); | ||
} | ||
|
||
public function runExisting(callable $handlerCallback): void | ||
{ | ||
$result = true; | ||
while (($payload = $this->reserve()) && ($result === true)) { | ||
if ($result = $handlerCallback(\unserialize($payload['job']))) { | ||
$this->release($payload); | ||
} | ||
} | ||
$this->run($handlerCallback, false); | ||
} | ||
|
||
public function status(string|int $id): JobStatus | ||
|
@@ -78,7 +85,7 @@ | |
$metadata = $message->getMetadata(); | ||
$this->db->createCommand()->insert($this->tableName, [ | ||
'channel' => $this->channel, | ||
'job' => \serialize($message), | ||
'job' => $this->serializer->serialize($message), | ||
'pushed_at' => time(), | ||
'ttr' => $metadata['ttr'] ?? 300, | ||
'delay' => $metadata['delay'] ?? 0, | ||
|
@@ -92,7 +99,7 @@ | |
|
||
public function subscribe(callable $handlerCallback): void | ||
{ | ||
$this->runExisting($handlerCallback); | ||
$this->run($handlerCallback, true, 5); // TWK TODO timeout should not be hard coded | ||
} | ||
|
||
public function withChannel(string $channel): self | ||
|
@@ -103,7 +110,8 @@ | |
|
||
$new = clone $this; | ||
$new->channel = $channel; | ||
|
||
$new->mutex = $this->mutexFactory->create(__CLASS__ . $this->channel); | ||
|
||
return $new; | ||
} | ||
|
||
|
@@ -115,10 +123,10 @@ | |
*/ | ||
protected function reserve(): array|null | ||
{ | ||
// TWK TODO ??? return $this->db->useMaster(function () { | ||
// TWK TODO ??? if (!$this->mutex->acquire(__CLASS__ . $this->channel, $this->mutexTimeout)) { | ||
// TWK TODO ??? throw new \Exception('Has not waited the lock.'); | ||
// TWK TODO ??? } | ||
// TWK TODO what is useMaster in Yii3 return $this->db->useMaster(function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if (!$this->mutex->acquire($this->mutexTimeout)) { | ||
throw new \Exception('Has not waited the lock.'); | ||
} | ||
|
||
try { | ||
$this->moveExpired(); | ||
|
@@ -147,7 +155,7 @@ | |
} | ||
} | ||
} finally { | ||
// TWK TODO ??? $this->mutex->release(__CLASS__ . $this->channel); | ||
$this->mutex->release(); | ||
} | ||
|
||
return $payload; | ||
|
@@ -194,4 +202,28 @@ | |
*/ | ||
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); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you plan to resolve it before merge?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can I do it after the merge, I believe the solution should be somewhere in the middleware but need to investigate a bit further so maybe that can be a lession for another day after the merge...