2016-04-05 13:33:15 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2016-04-05 13:33:15 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\FederatedFileSharing\BackgroundJob;
|
|
|
|
|
|
|
|
use OCA\FederatedFileSharing\Notifications;
|
2020-12-14 18:35:12 +03:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2016-04-05 13:33:15 +03:00
|
|
|
use OCP\BackgroundJob\IJobList;
|
2020-11-24 16:18:47 +03:00
|
|
|
use OCP\BackgroundJob\Job;
|
2016-04-05 13:33:15 +03:00
|
|
|
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
|
|
|
|
2020-12-14 18:35:12 +03:00
|
|
|
|
|
|
|
public function __construct(Notifications $notifications,
|
|
|
|
ITimeFactory $timeFactory) {
|
|
|
|
parent::__construct($timeFactory);
|
|
|
|
$this->notifications = $notifications;
|
2016-04-05 13:33:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* run the job, then remove it from the jobList
|
|
|
|
*
|
2020-11-24 16:18:47 +03:00
|
|
|
* @param IJobList $jobList
|
2017-07-19 17:06:22 +03:00
|
|
|
* @param ILogger|null $logger
|
2016-04-05 13:33:15 +03:00
|
|
|
*/
|
2020-11-24 16:18:47 +03:00
|
|
|
public function execute(IJobList $jobList, ILogger $logger = null) {
|
2016-04-05 13:33:15 +03:00
|
|
|
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) {
|
2018-01-26 01:16:13 +03:00
|
|
|
$jobList->add(RetryJob::class,
|
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,
|
2020-12-14 18:35:12 +03:00
|
|
|
'lastRun' => $this->time->getTime()
|
2016-04-05 13:33:15 +03:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test if it is time for the next run
|
|
|
|
*
|
|
|
|
* @param array $argument
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function shouldRun(array $argument) {
|
|
|
|
$lastRun = (int)$argument['lastRun'];
|
2020-12-14 18:35:12 +03:00
|
|
|
return (($this->time->getTime() - $lastRun) > $this->interval);
|
2016-04-05 13:33:15 +03:00
|
|
|
}
|
|
|
|
}
|