-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cff3fea
commit 8876150
Showing
22 changed files
with
372 additions
and
114 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ jobs: | |
- windows-latest | ||
|
||
php-version: | ||
- "7.4" | ||
- "7.4.5" | ||
- "8.0" | ||
|
||
steps: | ||
|
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
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
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 |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\Queue\Driver; | ||
|
||
use InvalidArgumentException; | ||
use Yiisoft\Yii\Queue\Cli\LoopInterface; | ||
use Yiisoft\Yii\Queue\Enum\JobStatus; | ||
use Yiisoft\Yii\Queue\Job\DelayableJobInterface; | ||
use Yiisoft\Yii\Queue\Job\JobInterface; | ||
use Yiisoft\Yii\Queue\Job\PrioritisedJobInterface; | ||
use Yiisoft\Yii\Queue\Message; | ||
use Yiisoft\Yii\Queue\MessageInterface; | ||
use Yiisoft\Yii\Queue\Queue; | ||
use Yiisoft\Yii\Queue\QueueDependentInterface; | ||
use Yiisoft\Yii\Queue\Worker\WorkerInterface; | ||
|
||
final class SynchronousDriver implements DriverInterface, QueueDependentInterface | ||
{ | ||
private array $messages = []; | ||
private Queue $queue; | ||
private LoopInterface $loop; | ||
private WorkerInterface $worker; | ||
private int $current = 0; | ||
|
||
public function __construct(LoopInterface $loop, WorkerInterface $worker) | ||
{ | ||
$this->loop = $loop; | ||
$this->worker = $worker; | ||
} | ||
|
||
public function __destruct() | ||
{ | ||
$this->run([$this->worker, 'process']); | ||
} | ||
|
||
public function nextMessage(): ?MessageInterface | ||
{ | ||
$message = null; | ||
|
||
if (isset($this->messages[$this->current])) { | ||
$message = $this->messages[$this->current]; | ||
unset($this->messages[$this->current]); | ||
$this->current++; | ||
} | ||
|
||
return $message; | ||
} | ||
|
||
public function status(string $id): JobStatus | ||
{ | ||
$id = (int) $id; | ||
|
||
if ($id < 0) { | ||
throw new InvalidArgumentException('This driver ids starts with 0'); | ||
} | ||
|
||
if ($id < $this->current) { | ||
return JobStatus::done(); | ||
} | ||
|
||
if (isset($this->messages[$id])) { | ||
return JobStatus::waiting(); | ||
} | ||
|
||
throw new InvalidArgumentException('There is no job with the given id.'); | ||
} | ||
|
||
public function push(JobInterface $job): MessageInterface | ||
{ | ||
$key = count($this->messages) + $this->current; | ||
$message = new Message((string) $key, $job); | ||
$this->messages[] = $message; | ||
|
||
return $message; | ||
} | ||
|
||
public function subscribe(callable $handler): void | ||
{ | ||
$this->run($handler); | ||
} | ||
|
||
public function canPush(JobInterface $job): bool | ||
{ | ||
return !($job instanceof DelayableJobInterface || $job instanceof PrioritisedJobInterface); | ||
} | ||
|
||
public function setQueue(Queue $queue): void | ||
{ | ||
$this->queue = $queue; | ||
} | ||
|
||
private function run(callable $handler): void | ||
{ | ||
while ($this->loop->canContinue() && $message = $this->nextMessage()) { | ||
$handler($message, $this->queue); | ||
} | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\Queue\Tests\App; | ||
|
||
use Yiisoft\Yii\Queue\Job\DelayableJobInterface; | ||
|
||
class DelayableJob extends SimpleJob implements DelayableJobInterface | ||
{ | ||
public function getDelay(): int | ||
{ | ||
return 1; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\Queue\Tests\App; | ||
|
||
use Yiisoft\Yii\Queue\Event\AfterExecution; | ||
use Yiisoft\Yii\Queue\Event\AfterPush; | ||
use Yiisoft\Yii\Queue\Event\BeforeExecution; | ||
use Yiisoft\Yii\Queue\Event\BeforePush; | ||
use Yiisoft\Yii\Queue\Event\JobFailure; | ||
|
||
interface EventManager | ||
{ | ||
public function beforePushHandler(BeforePush $event); | ||
public function afterPushHandler(AfterPush $event); | ||
public function beforeExecutionHandler(BeforeExecution $event); | ||
public function afterExecutionHandler(AfterExecution $event); | ||
public function jobFailureHandler(JobFailure $event); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\Queue\Tests\App; | ||
|
||
use Yiisoft\Yii\Queue\Job\PrioritisedJobInterface; | ||
|
||
class PrioritizedJob extends SimpleJob implements PrioritisedJobInterface | ||
{ | ||
public function getPriority(): int | ||
{ | ||
return 1; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\Queue\Tests\App; | ||
|
||
use RuntimeException; | ||
use Yiisoft\Yii\Queue\Job\AttemptsRestrictedJob; | ||
|
||
class RetryableJob extends AttemptsRestrictedJob | ||
{ | ||
public bool $executed = false; | ||
|
||
public function __construct(int $attemptsMax = 2) | ||
{ | ||
$this->attemptsMax = $attemptsMax; | ||
} | ||
|
||
public function getTtr(): int | ||
{ | ||
return 1; | ||
} | ||
|
||
public function execute(): void | ||
{ | ||
if ($this->canRetry()) { | ||
throw new RuntimeException('Test exception'); | ||
} | ||
|
||
$this->executed = true; | ||
} | ||
} |
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
Oops, something went wrong.