Skip to content
Open
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
14 changes: 8 additions & 6 deletions app/Models/Foundation/Main/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,10 @@ public function getCloudLink()
{
try {
$relativeLink = ltrim($this->getRelativeLinkFor(), '/');
return Storage::disk(Config::get('filesystems.assets_disk', 'assets') )->url($relativeLink);
}
catch (\Exception $ex){
$disk = Storage::disk(Config::get('filesystems.assets_disk', 'assets'));
if ($disk == null) return null;
return $disk->url($relativeLink);
} catch (\Exception $ex) {
Log::warning($ex);
return null;
}
Expand All @@ -294,9 +295,10 @@ public function getCloudLink()
public static function getCloudLinkForImages(string $imageRelativePath):?string {
try {
$imageRelativePath = ltrim($imageRelativePath, '/');
return Storage::disk(Config::get('filesystems.static_images_disk', 'static_images'))->url($imageRelativePath);
}
catch (\Exception $ex){
$disk = Storage::disk(Config::get('filesystems.static_images_disk', 'static_images'));
if ($disk == null) return null;
return $disk->url($imageRelativePath);
} catch (\Exception $ex) {
Log::warning($ex);
return null;
}
Expand Down
40 changes: 39 additions & 1 deletion app/Services/FileSystem/Swift/SwiftServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* limitations under the License.
**/

use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use League\Flysystem\Filesystem;
Expand All @@ -23,6 +25,35 @@
*/
final class SwiftServiceProvider extends ServiceProvider
{
const MAX_SKIPS = 4;

private function shouldBypass(): bool
{
$skip_until = Cache::tags(SwiftServiceProvider::class)->get('skip_until', Carbon::now());
return Carbon::now() < $skip_until;
}

private function recalculateSkipDelay()
{
$cache = Cache::tags(SwiftServiceProvider::class);
$skip_count = intval($cache->get('skip_count', 0));
$skip_delay = intval($cache->get('skip_delay', 1));
$skip_count++;
$skip_delay = $skip_delay * pow(2, $skip_count - 1);
$skip_until = Carbon::now()->add($skip_delay, 'minutes');
$cache->put('skip_count', $skip_count);
$cache->put('skip_delay', $skip_delay);
$cache->put('skip_until', $skip_until);
if ($skip_count > self::MAX_SKIPS) {
$this->resetSkips();
}
}

private function resetSkips()
{
Cache::tags(SwiftServiceProvider::class)->flush();
}

/**
* Register bindings in the container.
*
Expand All @@ -43,6 +74,10 @@ public function boot()
Storage::extend('swift', function ($app, $config) {

try {
if (empty($config["auth_url"]) || empty($config["region"]) || empty($config["container"])) return null;

if ($this->shouldBypass()) return null;

$configOptions = [
'authUrl' => $config["auth_url"],
'region' => $config["region"],
Expand Down Expand Up @@ -81,12 +116,15 @@ public function boot()

$container = $openstackClient->objectStoreV1()->getContainer($config["container"]);

$this->resetSkips();

return new Filesystem(new SwiftAdapter($container));
}
catch(\Exception $ex){
Log::error($ex);
$this->recalculateSkipDelay();
return null;
}
});
}
}
}