|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gewaer\Api\Controllers; |
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use PDOException; |
|
9
|
|
|
use Phalcon\Http\Response; |
|
10
|
|
|
use Phalcon\Queue\Beanstalk\Exception as BeanstalkException; |
|
11
|
|
|
use Gewaer\Exception\ServerErrorHttpException; |
|
12
|
|
|
use RedisException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class IndexController |
|
16
|
|
|
* |
|
17
|
|
|
* @package Gewaer\Api\Controllers |
|
18
|
|
|
* |
|
19
|
|
|
* @property Redis $redis |
|
20
|
|
|
* @property Beanstalk $queue |
|
21
|
|
|
* @property Mysql $db |
|
22
|
|
|
* @property \Monolog\Logger $log |
|
23
|
|
|
*/ |
|
24
|
|
|
class IndexController extends BaseController |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Index |
|
28
|
|
|
* |
|
29
|
|
|
* @method GET |
|
30
|
|
|
* @url / |
|
31
|
|
|
* |
|
32
|
|
|
* @return Response |
|
33
|
|
|
*/ |
|
34
|
|
|
public function index($id = null) : Response |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->response(['Woot Baka']); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Show the status of the diferent services |
|
41
|
|
|
* |
|
42
|
|
|
* @method GET |
|
43
|
|
|
* @url /status |
|
44
|
|
|
* |
|
45
|
|
|
* @return Response |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public function status() : Response |
|
48
|
|
|
{ |
|
49
|
1 |
|
$response = []; |
|
50
|
|
|
|
|
51
|
|
|
//Try to connect to Redis |
|
52
|
|
|
try { |
|
53
|
1 |
|
$this->redis->hSet('htest', 'a', 'x'); |
|
54
|
1 |
|
$this->redis->hSet('htest', 'b', 'y'); |
|
55
|
1 |
|
$this->redis->hSet('htest', 'c', 'z'); |
|
56
|
1 |
|
$this->redis->hSet('htest', 'd', 't'); |
|
57
|
1 |
|
$this->redis->hGetAll('htest'); |
|
58
|
|
|
|
|
59
|
|
|
//$this->redis->ping(); |
|
60
|
|
|
} catch (RedisException $e) { |
|
61
|
|
|
$this->log->error($e->getMessage(), $e->getTrace()); |
|
62
|
|
|
$response['errors']['redis'] = $e->getMessage(); |
|
63
|
|
|
} catch (Exception $e) { |
|
64
|
|
|
$this->log->error("Redis isn't working. {$e->getMessage()}", $e->getTrace()); |
|
65
|
|
|
$response['errors']['redis'] = "Redis isn't working."; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
//Try to connect to Beanstalk |
|
69
|
|
|
try { |
|
70
|
1 |
|
$this->queue->connect(); |
|
71
|
1 |
|
} catch (BeanstalkException $e) { |
|
72
|
|
|
$this->log->error($e->getMessage(), $e->getTrace()); |
|
73
|
|
|
$response['errors']['beanstalk'] = $e->getMessage(); |
|
74
|
1 |
|
} catch (Exception $e) { |
|
75
|
|
|
$this->log->error("Beanstalk isn't working. {$e->getMessage()}", $e->getTrace()); |
|
76
|
|
|
$response['errors']['beanstalk'] = "Beanstalk isn't working."; |
|
77
|
|
|
} finally { |
|
78
|
1 |
|
$this->queue->disconnect(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
//Try to connect to db |
|
82
|
|
|
try { |
|
83
|
|
|
$this->db->connect(); |
|
84
|
|
|
} catch (PDOException $e) { |
|
85
|
|
|
$this->log->error($e->getMessage(), $e->getTrace()); |
|
86
|
|
|
$response['errors']['db'] = $e->getMessage(); |
|
87
|
|
|
} catch (Exception $e) { |
|
88
|
|
|
$this->log->error("The database isn't working. {$e->getMessage()}", $e->getTrace()); |
|
89
|
|
|
$response['errors']['db'] = "The database isn't working."; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if (!count($response)) { |
|
93
|
|
|
return $this->response(['OK']); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
throw new ServerErrorHttpException(json_encode($response)); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|