diff --git a/app/controllers/api/edge.php b/app/controllers/api/edge.php index bbd1599a4b..71b11b9c4d 100644 --- a/app/controllers/api/edge.php +++ b/app/controllers/api/edge.php @@ -6,13 +6,11 @@ use Appwrite\Extend\Exception; use Appwrite\Utopia\Request; use Appwrite\Utopia\Response; use Utopia\App; -use Utopia\Database\Database; +use Utopia\Database\DateTime; use Utopia\Database\Document; use Utopia\Queue\Client; use Utopia\Validator\ArrayList; -use Utopia\Validator\Assoc; use Utopia\Validator\Text; -use Utopia\Validator\WhiteList; App::init() ->groups(['edge']) @@ -42,7 +40,13 @@ App::post('/v1/edge/sync') if (empty($keys)) { throw new Exception(Exception::KEY_NOT_FOUND); } + + $originEdgeUrl = $request->getHeader('origin-edge-url'); + $time = DateTime::now(); + + var_dump('[' . $time . '] incoming request from:' . $originEdgeUrl); var_dump($keys); + foreach ($keys as $parts) { $key = json_decode($parts); $queueForEdgeSyncIn diff --git a/app/worker.php b/app/worker.php index 97203d3bfc..b6572e59c1 100644 --- a/app/worker.php +++ b/app/worker.php @@ -12,7 +12,7 @@ use Utopia\CLI\Console; use Utopia\Config\Config; use Utopia\Database\Database; use Utopia\Database\Document; -use Utopia\Pools\Connection; +use Utopia\Queue\Connection; use Utopia\Queue\Adapter\Swoole; use Utopia\Queue\Client; use Utopia\Queue\Message; @@ -21,6 +21,7 @@ use Utopia\Registry\Registry; use Utopia\Logger\Log; use Utopia\Logger\Logger; use Utopia\Pools\Group; +use Utopia\Storage\Device; Runtime::enableCoroutine(SWOOLE_HOOK_ALL); @@ -88,7 +89,7 @@ Server::setResource('queueForCertificates', function (Connection $queue) { return new Certificate($queue); }, ['queue']); -Server::setResource('queueForSyncOut', function (Connection $queue) { +Server::setResource('queueForEdgeSyncOut', function (Connection $queue) { return new Client('v1-sync-out', $queue); }, ['queue']); @@ -107,13 +108,13 @@ Server::setResource('pools', function ($register) { $pools = $register->get('pools'); $connection = $pools->get('queue')->pop()->getResource(); $workerNumber = swoole_cpu_num() * intval(App::getEnv('_APP_WORKER_PER_CORE', 6)); -$workerNumber =1; + if (empty(App::getEnv('QUEUE'))) { throw new Exception('Please configure "QUEUE" environemnt variable.'); } $adapter = new Swoole($connection, $workerNumber, App::getEnv('QUEUE')); -$server = new Server($adapter); +$server = new Server($adapter); $server ->shutdown() @@ -245,7 +246,7 @@ function getProjectDB(Document $project): Database * @param string $projectId of the project * @return Device */ -function getFunctionsDevice($projectId): Device +function getFunctionsDevice(string $projectId): Device { return getDevice(APP_STORAGE_FUNCTIONS . '/app-' . $projectId); } @@ -255,7 +256,7 @@ function getFunctionsDevice($projectId): Device * @param string $projectId of the project * @return Device */ -function getFilesDevice($projectId): Device +function getFilesDevice(string $projectId): Device { return getDevice(APP_STORAGE_UPLOADS . '/app-' . $projectId); } @@ -265,7 +266,7 @@ function getFilesDevice($projectId): Device * @param string $projectId of the project * @return Device */ -function getBuildsDevice($projectId): Device +function getBuildsDevice(string $projectId): Device { return getDevice(APP_STORAGE_BUILDS . '/app-' . $projectId); } diff --git a/app/workers/certificates.php b/app/workers/certificates.php index 82bbd75339..38a0fb3548 100644 --- a/app/workers/certificates.php +++ b/app/workers/certificates.php @@ -109,6 +109,18 @@ Server::setResource('execute', function () { $certificate->setAttribute('renewDate', getRenewDate($domain->get())); $certificate->setAttribute('attempts', 0); $certificate->setAttribute('issueDate', DateTime::now()); + + // Enqueue certificate for regional sync + $filename = APP_STORAGE_CERTIFICATES . '/' . $domain . '.tar.gz'; + if (file_exists($filename)) { + $queueForEdgeSyncOut->enqueue([ + 'type' => 'certificate', + 'key' => [ + 'domain' => $domain, + 'contents' => base64_encode(file_get_contents($filename)), + ] + ]); + } } catch (Throwable $e) { // Set exception as log in certificate document $certificate->setAttribute('log', $e->getMessage()); @@ -128,17 +140,6 @@ Server::setResource('execute', function () { // Save all changes we made to certificate document into database saveCertificateDocument($domain->get(), $certificate, $dbForConsole); - - $filename = APP_STORAGE_CERTIFICATES . '/' . $domain . '.tar.gz'; - if (file_exists($filename)) { - $queueForEdgeSyncOut->enqueue([ - 'type' => 'certificate', - 'key' => [ - 'domain' => $domain, - 'contents' => base64_encode(file_get_contents($filename)), - ] - ]); - } } }; }); @@ -426,6 +427,7 @@ $server->job() ->inject('message') ->inject('dbForConsole') ->inject('execute') + ->inject('queueForEdgeSyncOut') ->action(function ($message, $dbForConsole, $execute, Client $queueForEdgeSyncOut) use ($server) { $payload = $message->getPayload() ?? []; diff --git a/app/workers/sync-In.php b/app/workers/sync-in.php similarity index 100% rename from app/workers/sync-In.php rename to app/workers/sync-in.php diff --git a/app/workers/sync-out.php b/app/workers/sync-out.php index 94e03a7dc6..faf7fa29ef 100644 --- a/app/workers/sync-out.php +++ b/app/workers/sync-out.php @@ -58,6 +58,7 @@ function call(string $url, string $token, array $data): int $ch = curl_init($url); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Authorization: Bearer ' . $token, + 'Origin-edge-url: ' . App::getEnv('_APP_REGION'), 'Content-type: multipart/form-data; boundary=' . $delimiter, 'Content-Length: ' . strlen($payload) ]);