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