Skip to content

Commit

Permalink
Queue: Improve result handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
TiSiE committed Feb 13, 2019
1 parent 488b095 commit 4fb2617
Show file tree
Hide file tree
Showing 11 changed files with 624 additions and 68 deletions.
1 change: 1 addition & 0 deletions module/Core/config/module.config.php
Expand Up @@ -33,6 +33,7 @@
'worker_strategies' => [
'default' => [
Queue\Strategy\IdleSleepStrategy::class => ['duration' => 1],
Queue\Strategy\JobResultStrategy::class,
],
'queues' => [
'default' => [
Expand Down
29 changes: 29 additions & 0 deletions module/Core/src/Queue/Job/ExceptionJobResult.php
@@ -0,0 +1,29 @@
<?php
/**
* YAWIK
*
* @filesource
* @license MIT
* @copyright 2013 - 2019 Cross Solution <http://cross-solution.de>
*/

/** */
namespace Core\Queue\Job;

/**
* ${CARET}
*
* @author Mathias Gelhausen <gelhausen@cross-solution.de>
* @todo write test
*/
class ExceptionJobResult extends JobResult
{
public function __construct(\Exception $e)
{
parent::__construct([
'message' => $e->getMessage(),
'extra' => $e->getTrace(),
]);
}

}
193 changes: 193 additions & 0 deletions module/Core/src/Queue/Job/JobResult.php
@@ -0,0 +1,193 @@
<?php
/**
* YAWIK
*
* @filesource
* @license MIT
* @copyright 2013 - 2019 Cross Solution <http://cross-solution.de>
*/

declare(strict_types=1);

/** */
namespace Core\Queue\Job;

use SlmQueue\Worker\Event\ProcessJobEvent;

/**
* ${CARET}
*
* @author Mathias Gelhausen <gelhausen@cross-solution.de>
* @todo write test
*/
class JobResult
{
/**
*
*
* @var int
*/
protected $result;

/**
*
*
* @var string
*/
protected $reason;

/**
*
*
* @var array
*/
protected $extra;

/**
*
*
* @var string|int|\DateInterval
*/
protected $delay;

/**
*
*
* @var string|int|\DateTime
*/
protected $scheduled;

public static function success(?string $reason = null, ?array $extra = null) : self
{
return (new static(ProcessJobEvent::JOB_STATUS_SUCCESS))
->withReason($reason)
->withExtra($extra)
;
}

public static function failure(string $reason, ?array $extra = null) : self
{
return (new static(ProcessJobEvent::JOB_STATUS_FAILURE))
->withReason($reason)
->withExtra($extra)
;
}

public static function recoverable(string $reason, array $options = []) : self
{
$result = (new static(ProcessJobEvent::JOB_STATUS_FAILURE_RECOVERABLE))
->withReason($reason);

foreach ($options as $key => $value) {
$callback = [$result, "with$key"];
if (is_callable($callback)) {
$callback($value);
}
}

return $result;
}

public function __construct(int $result)
{
$this->result = $result;
}

public function getResult() : int
{
return $this->result;
}

public function isSuccess() : bool
{
return ProcessJobEvent::JOB_STATUS_SUCCESS == $this->result;
}

public function isFailure() : bool
{
return ProcessJobEvent::JOB_STATUS_FAILURE == $this->result;
}

public function isRecoverable() : bool
{
return ProcessJobEvent::JOB_STATUS_FAILURE_RECOVERABLE == $this->result;
}

/**
* @return string
*/
public function getReason(): ?string
{
return $this->reason;
}

/**
* @param string $message
*
* @return self
*/
public function withReason($reason) : self
{
$this->reason = $reason;

return $this;
}

/**
* @return array
*/
public function getExtra(): ?array
{
return $this->extra;
}

/**
* @param array $extra
*
* @return self
*/
public function withExtra(array $extra) : self
{
$this->extra = $extra;

return $this;
}

/**
* @return \DateInterval|int|string
*/
public function getDelay()
{
return $this->delay;
}

/**
* @param \DateInterval|int|string $delay
*
* @return self
*/
public function withDelay($delay) : self
{
$this->delay = $delay;

return $this;
}

/**
* @return \DateTime|int|string
*/
public function getDate()
{
return $this->scheduled;
}

/**
* @param \DateTime|int|string $scheduled
*
* @return self
*/
public function withDate($scheduled) : self
{
$this->scheduled = $scheduled;
}
}
60 changes: 60 additions & 0 deletions module/Core/src/Queue/Job/MongoJob.php
@@ -0,0 +1,60 @@
<?php
/**
* YAWIK
*
* @filesource
* @license MIT
* @copyright 2013 - 2019 Cross Solution <http://cross-solution.de>
*/

/** */
namespace Core\Queue\Job;

use SlmQueue\Job\AbstractJob;
use SlmQueue\Worker\Event\ProcessJobEvent;

/**
* ${CARET}
*
* @author Mathias Gelhausen <gelhausen@cross-solution.de>
* @todo write test
*/
abstract class MongoJob extends AbstractJob implements ResultProviderInterface
{
protected $result;

public function setResult(JobResult $result) : void
{
$this->result = $result;
}

public function getResult() : JobResult
{
if (!$this->result) {
$this->setResult(new JobResult(ProcessJobEvent::JOB_STATUS_UNKNOWN));
}

return $this->result;
}

protected function failure(string $message, ?array $extra = null) : int
{
$this->setResult(JobResult::failure($message, $extra));

return ProcessJobEvent::JOB_STATUS_FAILURE;
}

protected function recoverable(string $message, array $options = []) : int
{
$this->setResult(JobResult::recoverable($message, $options));

return ProcessJobEvent::JOB_STATUS_FAILURE_RECOVERABLE;
}

protected function success(?string $message = null, ?array $extra = null) : int
{
$this->setResult(JobResult::success($message, $extra));

return ProcessJobEvent::JOB_STATUS_SUCCESS;
}
}
25 changes: 25 additions & 0 deletions module/Core/src/Queue/Job/ResultProviderInterface.php
@@ -0,0 +1,25 @@
<?php
/**
* YAWIK
*
* @filesource
* @license MIT
* @copyright 2013 - 2019 Cross Solution <http://cross-solution.de>
*/

declare(strict_types=1);

/** */
namespace Core\Queue\Job;

/**
* ${CARET}
*
* @author Mathias Gelhausen <gelhausen@cross-solution.de>
* @todo write test
*/
interface ResultProviderInterface
{
public function setResult(JobResult $error) : void;
public function getResult() : JobResult;
}
67 changes: 67 additions & 0 deletions module/Core/src/Queue/LoggerAwareJobTrait.php
@@ -0,0 +1,67 @@
<?php
/**
* YAWIK
*
* @filesource
* @license MIT
* @copyright 2013 - 2019 Cross Solution <http://cross-solution.de>
*/

declare(strict_types=1);

/** */
namespace Core\Queue;

use Zend\Log\LoggerInterface;

/**
* Trait implementing LoggerAwareInterface.
*
* @author Mathias Gelhausen <gelhausen@cross-solution.de>
* @todo write test
*/
trait LoggerAwareJobTrait
{
/**
*
*
* @var LoggerInterface
*/
private $logger;

/**
* Set the logger instance
*
* @param LoggerInterface $logger
*/
public function setLogger(LoggerInterface $logger) : void
{
$this->logger = $logger;
}

/**
* Get the logger instance.
*
* If no logger is set, it will create and return a null logger.
*
* @return LoggerInterface
*/
public function getLogger() : LoggerInterface
{
if (!$this->logger) {
$this->logger = new class implements LoggerInterface
{
public function emerg($message, $extra = []) {}
public function alert($message, $extra = []) {}
public function crit($message, $extra = []) {}
public function err($message, $extra = []) {}
public function warn($message, $extra = []) {}
public function notice($message, $extra = []) {}
public function info($message, $extra = []) {}
public function debug($message, $extra = []) {}
};
}

return $this->logger;
}
}

0 comments on commit 4fb2617

Please sign in to comment.