Skip to content

Commit

Permalink
Job statuses (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorprogger authored Feb 26, 2020
1 parent a812efc commit c849e34
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/DriverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Yiisoft\Yii\Queue;

use InvalidArgumentException;
use Yiisoft\Yii\Queue\Enum\JobStatus;
use Yiisoft\Yii\Queue\Jobs\DelayableJobInterface;
use Yiisoft\Yii\Queue\Jobs\JobInterface;
use Yiisoft\Yii\Queue\Jobs\PrioritisedJobInterface;
Expand All @@ -23,9 +25,11 @@ public function nextMessage(): ?MessageInterface;
*
* @param string $id of a job message
*
* @return int status code
* @return JobStatus
*
* @throws InvalidArgumentException when there is no such id in the driver
*/
public function status(string $id): int;
public function status(string $id): JobStatus;

/**
* Pushing a job to the queue
Expand Down
60 changes: 60 additions & 0 deletions src/Enum/JobStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Queue\Enum;

use InvalidArgumentException;

class JobStatus
{
public const WAITING = 1;
public const RESERVED = 2;
public const DONE = 3;

protected int $status;

protected function __construct(int $status)
{
if (!in_array($status, $this->available(), true)) {
throw new InvalidArgumentException('Invalid status provided');
}

$this->status = $status;
}

protected function available(): array
{
return [self::WAITING, self::RESERVED, self::DONE];
}

public static function waiting(): self
{
return new static(self::WAITING);
}

public static function reserved(): self
{
return new static(self::RESERVED);
}

public static function done(): self
{
return new static(self::DONE);
}

public function isWaiting(): bool
{
return $this->status === self::WAITING;
}

public function isReserved(): bool
{
return $this->status === self::RESERVED;
}

public function isDone(): bool
{
return $this->status === self::DONE;
}
}
14 changes: 14 additions & 0 deletions src/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Yiisoft\Yii\Queue;

use InvalidArgumentException;
use Psr\EventDispatcher\EventDispatcherInterface;
use Yiisoft\EventDispatcher\Provider\Provider;
use Yiisoft\Yii\Queue\Cli\LoopInterface;
use Yiisoft\Yii\Queue\Enum\JobStatus;
use Yiisoft\Yii\Queue\Events\AfterPush;
use Yiisoft\Yii\Queue\Events\BeforePush;
use Yiisoft\Yii\Queue\Events\JobFailure;
Expand Down Expand Up @@ -109,4 +111,16 @@ public function listen(): void

$this->driver->subscribe($handler);
}

/**
* @param string $id A job id
*
* @return JobStatus
*
* @throws InvalidArgumentException when there is no such id in the driver
*/
public function status(string $id): JobStatus
{
return $this->driver->status($id);
}
}

0 comments on commit c849e34

Please sign in to comment.