|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package: chapi |
|
4
|
|
|
* |
|
5
|
|
|
* @author: bthapaliya |
|
6
|
|
|
* @since: 2016-12-02 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Chapi\Service\JobRepository; |
|
10
|
|
|
|
|
11
|
|
|
use Chapi\Component\Cache\CacheInterface; |
|
12
|
|
|
use Chapi\Component\RemoteClients\ApiClientInterface; |
|
13
|
|
|
use Chapi\Entity\Chronos\ChronosJobEntity; |
|
14
|
|
|
use Chapi\Entity\JobEntityInterface; |
|
15
|
|
|
use Chapi\Entity\Marathon\MarathonAppEntity; |
|
16
|
|
|
use Chapi\Service\JobValidator\JobValidatorServiceInterface; |
|
17
|
|
|
use Psr\Log\LoggerInterface; |
|
18
|
|
|
|
|
19
|
|
|
class BridgeMarathon implements BridgeInterface |
|
20
|
|
|
{ |
|
21
|
|
|
const CACHE_TIME_JOB_LIST = 60; |
|
22
|
|
|
|
|
23
|
|
|
const CACHE_KEY_APP_LIST = 'marathon.app.list'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var \Chapi\Component\RemoteClients\ApiClientInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $apiClient; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var JobValidatorServiceInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $jobEntityValidatorService; |
|
33
|
|
|
/** |
|
34
|
|
|
* @var CacheInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private $cache; |
|
37
|
|
|
/** |
|
38
|
|
|
* @var LoggerInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
private $logger; |
|
41
|
|
|
|
|
42
|
|
|
private $cacheHasToDelete = false; |
|
43
|
|
|
|
|
44
|
5 |
View Code Duplication |
public function __construct( |
|
45
|
|
|
ApiClientInterface $apiClient, |
|
46
|
|
|
CacheInterface $cache, |
|
47
|
|
|
JobValidatorServiceInterface $jobEntityValidatorService, |
|
48
|
|
|
LoggerInterface $logger |
|
49
|
|
|
) { |
|
50
|
5 |
|
$this->apiClient = $apiClient; |
|
51
|
5 |
|
$this->jobEntityValidatorService = $jobEntityValidatorService; |
|
52
|
5 |
|
$this->cache = $cache; |
|
53
|
5 |
|
$this->logger = $logger; |
|
54
|
5 |
|
} |
|
55
|
|
|
|
|
56
|
5 |
|
public function __destruct() |
|
57
|
|
|
{ |
|
58
|
5 |
|
if ($this->cacheHasToDelete) { |
|
59
|
2 |
|
$this->cache->delete(self::CACHE_KEY_APP_LIST); |
|
60
|
|
|
} |
|
61
|
5 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return JobEntityInterface[] |
|
65
|
|
|
*/ |
|
66
|
1 |
|
public function getJobs() |
|
67
|
|
|
{ |
|
68
|
1 |
|
$apps = []; |
|
69
|
1 |
|
$jobsList = $this->getJobList(); |
|
70
|
|
|
|
|
71
|
1 |
|
if (!empty($jobsList)) { |
|
72
|
|
|
foreach ($jobsList as $jobData) { |
|
73
|
|
|
$apps[] = new MarathonAppEntity($jobData); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
1 |
|
return $apps; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param JobEntityInterface $jobEntity |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
2 |
View Code Duplication |
public function addJob(JobEntityInterface $jobEntity) |
|
84
|
|
|
{ |
|
85
|
2 |
|
if ($this->apiClient->addingJob($jobEntity)) { |
|
86
|
1 |
|
$this->cacheHasToDelete = true; |
|
87
|
1 |
|
return true; |
|
88
|
|
|
} |
|
89
|
1 |
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param JobEntityInterface $jobEntity |
|
94
|
|
|
* @return bool |
|
95
|
|
|
*/ |
|
96
|
|
View Code Duplication |
public function updateJob(JobEntityInterface $jobEntity) |
|
97
|
|
|
{ |
|
98
|
|
|
if ($this->apiClient->updatingJob($jobEntity)) { |
|
99
|
|
|
$this->cacheHasToDelete = true; |
|
100
|
|
|
return true; |
|
101
|
|
|
} |
|
102
|
|
|
return false; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param JobEntityInterface $jobEntity |
|
107
|
|
|
* @return bool |
|
108
|
|
|
*/ |
|
109
|
2 |
View Code Duplication |
public function removeJob(JobEntityInterface $jobEntity) |
|
110
|
|
|
{ |
|
111
|
2 |
|
if ($this->apiClient->removeJob($jobEntity->getKey())) { |
|
112
|
1 |
|
$this->cacheHasToDelete = true; |
|
113
|
1 |
|
return true; |
|
114
|
|
|
} |
|
115
|
1 |
|
return false; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return array|mixed |
|
120
|
|
|
*/ |
|
121
|
1 |
|
private function getJobList() |
|
122
|
|
|
{ |
|
123
|
1 |
|
$result = $this->cache->get(self::CACHE_KEY_APP_LIST); |
|
124
|
|
|
|
|
125
|
1 |
|
if (is_array($result)) { |
|
126
|
1 |
|
return $result; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$result = $this->apiClient->listingJobs(); |
|
130
|
|
|
|
|
131
|
|
|
if (!empty($result['apps'])) { |
|
132
|
|
|
$this->cache->set(self::CACHE_KEY_APP_LIST, $result['apps'], self::CACHE_TIME_JOB_LIST); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $result['apps']; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|