2016-04-05 13:33:15 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2016-04-05 13:33:15 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
namespace OCA\FederatedFileSharing\BackgroundJob;
|
|
|
|
|
|
|
|
|
|
|
|
use OC\BackgroundJob\Job;
|
|
|
|
use OC\BackgroundJob\JobList;
|
|
|
|
use OCA\FederatedFileSharing\AddressHandler;
|
|
|
|
use OCA\FederatedFileSharing\Notifications;
|
|
|
|
use OCP\BackgroundJob\IJobList;
|
|
|
|
use OCP\ILogger;
|
|
|
|
|
|
|
|
/**
|
2016-05-04 16:26:30 +03:00
|
|
|
* Class RetryJob
|
2016-04-05 13:33:15 +03:00
|
|
|
*
|
2016-05-04 16:26:30 +03:00
|
|
|
* Background job to re-send update of federated re-shares to the remote server in
|
2016-04-05 13:33:15 +03:00
|
|
|
* case the server was not available on the first try
|
|
|
|
*
|
|
|
|
* @package OCA\FederatedFileSharing\BackgroundJob
|
|
|
|
*/
|
2016-05-04 16:26:30 +03:00
|
|
|
class RetryJob extends Job {
|
2016-04-05 13:33:15 +03:00
|
|
|
|
|
|
|
/** @var bool */
|
|
|
|
private $retainJob = true;
|
2016-05-04 16:26:30 +03:00
|
|
|
|
2016-04-05 13:33:15 +03:00
|
|
|
/** @var Notifications */
|
|
|
|
private $notifications;
|
|
|
|
|
2016-05-04 16:26:30 +03:00
|
|
|
/** @var int max number of attempts to send the request */
|
|
|
|
private $maxTry = 20;
|
2016-04-05 13:33:15 +03:00
|
|
|
|
2016-05-04 16:26:30 +03:00
|
|
|
/** @var int how much time should be between two tries (10 minutes) */
|
|
|
|
private $interval = 600;
|
2016-04-05 13:33:15 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* UnShare constructor.
|
|
|
|
*
|
|
|
|
* @param Notifications $notifications
|
|
|
|
*/
|
|
|
|
public function __construct(Notifications $notifications = null) {
|
|
|
|
if ($notifications) {
|
|
|
|
$this->notifications = $notifications;
|
|
|
|
} else {
|
|
|
|
$addressHandler = new AddressHandler(
|
|
|
|
\OC::$server->getURLGenerator(),
|
2017-01-27 14:52:17 +03:00
|
|
|
\OC::$server->getL10N('federatedfilesharing'),
|
|
|
|
\OC::$server->getCloudIdManager()
|
2016-04-05 13:33:15 +03:00
|
|
|
);
|
|
|
|
$this->notifications = new Notifications(
|
|
|
|
$addressHandler,
|
|
|
|
\OC::$server->getHTTPClientService(),
|
2017-04-05 23:35:59 +03:00
|
|
|
\OC::$server->query(\OCP\OCS\IDiscoveryService::class),
|
2016-04-05 13:33:15 +03:00
|
|
|
\OC::$server->getJobList()
|
|
|
|
);
|
|
|
|
}
|
2016-05-04 16:26:30 +03:00
|
|
|
|
2016-04-05 13:33:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* run the job, then remove it from the jobList
|
|
|
|
*
|
|
|
|
* @param JobList $jobList
|
2017-07-19 17:06:22 +03:00
|
|
|
* @param ILogger|null $logger
|
2016-04-05 13:33:15 +03:00
|
|
|
*/
|
|
|
|
public function execute($jobList, ILogger $logger = null) {
|
|
|
|
|
|
|
|
if ($this->shouldRun($this->argument)) {
|
|
|
|
parent::execute($jobList, $logger);
|
|
|
|
$jobList->remove($this, $this->argument);
|
|
|
|
if ($this->retainJob) {
|
|
|
|
$this->reAddJob($jobList, $this->argument);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function run($argument) {
|
|
|
|
$remote = $argument['remote'];
|
2016-05-04 16:26:30 +03:00
|
|
|
$remoteId = $argument['remoteId'];
|
2016-04-05 13:33:15 +03:00
|
|
|
$token = $argument['token'];
|
2016-05-04 16:26:30 +03:00
|
|
|
$action = $argument['action'];
|
|
|
|
$data = json_decode($argument['data'], true);
|
2016-04-05 13:33:15 +03:00
|
|
|
$try = (int)$argument['try'] + 1;
|
|
|
|
|
2016-05-04 16:26:30 +03:00
|
|
|
$result = $this->notifications->sendUpdateToRemote($remote, $remoteId, $token, $action, $data, $try);
|
2017-03-10 17:37:21 +03:00
|
|
|
|
2016-04-05 13:33:15 +03:00
|
|
|
if ($result === true || $try > $this->maxTry) {
|
|
|
|
$this->retainJob = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* re-add background job with new arguments
|
|
|
|
*
|
|
|
|
* @param IJobList $jobList
|
|
|
|
* @param array $argument
|
|
|
|
*/
|
|
|
|
protected function reAddJob(IJobList $jobList, array $argument) {
|
2016-05-04 16:26:30 +03:00
|
|
|
$jobList->add('OCA\FederatedFileSharing\BackgroundJob\RetryJob',
|
2016-04-05 13:33:15 +03:00
|
|
|
[
|
|
|
|
'remote' => $argument['remote'],
|
2016-05-04 16:26:30 +03:00
|
|
|
'remoteId' => $argument['remoteId'],
|
2016-04-05 13:33:15 +03:00
|
|
|
'token' => $argument['token'],
|
2016-05-04 16:26:30 +03:00
|
|
|
'data' => $argument['data'],
|
|
|
|
'action' => $argument['action'],
|
2016-04-05 13:33:15 +03:00
|
|
|
'try' => (int)$argument['try'] + 1,
|
|
|
|
'lastRun' => time()
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test if it is time for the next run
|
|
|
|
*
|
|
|
|
* @param array $argument
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function shouldRun(array $argument) {
|
|
|
|
$lastRun = (int)$argument['lastRun'];
|
|
|
|
return ((time() - $lastRun) > $this->interval);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|