From dac980115fe33233d56e450c0a33ac72ee1ddfaa Mon Sep 17 00:00:00 2001 From: Lukas Reschke Date: Fri, 18 Nov 2016 15:33:51 +0100 Subject: [PATCH] Add retry job Signed-off-by: Lukas Reschke --- apps/lookup_server_connector/appinfo/app.php | 3 +- .../lib/BackgroundJobs/RetryJob.php | 50 ++++++++++++++++++- .../lib/UpdateLookupServer.php | 47 +++++++++-------- 3 files changed, 76 insertions(+), 24 deletions(-) diff --git a/apps/lookup_server_connector/appinfo/app.php b/apps/lookup_server_connector/appinfo/app.php index d757284f7d5..6c63e9a0400 100644 --- a/apps/lookup_server_connector/appinfo/app.php +++ b/apps/lookup_server_connector/appinfo/app.php @@ -39,7 +39,8 @@ $dispatcher->addListener('OC\AccountManager::userUpdated', function(\Symfony\Com new \OC\AppFramework\Utility\TimeFactory(), \OC::$server->getURLGenerator(), \OC::$server->getUserManager() - ) + ), + \OC::$server->getJobList() ); $updateLookupServer->userUpdated($user); }); diff --git a/apps/lookup_server_connector/lib/BackgroundJobs/RetryJob.php b/apps/lookup_server_connector/lib/BackgroundJobs/RetryJob.php index 5c4f1e73259..f33323b2d4f 100644 --- a/apps/lookup_server_connector/lib/BackgroundJobs/RetryJob.php +++ b/apps/lookup_server_connector/lib/BackgroundJobs/RetryJob.php @@ -24,10 +24,58 @@ namespace OCA\LookupServerConnector\BackgroundJobs; use OC\BackgroundJob\Job; +use OCP\BackgroundJob\IJobList; +use OCP\Http\Client\IClientService; class RetryJob extends Job { + /** @var IClientService */ + private $clientService; + /** @var IJobList */ + private $jobList; + /** @var string */ + private $lookupServer = 'https://lookup.nextcloud.com/users'; + + /** + * @param IClientService|null $clientService + * @param IJobList|null $jobList + */ + public function __construct(IClientService $clientService = null, + IJobList $jobList = null) { + if($clientService !== null) { + $this->clientService = $clientService; + } else { + $this->clientService = \OC::$server->getHTTPClientService(); + } + if($jobList !== null) { + $this->jobList = $jobList; + } else { + $this->jobList = \OC::$server->getJobList(); + } + } protected function run($argument) { - // TODO: Implement run() method. + if($argument['retryNo'] === 5) { + return; + } + + $client = $this->clientService->newClient(); + + try { + $client->post($this->lookupServer, + [ + 'body' => json_encode($argument['dataArray']), + 'timeout' => 10, + 'connect_timeout' => 3, + ] + ); + } catch (\Exception $e) { + $this->jobList->add(RetryJob::class, + [ + 'dataArray' => $argument['dataArray'], + 'retryNo' => $argument['retryNo'] + 1, + ] + ); + + } } } diff --git a/apps/lookup_server_connector/lib/UpdateLookupServer.php b/apps/lookup_server_connector/lib/UpdateLookupServer.php index e9f0d069d92..a1bc99091de 100644 --- a/apps/lookup_server_connector/lib/UpdateLookupServer.php +++ b/apps/lookup_server_connector/lib/UpdateLookupServer.php @@ -25,6 +25,8 @@ namespace OCA\LookupServerConnector; use OC\Accounts\AccountManager; use OC\Security\IdentityProof\Manager; use OC\Security\IdentityProof\Signer; +use OCA\LookupServerConnector\BackgroundJobs\RetryJob; +use OCP\BackgroundJob\IJobList; use OCP\Http\Client\IClientService; use OCP\IConfig; use OCP\IUser; @@ -48,6 +50,8 @@ class UpdateLookupServer { private $keyManager; /** @var Signer */ private $signer; + /** @var IJobList */ + private $jobList; /** @var string URL point to lookup server */ private $lookupServer = 'https://lookup.nextcloud.com/users'; @@ -58,19 +62,22 @@ class UpdateLookupServer { * @param IClientService $clientService * @param Manager $manager * @param Signer $signer + * @param IJobList $jobList */ public function __construct(AccountManager $accountManager, IConfig $config, ISecureRandom $secureRandom, IClientService $clientService, Manager $manager, - Signer $signer) { + Signer $signer, + IJobList $jobList) { $this->accountManager = $accountManager; $this->config = $config; $this->secureRandom = $secureRandom; $this->clientService = $clientService; $this->keyManager = $manager; $this->signer = $signer; + $this->jobList = $jobList; } /** @@ -86,24 +93,11 @@ class UpdateLookupServer { } } - if (empty($publicData) && !empty($authKey)) { - $this->removeFromLookupServer($user); - } else { + if (!empty($publicData) && !empty($authKey)) { $this->sendToLookupServer($user, $publicData); } } - /** - * TODO: FIXME. Implement removal from lookup server. - * - * remove user from lookup server - * - * @param IUser $user - */ - protected function removeFromLookupServer(IUser $user) { - $this->config->deleteUserValue($user->getUID(), 'lookup_server_connector', 'authKey'); - } - /** * send public user data to the lookup server * @@ -122,12 +116,21 @@ class UpdateLookupServer { ]; $dataArray = $this->signer->sign('lookupserver', $dataArray, $user); $httpClient = $this->clientService->newClient(); - $httpClient->post($this->lookupServer, - [ - 'body' => json_encode($dataArray), - 'timeout' => 10, - 'connect_timeout' => 3, - ] - ); + try { + $httpClient->post($this->lookupServer, + [ + 'body' => json_encode($dataArray), + 'timeout' => 10, + 'connect_timeout' => 3, + ] + ); + } catch (\Exception $e) { + $this->jobList->add(RetryJob::class, + [ + 'dataArray' => $dataArray, + 'retryNo' => 0, + ] + ); + } } } -- 2.39.5