]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add retry job
authorLukas Reschke <lukas@statuscode.ch>
Fri, 18 Nov 2016 14:33:51 +0000 (15:33 +0100)
committerRoeland Jago Douma <roeland@famdouma.nl>
Mon, 21 Nov 2016 10:30:01 +0000 (11:30 +0100)
Signed-off-by: Lukas Reschke <lukas@statuscode.ch>
apps/lookup_server_connector/appinfo/app.php
apps/lookup_server_connector/lib/BackgroundJobs/RetryJob.php
apps/lookup_server_connector/lib/UpdateLookupServer.php

index d757284f7d540c2c92d13379387ea70a317f1780..6c63e9a04007d753cd9ec28f9308a0bf902b74e6 100644 (file)
@@ -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);
 });
index 5c4f1e73259732e6c2db09c81c5056fb9cf26ff9..f33323b2d4f371fe0ca4426d5c518bbdef6109fb 100644 (file)
@@ -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,
+                               ]
+                       );
+
+               }
        }
 }
index e9f0d069d923de66fbfaedfcc5f5745b71d48398..a1bc99091ded930fca856539de064036882fc0cb 100644 (file)
@@ -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,
+                               ]
+                       );
+               }
        }
 }