1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* YAWIK |
4
|
|
|
* |
5
|
|
|
* @filesource |
6
|
|
|
* @license MIT |
7
|
|
|
* @copyright 2013 - 2019 Cross Solution <http://cross-solution.de> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** */ |
11
|
|
|
namespace Core\Queue\Job; |
12
|
|
|
|
13
|
|
|
use SlmQueue\Job\AbstractJob; |
14
|
|
|
use SlmQueue\Worker\Event\ProcessJobEvent; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* ${CARET} |
18
|
|
|
* |
19
|
|
|
* @author Mathias Gelhausen <[email protected]> |
20
|
|
|
* @todo write test |
21
|
|
|
*/ |
22
|
|
|
abstract class MongoJob extends AbstractJob implements ResultProviderInterface |
23
|
|
|
{ |
24
|
|
|
protected $result; |
25
|
|
|
|
26
|
|
|
public static function create($payload = null) |
27
|
|
|
{ |
28
|
|
|
$class = new \ReflectionClass(static::class); |
29
|
|
|
$instance = $class->newInstanceWithoutConstructor(); |
30
|
|
|
|
31
|
|
|
if ($payload) { |
32
|
|
|
$payload = static::filterPayload($payload); |
33
|
|
|
$instance->setContent($payload); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $instance; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected static function filterPayload($payload) |
40
|
|
|
{ |
41
|
|
|
return $payload; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function setResult(JobResult $result) : void |
45
|
|
|
{ |
46
|
|
|
$this->result = $result; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getResult() : JobResult |
50
|
|
|
{ |
51
|
|
|
if (!$this->result) { |
52
|
|
|
$this->setResult(new JobResult(ProcessJobEvent::JOB_STATUS_UNKNOWN)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $this->result; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function failure(string $message, ?array $extra = null) : int |
59
|
|
|
{ |
60
|
|
|
$this->setResult(JobResult::failure($message, $extra)); |
61
|
|
|
|
62
|
|
|
return ProcessJobEvent::JOB_STATUS_FAILURE; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function recoverable(string $message, array $options = []) : int |
66
|
|
|
{ |
67
|
|
|
$this->setResult(JobResult::recoverable($message, $options)); |
68
|
|
|
|
69
|
|
|
return ProcessJobEvent::JOB_STATUS_FAILURE_RECOVERABLE; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function success(?string $message = null, ?array $extra = null) : int |
73
|
|
|
{ |
74
|
|
|
$this->setResult(JobResult::success($message, $extra)); |
75
|
|
|
|
76
|
|
|
return ProcessJobEvent::JOB_STATUS_SUCCESS; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|