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