regions = Config::getParam('regions', []); } public function run(): void { $currentRegion = App::getEnv('_APP_REGION', 'nyc1'); $data[] = $this->args['key']; $jwt = new JWT(App::getEnv('_APP_OPENSSL_KEY_V1'), 'HS256', 600, 10); $token = $jwt->encode($data); if (!empty($this->args['region'])) { $this->regions = $this->regions[$this->args['region']]; } foreach ($this->regions as $code => $region) { if ($currentRegion === $code) { continue; } $status = $this->send($region['domain'] . '/v1/edge', $token, ['keys' => $data]); if ($status !== Response::STATUS_CODE_OK) { $this->getConsoleDB()->createDocument('syncs', new Document([ 'requestedAt' => DateTime::now(), 'regionOrg' => $currentRegion, 'regionDest' => $code, 'keys' => $data, 'status' => $status, ])); } } } private function send($url, $token, $data): int { $ch = curl_init($url); 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 < 3; $attempts++) { curl_exec($ch); $responseStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($responseStatus === 200) { return $responseStatus; } sleep(2); } curl_close($ch); return $responseStatus; } public function shutdown(): void { } }