aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Müller <thomas.mueller@tmit.eu>2015-12-21 09:47:07 +0100
committerThomas Müller <thomas.mueller@tmit.eu>2015-12-21 09:47:07 +0100
commit0b913f00c7e49c57d2e0068854b5d4c133b0cb36 (patch)
tree51d1c6e25d73ac651000d209820ce0090393f9de
parenta635975d19873ba660bc378d45da722ae09dea74 (diff)
parent54558bb5b296740233036601d2b3eb7f87ee48c4 (diff)
downloadnextcloud-server-0b913f00c7e49c57d2e0068854b5d4c133b0cb36.tar.gz
nextcloud-server-0b913f00c7e49c57d2e0068854b5d4c133b0cb36.zip
Merge pull request #21289 from owncloud/issue-20399-keep-periodic-background-jobs
Do not delete background jobs, in case an exception occured
-rw-r--r--lib/private/backgroundjob/job.php1
-rw-r--r--tests/lib/backgroundjob/job.php11
2 files changed, 9 insertions, 3 deletions
diff --git a/lib/private/backgroundjob/job.php b/lib/private/backgroundjob/job.php
index 88682cd09bb..40a27491fe6 100644
--- a/lib/private/backgroundjob/job.php
+++ b/lib/private/backgroundjob/job.php
@@ -54,7 +54,6 @@ abstract class Job implements IJob {
if ($logger) {
$logger->error('Error while running background job: ' . $e->getMessage());
}
- $jobList->remove($this, $this->argument);
}
}
diff --git a/tests/lib/backgroundjob/job.php b/tests/lib/backgroundjob/job.php
index fec9b0a792d..912e0e13b57 100644
--- a/tests/lib/backgroundjob/job.php
+++ b/tests/lib/backgroundjob/job.php
@@ -23,10 +23,17 @@ class Job extends \Test\TestCase {
});
$jobList->add($job);
+ $logger = $this->getMockBuilder('OCP\ILogger')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $logger->expects($this->once())
+ ->method('error')
+ ->with('Error while running background job: ');
+
$this->assertCount(1, $jobList->getAll());
- $job->execute($jobList);
+ $job->execute($jobList, $logger);
$this->assertTrue($this->run);
- $this->assertCount(0, $jobList->getAll());
+ $this->assertCount(1, $jobList->getAll());
}
public function markRun() {
='#n195'>195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
<?php

declare(strict_types=1);

/**
 * @copyright Copyright (c) 2016, ownCloud, Inc.
 *
 * @author Björn Schießle <bjoern@schiessle.org>
 * @author Morris Jobke <hey@morrisjobke.de>
 * @author Robin Appelman <robin@icewind.nl>
 * @author Robin McCorkell <robin@mccorkell.me.uk>
 * @author Roeland Jago Douma <roeland@famdouma.nl>
 *
 * @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 OC\Files\Mount;

use OCP\Cache\CappedMemoryCache;
use OC\Files\Filesystem;
use OC\Files\SetupManager;
use OC\Files\SetupManagerFactory;
use OCP\Files\Mount\IMountManager;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\NotFoundException;

class Manager implements IMountManager {
	/** @var MountPoint[] */
	private array $mounts = [];
	/** @var CappedMemoryCache<IMountPoint> */
	private CappedMemoryCache $pathCache;
	/** @var CappedMemoryCache<IMountPoint[]> */
	private CappedMemoryCache $inPathCache;
	private SetupManager $setupManager;

	public function __construct(SetupManagerFactory $setupManagerFactory) {
		$this->pathCache = new CappedMemoryCache();
		$this->inPathCache = new CappedMemoryCache();
		$this->setupManager = $setupManagerFactory->create($this);
	}

	/**
	 * @param IMountPoint $mount
	 */
	public function addMount(IMountPoint $mount) {
		$this->mounts[$mount->getMountPoint()] = $mount;
		$this->pathCache->clear();
		$this->inPathCache->clear();
	}

	/**
	 * @param string $mountPoint
	 */
	public function removeMount(string $mountPoint) {
		$mountPoint = Filesystem::normalizePath($mountPoint);
		if (\strlen($mountPoint) > 1) {
			$mountPoint .= '/';
		}
		unset($this->mounts[$mountPoint]);
		$this->pathCache->clear();
		$this->inPathCache->clear();
	}

	/**
	 * @param string $mountPoint
	 * @param string $target
	 */
	public function moveMount(string $mountPoint, string $target) {
		$this->mounts[$target] = $this->mounts[$mountPoint];
		unset($this->mounts[$mountPoint]);
		$this->pathCache->clear();
		$this->inPathCache->clear();
	}

	/**
	 * Find the mount for $path
	 *
	 * @param string $path
	 * @return IMountPoint
	 */
	public function find(string $path): IMountPoint {
		$this->setupManager->setupForPath($path);
		$path = Filesystem::normalizePath($path);

		if (isset($this->pathCache[$path])) {
			return $this->pathCache[$path];
		}

		$current = $path;
		while (true) {
			$mountPoint = $current . '/';
			if (isset($this->mounts[$mountPoint])) {
				$this->pathCache[$path] = $this->mounts[$mountPoint];
				return $this->mounts[$mountPoint];
			} elseif ($current === '') {
				break;
			}

			$current = dirname($current);
			if ($current === '.' || $current === '/') {
				$current = '';
			}
		}

		throw new NotFoundException("No mount for path " . $path . " existing mounts: " . implode(",", array_keys($this->mounts)));
	}

	/**
	 * Find all mounts in $path
	 *
	 * @param string $path
	 * @return IMountPoint[]
	 */
	public function findIn(string $path): array {
		$this->setupManager->setupForPath($path, true);
		$path = $this->formatPath($path);

		if (isset($this->inPathCache[$path])) {
			return $this->inPathCache[$path];
		}

		$result = [];
		$pathLength = \strlen($path);
		$mountPoints = array_keys($this->mounts);
		foreach ($mountPoints as $mountPoint) {
			if (substr($mountPoint, 0, $pathLength) === $path && \strlen($mountPoint) > $pathLength) {
				$result[] = $this->mounts[$mountPoint];
			}
		}

		$this->inPathCache[$path] = $result;
		return $result;
	}

	public function clear() {
		$this->mounts = [];
		$this->pathCache->clear();
		$this->inPathCache->clear();
	}

	/**
	 * Find mounts by storage id
	 *
	 * @param string $id
	 * @return IMountPoint[]
	 */
	public function findByStorageId(string $id): array {
		if (\strlen($id) > 64) {
			$id = md5($id);
		}
		$result = [];
		foreach ($this->mounts as $mount) {
			if ($mount->getStorageId() === $id) {
				$result[] = $mount;
			}
		}
		return $result;
	}

	/**
	 * @return IMountPoint[]
	 */
	public function getAll(): array {
		return $this->mounts;
	}

	/**
	 * Find mounts by numeric storage id
	 *
	 * @param int $id
	 * @return IMountPoint[]
	 */
	public function findByNumericId(int $id): array {
		$storageId = \OC\Files\Cache\Storage::getStorageId($id);
		return $this->findByStorageId($storageId);
	}

	/**
	 * @param string $path
	 * @return string
	 */
	private function formatPath(string $path): string {
		$path = Filesystem::normalizePath($path);
		if (\strlen($path) > 1) {
			$path .= '/';
		}
		return $path;
	}

	public function getSetupManager(): SetupManager {
		return $this->setupManager;
	}

	/**
	 * Return all mounts in a path from a specific mount provider
	 *
	 * @param string $path
	 * @param string[] $mountProviders
	 * @return MountPoint[]
	 */
	public function getMountsByMountProvider(string $path, array $mountProviders) {
		$this->getSetupManager()->setupForProvider($path, $mountProviders);
		if (in_array('', $mountProviders)) {
			return $this->mounts;
		} else {
			return array_filter($this->mounts, function ($mount) use ($mountProviders) {
				return in_array($mount->getMountProvider(), $mountProviders);
			});
		}
	}
}