Skip to content

Commit

Permalink
Merge pull request #22868 from owncloud/stable8.2-backport-22865
Browse files Browse the repository at this point in the history
[stable8.2] Run cleanup of expired DB file locks to background job
  • Loading branch information
C. Montero Luque committed Mar 4, 2016
2 parents bf9be4b + 12e006c commit 0c03e7c
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 16 deletions.
2 changes: 1 addition & 1 deletion apps/files/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<shipped>true</shipped>
<standalone/>
<default_enable/>
<version>1.2.0</version>
<version>1.2.1</version>
<types>
<filesystem/>
</types>
Expand Down
22 changes: 22 additions & 0 deletions apps/files/appinfo/install.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* @author Morris Jobke <hey@morrisjobke.de>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @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/>
*
*/

\OC::$server->getJobList()->add('OCA\Files\BackgroundJob\CleanupFileLocks');
2 changes: 2 additions & 0 deletions apps/files/appinfo/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,5 @@ function owncloud_reset_encrypted_flag(\OCP\IDBConnection $conn) {
if(defined('DEBUG') && DEBUG === true) {
\OC::$server->getConfig()->setSystemValue('debug', true);
}

\OC::$server->getJobList()->add('OCA\Files\BackgroundJob\CleanupFileLocks');
57 changes: 57 additions & 0 deletions apps/files/lib/backgroundjob/cleanupfilelocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* @author Morris Jobke <hey@morrisjobke.de>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @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\Files\BackgroundJob;

use OC\BackgroundJob\TimedJob;
use OC\Lock\DBLockingProvider;

/**
* Clean up all file locks that are expired for the DB file locking provider
*/
class CleanupFileLocks extends TimedJob {

/**
* Default interval in minutes
*
* @var int $defaultIntervalMin
**/
protected $defaultIntervalMin = 5;

/**
* sets the correct interval for this timed job
*/
public function __construct() {
$this->interval = $this->defaultIntervalMin * 60;
}

/**
* Makes the background job do its work
*
* @param array $argument unused argument
*/
public function run($argument) {
$lockingProvider = \OC::$server->getLockingProvider();
if($lockingProvider instanceof DBLockingProvider) {
$lockingProvider->cleanExpiredLocks();
}
}
}
26 changes: 11 additions & 15 deletions lib/private/lock/dblockingprovider.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,17 @@ public function changeLock($path, $targetType) {
*/
public function cleanExpiredLocks() {
$expire = $this->timeFactory->getTime();
$this->connection->executeUpdate(
'DELETE FROM `*PREFIX*file_locks` WHERE `ttl` < ?',
[$expire]
);
try {
$this->connection->executeUpdate(
'DELETE FROM `*PREFIX*file_locks` WHERE `ttl` < ?',
[$expire]
);
} catch (\Exception $e) {
// If the table is missing, the clean up was successful
if ($this->connection->tableExists('file_locks')) {
throw $e;
}
}
}

/**
Expand All @@ -255,15 +262,4 @@ public function releaseAll() {
}
}
}

public function __destruct() {
try {
$this->cleanExpiredLocks();
} catch (\Exception $e) {
// If the table is missing, the clean up was successful
if ($this->connection->tableExists('file_locks')) {
throw $e;
}
}
}
}

0 comments on commit 0c03e7c

Please sign in to comment.