redis-syncs

This commit is contained in:
shimon
2022-10-06 15:33:57 +03:00
parent 4a8befd110
commit c6a054f88d
4 changed files with 117 additions and 2 deletions
+13
View File
@@ -186,4 +186,17 @@ return [
'optional' => false,
'icon' => '',
],
'syncs' => [
'key' => 'syncs',
'name' => 'syncs',
'subtitle' => 'Appwrite\'s cloud regions syncs Endpoint',
'description' => 'Syncs Endpoint',
'controller' => 'api/syncs.php',
'sdk' => false,
'docs' => false,
'docsUrl' => '',
'tests' => true,
'optional' => false,
'icon' => '',
],
];
+44
View File
@@ -0,0 +1,44 @@
<?php
use Ahc\Jwt\JWT;
use Ahc\Jwt\JWTException;
use Appwrite\Event\SyncIn;
use Appwrite\Extend\Exception;
use Appwrite\Utopia\Request;
use Appwrite\Utopia\Response;
use Utopia\App;
use Utopia\Validator\ArrayList;
use Utopia\Validator\Text;
App::post('/v1/syncs')
->desc('Purge cache keys')
->label('scope', 'public')
->param('keys', '', new ArrayList(new Text(100), 1000), 'Cache keys')
->inject('request')
->inject('response')
->action(function (array $keys, Request $request, Response $response) {
if (empty($keys)) {
throw new Exception(Exception::KEY_NOT_FOUND);
}
$token = $request->getHeader('authorization');
$token = str_replace(["Bearer"," "], "", $token);
$jwt = new JWT(App::getEnv('_APP_OPENSSL_KEY_V1'), 'HS256', 900, 10);
// try {
// $payload = $jwt->decode($token);
// } catch (JWTException $error) {
// throw new Exception(Exception::USER_JWT_INVALID, 'Failed to verify JWT. ' . $error->getMessage());
// }
$syncIn = new SyncIn();
foreach ($keys as $key) {
$syncIn
->addKey($key)
->trigger();
}
$response
->setStatusCode(Response::STATUS_CODE_OK)
->send();
});
+20
View File
@@ -1,6 +1,8 @@
<?php
use Appwrite\Resque\Worker;
use Utopia\Cache\Adapter\Redis as RedisCache;
use Utopia\Cache\Cache;
use Utopia\CLI\Console;
require_once __DIR__ . '/../init.php';
@@ -23,8 +25,26 @@ class SyncsInV1 extends Worker
public function run(): void
{
if (!empty($this->args['key'])) {
//var_dump('Purging -> ' . $this->args['key'] . ' from Redis cache');
$this->getCache()->purge($this->args['key']);
}
}
/**
* Get cache
* @return RedisCache
* @throws Exception
*/
private function getCache(): RedisCache
{
global $register;
return new RedisCache($register->get('cache'));
}
public function shutdown(): void
{
}
+40 -2
View File
@@ -1,6 +1,8 @@
<?php
use Ahc\Jwt\JWT;
use Appwrite\Resque\Worker;
use Utopia\App;
use Utopia\CLI\Console;
require_once __DIR__ . '/../init.php';
@@ -12,6 +14,12 @@ class SyncsOutV1 extends Worker
{
protected array $errors = [];
private array $regions = [
'fra1' => '172.17.0.1',
'nyc1' => '172.17.0.1',
'blr1' => '172.17.0.1',
];
public function getName(): string
{
return "syncs-out";
@@ -23,8 +31,38 @@ class SyncsOutV1 extends Worker
public function run(): void
{
var_dump('run');
var_dump($this->args['key']);
//TODO current region env implementation
$currentRegion = 'nyc1';
$data['keys'][] = $this->args['key'];
$jwt = new JWT(App::getEnv('_APP_OPENSSL_KEY_V1'), 'HS256', 900, 10);
$token = $jwt->encode($data);
foreach ($this->regions as $region => $host) {
if ($currentRegion === $region) {
continue;
}
$ch = curl_init($host . '/v1/syncs');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $token,
'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
for ($attempts = 0; $attempts < 6; $attempts++) {
curl_exec($ch);
$responseStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($responseStatus === 200) {
break;
}
sleep(2);
}
curl_close($ch);
}
}