2016-04-05 10:33:15 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 14:49:16 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-04-05 10:33:15 +00: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 13:26:30 +00:00
|
|
|
* Class RetryJob
|
2016-04-05 10:33:15 +00:00
|
|
|
*
|
2016-05-04 13:26:30 +00:00
|
|
|
* Background job to re-send update of federated re-shares to the remote server in
|
2016-04-05 10:33:15 +00:00
|
|
|
* case the server was not available on the first try
|
|
|
|
*
|
|
|
|
* @package OCA\FederatedFileSharing\BackgroundJob
|
|
|
|
*/
|
2016-05-04 13:26:30 +00:00
|
|
|
class RetryJob extends Job {
|
2016-04-05 10:33:15 +00:00
|
|
|
|
|
|
|
/** @var bool */
|
|
|
|
private $retainJob = true;
|
2016-05-04 13:26:30 +00:00
|
|
|
|
2016-04-05 10:33:15 +00:00
|
|
|
/** @var Notifications */
|
|
|
|
private $notifications;
|
|
|
|
|
2016-05-04 13:26:30 +00:00
|
|
|
/** @var int max number of attempts to send the request */
|
|
|
|
private $maxTry = 20;
|
2016-04-05 10:33:15 +00:00
|
|
|
|
2016-05-04 13:26:30 +00:00
|
|
|
/** @var int how much time should be between two tries (10 minutes) */
|
|
|
|
private $interval = 600;
|
2016-04-05 10:33:15 +00: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 11:52:17 +00:00
|
|
|
\OC::$server->getL10N('federatedfilesharing'),
|
|
|
|
\OC::$server->getCloudIdManager()
|
2016-04-05 10:33:15 +00:00
|
|
|
);
|
|
|
|
$this->notifications = new Notifications(
|
|
|
|
$addressHandler,
|
|
|
|
\OC::$server->getHTTPClientService(),
|
2017-04-05 20:35:59 +00:00
|
|
|
\OC::$server->query(\OCP\OCS\IDiscoveryService::class),
|
2018-05-04 13:25:02 +00:00
|
|
|
\OC::$server->getJobList(),
|
|
|
|
\OC::$server->getCloudFederationProviderManager(),
|
|
|
|
\OC::$server->getCloudFederationFactory()
|
2016-04-05 10:33:15 +00:00
|
|
|
);
|
|
|
|
}
|
2016-05-04 13:26:30 +00:00
|
|
|
|
2016-04-05 10:33:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* run the job, then remove it from the jobList
|
|
|
|
*
|
|
|
|
* @param JobList $jobList
|
2017-07-19 14:06:22 +00:00
|
|
|
* @param ILogger|null $logger
|
2016-04-05 10:33:15 +00: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 13:26:30 +00:00
|
|
|
$remoteId = $argument['remoteId'];
|
2016-04-05 10:33:15 +00:00
|
|
|
$token = $argument['token'];
|
2016-05-04 13:26:30 +00:00
|
|
|
$action = $argument['action'];
|
|
|
|
$data = json_decode($argument['data'], true);
|
2016-04-05 10:33:15 +00:00
|
|
|
$try = (int)$argument['try'] + 1;
|
|
|
|
|
2016-05-04 13:26:30 +00:00
|
|
|
$result = $this->notifications->sendUpdateToRemote($remote, $remoteId, $token, $action, $data, $try);
|
2017-03-10 14:37:21 +00:00
|
|
|
|
2016-04-05 10:33:15 +00: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) {
|
2018-01-25 22:16:13 +00:00
|
|
|
$jobList->add(RetryJob::class,
|
2016-04-05 10:33:15 +00:00
|
|
|
[
|
|
|
|
'remote' => $argument['remote'],
|
2016-05-04 13:26:30 +00:00
|
|
|
'remoteId' => $argument['remoteId'],
|
2016-04-05 10:33:15 +00:00
|
|
|
'token' => $argument['token'],
|
2016-05-04 13:26:30 +00:00
|
|
|
'data' => $argument['data'],
|
|
|
|
'action' => $argument['action'],
|
2016-04-05 10:33:15 +00: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|