Skip to content

Commit

Permalink
fix for mevdschee#755
Browse files Browse the repository at this point in the history
  • Loading branch information
mevdschee committed Feb 22, 2021
1 parent d52ad99 commit 0a5f3b9
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 24 deletions.
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ These are all the configuration options and their default value between brackets
- "database": Database the connecting is made to (no default)
- "tables": Comma separated list of tables to publish (defaults to 'all')
- "middlewares": List of middlewares to load (`cors`)
- "controllers": List of controllers to load (`records,geojson,openapi`)
- "controllers": List of controllers to load (`records,geojson,openapi,status`)
- "openApiBase": OpenAPI info (`{"info":{"title":"PHP-CRUD-API","version":"1.0.0"}}`)
- "cacheType": `TempFile`, `Redis`, `Memcache`, `Memcached` or `NoCache` (`TempFile`)
- "cachePath": Path/address of the cache (defaults to system's temp directory)
Expand Down Expand Up @@ -1216,6 +1216,32 @@ The following JSON structure is used:

NB: Any non-error response will have status: 200 OK

## Status

To connect to your monitoring there are two endpoints, one is up:

GET /status/up

And this should return status 200 and as data:

{
"db": true,
"cache": true
}

Values will be false when reading takes longer than 1 second. Alternatively you can use:

GET /status/ping

And this should return status 200 and as data:

{
"db": 42,
"cache": 9
}

These can be used to measure the time (in microseconds) to connect and read data from the database and the cache.

## Tests

I am testing mainly on Ubuntu and I have the following test setups:
Expand Down
4 changes: 4 additions & 0 deletions src/Tqdev/PhpCrudApi/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Tqdev\PhpCrudApi\Controller\JsonResponder;
use Tqdev\PhpCrudApi\Controller\OpenApiController;
use Tqdev\PhpCrudApi\Controller\RecordController;
use Tqdev\PhpCrudApi\Controller\StatusController;
use Tqdev\PhpCrudApi\Database\GenericDB;
use Tqdev\PhpCrudApi\GeoJson\GeoJsonService;
use Tqdev\PhpCrudApi\Middleware\AuthorizationMiddleware;
Expand Down Expand Up @@ -138,6 +139,9 @@ public function __construct(Config $config)
$geoJson = new GeoJsonService($reflection, $records);
new GeoJsonController($router, $responder, $geoJson);
break;
case 'status':
new StatusController($router, $responder, $cache, $db);
break;
}
}
foreach ($config->getCustomControllers() as $className) {
Expand Down
1 change: 1 addition & 0 deletions src/Tqdev/PhpCrudApi/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ interface Cache
public function set(string $key, string $value, int $ttl = 0): bool;
public function get(string $key): string;
public function clear(): bool;
public function ping(): int;
}
4 changes: 3 additions & 1 deletion src/Tqdev/PhpCrudApi/Cache/MemcacheCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Tqdev\PhpCrudApi\Cache;

class MemcacheCache implements Cache
use Tqdev\PhpCrudApi\Cache\Base\BaseCache;

class MemcacheCache extends BaseCache implements Cache
{
protected $prefix;
protected $memcache;
Expand Down
22 changes: 3 additions & 19 deletions src/Tqdev/PhpCrudApi/Cache/NoCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,8 @@

namespace Tqdev\PhpCrudApi\Cache;

class NoCache implements Cache
{
public function __construct()
{
}

public function set(string $key, string $value, int $ttl = 0): bool
{
return true;
}
use Tqdev\PhpCrudApi\Cache\Base\BaseCache;

public function get(string $key): string
{
return '';
}

public function clear(): bool
{
return true;
}
class NoCache extends BaseCache implements Cache
{
}
2 changes: 2 additions & 0 deletions src/Tqdev/PhpCrudApi/Cache/RedisCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Tqdev\PhpCrudApi\Cache;

use Tqdev\PhpCrudApi\Cache\Base\BaseCache;

class RedisCache implements Cache
{
protected $prefix;
Expand Down
4 changes: 3 additions & 1 deletion src/Tqdev/PhpCrudApi/Cache/TempFileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Tqdev\PhpCrudApi\Cache;

class TempFileCache implements Cache
use Tqdev\PhpCrudApi\Cache\Base\BaseCache;

class TempFileCache extends BaseCache implements Cache
{
const SUFFIX = 'cache';

Expand Down
2 changes: 1 addition & 1 deletion src/Tqdev/PhpCrudApi/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Config
'database' => null,
'tables' => '',
'middlewares' => 'cors,errors',
'controllers' => 'records,geojson,openapi',
'controllers' => 'records,geojson,openapi,status',
'customControllers' => '',
'customOpenApiBuilders' => '',
'cacheType' => 'TempFile',
Expand Down
5 changes: 5 additions & 0 deletions src/Tqdev/PhpCrudApi/Controller/Responder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ interface Responder
public function error(int $error, string $argument, $details = null): ResponseInterface;

public function success($result): ResponseInterface;

public function multi($results): ResponseInterface;

public function exception($exception): ResponseInterface;

}
8 changes: 8 additions & 0 deletions src/Tqdev/PhpCrudApi/Database/GenericDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,14 @@ private function query(string $sql, array $parameters): \PDOStatement
return $stmt;
}

public function ping(): int
{
$start = microtime(true);
$stmt = $this->pdo->prepare('SELECT 1');
$stmt->execute();
return intval((microtime(true)-$start)*1000000);
}

public function getCacheKey(): string
{
return md5(json_encode([
Expand Down
5 changes: 5 additions & 0 deletions src/Tqdev/PhpCrudApi/Record/RecordService.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,9 @@ public function _list(string $tableName, array $params): ListDocument
$this->joiner->addJoins($table, $records, $params, $this->db);
return new ListDocument($records, $count);
}

public function ping(): int
{
return $this->db->ping();
}
}
2 changes: 1 addition & 1 deletion tests/config/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'database' => 'incorrect_database',
'username' => 'incorrect_username',
'password' => 'incorrect_password',
'controllers' => 'records,columns,cache,openapi,geojson',
'controllers' => 'records,columns,cache,openapi,geojson,status',
'middlewares' => 'sslRedirect,xml,cors,reconnect,dbAuth,jwtAuth,basicAuth,authorization,sanitation,validation,ipAddress,multiTenancy,pageLimits,joinLimits,customization',
'dbAuth.mode' => 'optional',
'dbAuth.returnedColumns' => 'id,username,password',
Expand Down
8 changes: 8 additions & 0 deletions tests/functional/005_status/001_up.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
===
GET /status/up
===
200
Content-Type: application/json; charset=utf-8
Content-Length: 24

{"db":true,"cache":true}

0 comments on commit 0a5f3b9

Please sign in to comment.