1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package: chapi |
4
|
|
|
* |
5
|
|
|
* @author: msiebeneicher |
6
|
|
|
* @since: 2015-07-28 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Chapi\Component\RemoteClients; |
12
|
|
|
|
13
|
|
|
use Chapi\Component\RemoteClients\ApiClientInterface; |
14
|
|
|
use Chapi\Component\Http\HttpClientInterface; |
15
|
|
|
use Chapi\Entity\Chronos\ChronosJobEntity; |
16
|
|
|
use Chapi\Entity\JobEntityInterface; |
17
|
|
|
use Chapi\Exception\ApiClientException; |
18
|
|
|
use Chapi\Exception\HttpConnectionException; |
19
|
|
|
use Symfony\Component\Config\Definition\Exception\Exception; |
20
|
|
|
|
21
|
|
|
class ChronosApiClient implements ApiClientInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var HttpClientInterface |
25
|
|
|
*/ |
26
|
|
|
private $httpClient; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param HttpClientInterface $httpClient |
30
|
|
|
*/ |
31
|
21 |
|
public function __construct( |
32
|
|
|
HttpClientInterface $httpClient |
33
|
|
|
) { |
34
|
21 |
|
$this->httpClient = $httpClient; |
35
|
21 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritdoc |
39
|
|
|
* @link: https://mesos.github.io/chronos/docs/api.html#listing-jobs |
40
|
|
|
*/ |
41
|
2 |
|
public function listingJobs() |
42
|
|
|
{ |
43
|
2 |
|
return $this->sendGetJsonRequest('/scheduler/jobs'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param JobEntityInterface $jobEntity |
48
|
|
|
* @return bool |
49
|
|
|
* @throws ApiClientException |
50
|
|
|
*/ |
51
|
10 |
|
public function addingJob(JobEntityInterface $jobEntity) |
52
|
|
|
{ |
53
|
10 |
|
$targetUrl = ''; |
54
|
|
|
|
55
|
10 |
|
if (!$jobEntity instanceof ChronosJobEntity) { |
56
|
|
|
throw new \RuntimeException('Expected ChronosJobEntity.'); |
57
|
|
|
} |
58
|
|
|
|
59
|
10 |
|
if (!empty($jobEntity->schedule) && empty($jobEntity->parents)) { |
60
|
4 |
|
$targetUrl = '/scheduler/iso8601'; |
61
|
6 |
|
} elseif (empty($jobEntity->schedule) && !empty($jobEntity->parents)) { |
62
|
4 |
|
$targetUrl = '/scheduler/dependency'; |
63
|
|
|
} |
64
|
|
|
|
65
|
10 |
|
if (empty($targetUrl)) { |
66
|
2 |
|
throw new ApiClientException('No scheduler or dependency found. Can\'t get right target url.'); |
67
|
|
|
} |
68
|
|
|
|
69
|
8 |
|
$response = $this->httpClient->postJsonData($targetUrl, $jobEntity); |
70
|
8 |
|
return ($response->getStatusCode() == 204); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param JobEntityInterface|ChronosJobEntity $jobEntity |
75
|
|
|
* @return bool |
76
|
|
|
* @throws ApiClientException |
77
|
|
|
*/ |
78
|
5 |
|
public function updatingJob(JobEntityInterface $jobEntity) |
79
|
|
|
{ |
80
|
5 |
|
return $this->addingJob($jobEntity); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $jobName |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
2 |
|
public function removeJob($jobName) |
88
|
|
|
{ |
89
|
2 |
|
$response = $this->httpClient->delete('/scheduler/job/' . $jobName); |
90
|
2 |
|
return ($response->getStatusCode() == 204); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @inheritdoc |
95
|
|
|
*/ |
96
|
2 |
|
public function getJobStats($jobName) |
97
|
|
|
{ |
98
|
2 |
|
return $this->sendGetJsonRequest('/scheduler/job/stat/' . $jobName); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $url |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
4 |
View Code Duplication |
private function sendGetJsonRequest($url) |
106
|
|
|
{ |
107
|
4 |
|
$response = $this->httpClient->get($url); |
108
|
4 |
|
if (200 == $response->getStatusCode()) { |
109
|
2 |
|
return $response->json(); |
110
|
|
|
} |
111
|
|
|
|
112
|
2 |
|
return []; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns true if the client can be connected to. |
117
|
|
|
* @return bool |
118
|
|
|
*/ |
119
|
5 |
View Code Duplication |
public function ping() |
120
|
|
|
{ |
121
|
|
|
try { |
122
|
5 |
|
$this->httpClient->get('/scheduler/jobs'); |
123
|
4 |
|
} catch (HttpConnectionException $exception) { |
124
|
4 |
|
if ($exception->getCode() == HttpConnectionException::ERROR_CODE_REQUEST_EXCEPTION || |
125
|
4 |
|
$exception->getCode() == HttpConnectionException::ERROR_CODE_CONNECT_EXCEPTION |
126
|
|
|
) { |
127
|
2 |
|
return false; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
3 |
|
return true; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|