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
Showing
11 changed files
with
624 additions
and
68 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
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,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(), | ||
]); | ||
} | ||
|
||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} |
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,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; | ||
} | ||
} |
Oops, something went wrong.