Merge pull request #20790 from owncloud/deduplicate-trashbin-jobs

Deduplicate queued trashbin expire jobs
This commit is contained in:
Thomas Müller 2015-11-27 16:04:03 +01:00
commit 99c28a6ce6
3 changed files with 12 additions and 26 deletions

View File

@ -36,18 +36,11 @@ class Expire implements ICommand {
*/ */
private $user; private $user;
/**
* @var int
*/
private $trashBinSize;
/** /**
* @param string $user * @param string $user
* @param int $trashBinSize
*/ */
function __construct($user, $trashBinSize) { function __construct($user) {
$this->user = $user; $this->user = $user;
$this->trashBinSize = $trashBinSize;
} }
public function handle() { public function handle() {
@ -59,7 +52,7 @@ class Expire implements ICommand {
\OC_Util::tearDownFS(); \OC_Util::tearDownFS();
\OC_Util::setupFS($this->user); \OC_Util::setupFS($this->user);
Trashbin::expire($this->trashBinSize, $this->user); Trashbin::expire($this->user);
\OC_Util::tearDownFS(); \OC_Util::tearDownFS();
} }
} }

View File

@ -186,7 +186,6 @@ class Trashbin {
// get the user for which the filesystem is setup // get the user for which the filesystem is setup
$root = Filesystem::getRoot(); $root = Filesystem::getRoot();
list(, $user) = explode('/', $root); list(, $user) = explode('/', $root);
$size = 0;
list($owner, $ownerPath) = self::getUidAndFilename($file_path); list($owner, $ownerPath) = self::getUidAndFilename($file_path);
$ownerView = new \OC\Files\View('/' . $owner); $ownerView = new \OC\Files\View('/' . $owner);
@ -207,8 +206,6 @@ class Trashbin {
$location = $path_parts['dirname']; $location = $path_parts['dirname'];
$timestamp = time(); $timestamp = time();
$userTrashSize = self::getTrashbinSize($user);
// disable proxy to prevent recursive calls // disable proxy to prevent recursive calls
$trashPath = '/files_trashbin/files/' . $filename . '.d' . $timestamp; $trashPath = '/files_trashbin/files/' . $filename . '.d' . $timestamp;
@ -238,7 +235,6 @@ class Trashbin {
$trashStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $trashInternalPath); $trashStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $trashInternalPath);
if ($sizeOfAddedFiles !== false) { if ($sizeOfAddedFiles !== false) {
$size = $sizeOfAddedFiles;
$query = \OC_DB::prepare("INSERT INTO `*PREFIX*files_trash` (`id`,`timestamp`,`location`,`user`) VALUES (?,?,?,?)"); $query = \OC_DB::prepare("INSERT INTO `*PREFIX*files_trash` (`id`,`timestamp`,`location`,`user`) VALUES (?,?,?,?)");
$result = $query->execute(array($filename, $timestamp, $location, $owner)); $result = $query->execute(array($filename, $timestamp, $location, $owner));
if (!$result) { if (!$result) {
@ -247,7 +243,7 @@ class Trashbin {
\OCP\Util::emitHook('\OCA\Files_Trashbin\Trashbin', 'post_moveToTrash', array('filePath' => \OC\Files\Filesystem::normalizePath($file_path), \OCP\Util::emitHook('\OCA\Files_Trashbin\Trashbin', 'post_moveToTrash', array('filePath' => \OC\Files\Filesystem::normalizePath($file_path),
'trashPath' => \OC\Files\Filesystem::normalizePath($filename . '.d' . $timestamp))); 'trashPath' => \OC\Files\Filesystem::normalizePath($filename . '.d' . $timestamp)));
$size += self::retainVersions($filename, $owner, $ownerPath, $timestamp); self::retainVersions($filename, $owner, $ownerPath, $timestamp);
// if owner !== user we need to also add a copy to the owners trash // if owner !== user we need to also add a copy to the owners trash
if ($user !== $owner) { if ($user !== $owner) {
@ -255,14 +251,11 @@ class Trashbin {
} }
} }
$userTrashSize += $size; self::scheduleExpire($user);
self::scheduleExpire($userTrashSize, $user);
// if owner !== user we also need to update the owners trash size // if owner !== user we also need to update the owners trash size
if ($owner !== $user) { if ($owner !== $user) {
$ownerTrashSize = self::getTrashbinSize($owner); self::scheduleExpire($owner);
$ownerTrashSize += $size;
self::scheduleExpire($ownerTrashSize, $owner);
} }
return ($sizeOfAddedFiles === false) ? false : true; return ($sizeOfAddedFiles === false) ? false : true;
@ -628,17 +621,17 @@ class Trashbin {
$freeSpace = self::calculateFreeSpace($size, $user); $freeSpace = self::calculateFreeSpace($size, $user);
if ($freeSpace < 0) { if ($freeSpace < 0) {
self::scheduleExpire($size, $user); self::scheduleExpire($user);
} }
} }
/** /**
* clean up the trash bin * clean up the trash bin
* *
* @param int $trashBinSize current size of the trash bin
* @param string $user * @param string $user
*/ */
public static function expire($trashBinSize, $user) { public static function expire($user) {
$trashBinSize = self::getTrashbinSize($user);
$availableSpace = self::calculateFreeSpace($trashBinSize, $user); $availableSpace = self::calculateFreeSpace($trashBinSize, $user);
$size = 0; $size = 0;
@ -654,15 +647,15 @@ class Trashbin {
$size += self::deleteFiles(array_slice($dirContent, $count), $user, $availableSpace); $size += self::deleteFiles(array_slice($dirContent, $count), $user, $availableSpace);
} }
/**@param int $trashBinSize current size of the trash bin /**
* @param string $user * @param string $user
*/ */
private static function scheduleExpire($trashBinSize, $user) { private static function scheduleExpire($user) {
// let the admin disable auto expire // let the admin disable auto expire
$application = new Application(); $application = new Application();
$expiration = $application->getContainer()->query('Expiration'); $expiration = $application->getContainer()->query('Expiration');
if ($expiration->isEnabled()) { if ($expiration->isEnabled()) {
\OC::$server->getCommandBus()->push(new Expire($user, $trashBinSize)); \OC::$server->getCommandBus()->push(new Expire($user));
} }
} }

View File

@ -26,7 +26,7 @@ use Test\TestCase;
class ExpireTest extends TestCase { class ExpireTest extends TestCase {
public function testExpireNonExistingUser() { public function testExpireNonExistingUser() {
$command = new Expire('test', 0); $command = new Expire('test');
$command->handle(); $command->handle();
$this->assertTrue(true); $this->assertTrue(true);