Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CI] Storage availability #13723

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/files_external/lib/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,9 @@ private static function getBackendStatus($class, $options, $isPersonal) {
if (class_exists($class)) {
try {
$storage = new $class($options);
return $storage->test($isPersonal);
$result = $storage->test($isPersonal);
$storage->setAvailability($result);
return $result;
} catch (Exception $exception) {
\OCP\Util::logException('files_external', $exception);
return false;
Expand Down
12 changes: 12 additions & 0 deletions db_structure.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@
<length>4</length>
</field>

<field>
<name>available</name>
<type>boolean</type>
<default>true</default>
<notnull>true</notnull>
</field>

<field>
<name>last_checked</name>
<type>integer</type>
</field>

<index>
<name>storages_id_index</name>
<unique>true</unique>
Expand Down
48 changes: 39 additions & 9 deletions lib/private/files/cache/storage.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* Copyright (c) 2013 Robin Appelman <[email protected]>
* Copyright (c) 2015 Robin McCorkell <[email protected]>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
Expand All @@ -21,26 +22,35 @@ class Storage {

/**
* @param \OC\Files\Storage\Storage|string $storage
* @param bool $isAvailable
*/
public function __construct($storage) {
public function __construct($storage, $isAvailable = true) {
if ($storage instanceof \OC\Files\Storage\Storage) {
$this->storageId = $storage->getId();
} else {
$this->storageId = $storage;
}
$this->storageId = self::adjustStorageId($this->storageId);

$sql = 'SELECT `numeric_id` FROM `*PREFIX*storages` WHERE `id` = ?';
$result = \OC_DB::executeAudited($sql, array($this->storageId));
if ($row = $result->fetchRow()) {
if ($row = self::getStorageById($this->storageId)) {
$this->numericId = $row['numeric_id'];
} else {
$sql = 'INSERT INTO `*PREFIX*storages` (`id`) VALUES(?)';
\OC_DB::executeAudited($sql, array($this->storageId));
$sql = 'INSERT INTO `*PREFIX*storages` (`id`, `available`) VALUES(?, ?)';
\OC_DB::executeAudited($sql, array($this->storageId, $isAvailable));
$this->numericId = \OC_DB::insertid('*PREFIX*storages');
}
}

/**
* @param string $storageId
* @return array|null
*/
public static function getStorageById($storageId) {
$sql = 'SELECT * FROM `*PREFIX*storages` WHERE `id` = ?';
$result = \OC_DB::executeAudited($sql, array($storageId));
return $result->fetchRow();
}

/**
* Adjusts the storage id to use md5 if too long
* @param string $storageId storage id
Expand Down Expand Up @@ -81,15 +91,35 @@ public static function getStorageId($numericId) {
public static function getNumericStorageId($storageId) {
$storageId = self::adjustStorageId($storageId);

$sql = 'SELECT `numeric_id` FROM `*PREFIX*storages` WHERE `id` = ?';
$result = \OC_DB::executeAudited($sql, array($storageId));
if ($row = $result->fetchRow()) {
if ($row = self::getStorageById($storageId)) {
return $row['numeric_id'];
} else {
return null;
}
}

/**
* @return array|null [ available, last_checked ]
*/
public function getAvailability() {
if ($row = self::getStorageById($this->storageId)) {
return [
'available' => $row['available'],
'last_checked' => $row['last_checked']
];
} else {
return null;
}
}

/**
* @param bool $isAvailable
*/
public function setAvailability($isAvailable) {
$sql = 'UPDATE `*PREFIX*storages` SET `available` = ?, `last_checked` = ? WHERE `id` = ?';
\OC_DB::executeAudited($sql, array($isAvailable, time(), $this->storageId));
}

/**
* @param string $storageId
* @return bool
Expand Down
17 changes: 17 additions & 0 deletions lib/private/files/storage/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,4 +451,21 @@ public function getDirectDownload($path) {
return [];
}

/**
* Get availability of the storage
*
* @return array [ available, last_checked ]
*/
public function getAvailability() {
return $this->getStorageCache()->getAvailability();
}

/**
* Set availability of the storage
*
* @param bool $isAvailable
*/
public function setAvailability($isAvailable) {
$this->getStorageCache()->setAvailability($isAvailable);
}
}
Loading