|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace eXpansion\Framework\Core\Helpers\JobRunner; |
|
4
|
|
|
|
|
5
|
|
|
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpApplication; |
|
6
|
|
|
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpTimer; |
|
7
|
|
|
use eXpansion\Framework\Core\Helpers\Structures\HttpRequest; |
|
8
|
|
|
use eXpansion\Framework\Core\Helpers\Structures\HttpResult; |
|
9
|
|
|
use eXpansion\Framework\Core\Services\Console; |
|
10
|
|
|
use oliverde8\AsynchronousJobs\Job; |
|
11
|
|
|
use oliverde8\AsynchronousJobs\Job\CallbackCurl; |
|
12
|
|
|
use oliverde8\AsynchronousJobs\JobRunner; |
|
13
|
|
|
use Psr\Log\LoggerInterface; |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Factory |
|
18
|
|
|
* |
|
19
|
|
|
* @author de Cramer Oliver<[email protected]> |
|
20
|
|
|
* @copyright 2017 Smile |
|
21
|
|
|
* @package Tests\eXpansion\Framework\Core\Helpers\JobRunner |
|
22
|
|
|
*/ |
|
23
|
|
|
class Factory implements ListenerInterfaceExpTimer |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var LoggerInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $logger; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var Console |
|
33
|
|
|
*/ |
|
34
|
|
|
private $console; |
|
35
|
|
|
|
|
36
|
2 |
|
public function __construct(LoggerInterface $logger, Console $console) |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
2 |
|
$this->logger = $logger; |
|
40
|
2 |
|
$this->console = $console; |
|
41
|
2 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return JobRunner |
|
45
|
|
|
*/ |
|
46
|
2 |
|
public function getJobRunner() |
|
47
|
|
|
{ |
|
48
|
|
|
try { |
|
49
|
2 |
|
return JobRunner::getInstance('expansion', PHP_BINARY, 'var/tmp/asynchronous/'); |
|
50
|
|
|
} catch (\Exception $ex) { |
|
51
|
|
|
$this->console->writeln('PHP exec is not enabled, therefore all http transport is disabled!'); |
|
52
|
|
|
$this->logger->critical('PHP exec is not enabled, therefore all http transport is disabled!'); |
|
53
|
|
|
exit(1); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param $url |
|
59
|
|
|
* @param $callback |
|
60
|
|
|
* @param null|mixed $additionalData |
|
61
|
|
|
* @param array $options |
|
62
|
|
|
* |
|
63
|
|
|
* @param array|\stdClass $parameters one dimensional array or \stdClass with post key-value pairs |
|
64
|
|
|
* @return CallbackCurl |
|
65
|
|
|
*/ |
|
66
|
1 |
|
public function createCurlJob($url, $callback, $additionalData = null, $options = [], $parameters = []) |
|
67
|
|
|
{ |
|
68
|
1 |
|
$curlJob = new HttpRequest(); |
|
69
|
1 |
|
$curlJob->setCallback($callback); |
|
70
|
1 |
|
$curlJob->setUrl($url); |
|
71
|
1 |
|
$curlJob->setOptions($options); |
|
72
|
1 |
|
if (is_object($parameters)) { |
|
73
|
|
|
$parameters = (array)$parameters; |
|
74
|
|
|
} |
|
75
|
1 |
|
$curlJob->setParameters($parameters); |
|
76
|
1 |
|
$curlJob->setAdditionalData($additionalData); |
|
77
|
|
|
|
|
78
|
1 |
|
return $curlJob; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param Job $job |
|
83
|
|
|
*/ |
|
84
|
1 |
|
public function startJob(Job $job) |
|
85
|
|
|
{ |
|
86
|
1 |
|
$job->start(); |
|
87
|
1 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
public function onPreLoop() |
|
91
|
|
|
{ |
|
92
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
public function onPostLoop() |
|
96
|
|
|
{ |
|
97
|
1 |
|
$this->getJobRunner()->proccess(); |
|
98
|
1 |
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function onEverySecond() |
|
101
|
|
|
{ |
|
102
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exitexpression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.