From 3c922e5e11ff46f6f5a802e7b2e47ba5b72c9fb5 Mon Sep 17 00:00:00 2001 From: Artem Otliaguzov Date: Mon, 29 Jul 2024 10:50:48 +0200 Subject: [PATCH] rewrite work with federation folder to make less depends on naming --- app/Jobs/DeleteFederation.php | 14 +++++++++---- app/Jobs/EduGainAddEntity.php | 12 ++++------- app/Jobs/EduGainDeleteEntity.php | 11 +++------- app/Jobs/FolderAddEntity.php | 12 +++++++---- app/Jobs/FolderAddMembership.php | 14 ++++++++----- app/Jobs/FolderDeleteEntity.php | 12 +++++++---- app/Jobs/FolderDeleteMembership.php | 10 +++++++--- app/Observers/FederationObserver.php | 7 ++----- app/Services/EntityService.php | 2 +- app/Services/FederationService.php | 30 +++++++++++++++++++++++----- database/seeders/DatabaseSeeder.php | 2 +- 11 files changed, 78 insertions(+), 48 deletions(-) diff --git a/app/Jobs/DeleteFederation.php b/app/Jobs/DeleteFederation.php index 941651d..d7083ba 100644 --- a/app/Jobs/DeleteFederation.php +++ b/app/Jobs/DeleteFederation.php @@ -11,7 +11,6 @@ use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Cache; -use Illuminate\Support\Facades\Storage; use Mockery\Exception; class DeleteFederation implements ShouldQueue @@ -38,13 +37,20 @@ public function __construct(Federation $federation) */ public function handle(): void { - $diskName = config('storageCfg.name'); - $pathToDirectory = Storage::disk($diskName)->path($this->federation->name); + + try { + $pathToDirectory = FederationService::getFederationFolder($this->federation); + } catch (\Exception $e) { + $this->fail($e); + + return; + } + $lockKey = 'directory-'.md5($pathToDirectory).'-lock'; $lock = Cache::lock($lockKey, 61); try { $lock->block(61); - FederationService::DeleteFederationFolder($this->federation->name); + FederationService::deleteFederationFolder($this->federation); } catch (Exception $e) { $this->fail($e); diff --git a/app/Jobs/EduGainAddEntity.php b/app/Jobs/EduGainAddEntity.php index 18ee149..6215a07 100644 --- a/app/Jobs/EduGainAddEntity.php +++ b/app/Jobs/EduGainAddEntity.php @@ -40,17 +40,13 @@ public function handle(): void { $diskName = config('storageCfg.name'); $folderName = config('storageCfg.edu2edugain'); + if (! Storage::disk($diskName)->exists($folderName)) { - $this->makeEdu2Edugain(); - } + $this->fail(new Exception("no $folderName in Disk")); - try { - if (! Storage::disk($diskName)->exists($folderName)) { - throw new Exception("No $folderName in $diskName"); - } - } catch (Exception $e) { - $this->fail($e); + return; } + $pathToDirectory = Storage::disk($diskName)->path($folderName); $lockKey = 'directory-'.md5($pathToDirectory).'-lock'; $lock = Cache::lock($lockKey, 61); diff --git a/app/Jobs/EduGainDeleteEntity.php b/app/Jobs/EduGainDeleteEntity.php index ee1182a..584afb9 100644 --- a/app/Jobs/EduGainDeleteEntity.php +++ b/app/Jobs/EduGainDeleteEntity.php @@ -46,16 +46,11 @@ public function handle(): void $folderName = config('storageCfg.edu2edugain'); if (! Storage::disk($diskName)->exists($folderName)) { - $this->makeEdu2Edugain(); - } + $this->fail(new Exception("No $folderName in Disk")); - try { - if (! Storage::disk($diskName)->exists($folderName)) { - throw new Exception("No $folderName in $diskName"); - } - } catch (Exception $e) { - $this->fail($e); + return; } + $pathToDirectory = Storage::disk($diskName)->path($folderName); $lockKey = 'directory-'.md5($pathToDirectory).'-lock'; $lock = Cache::lock($lockKey, 61); diff --git a/app/Jobs/FolderAddEntity.php b/app/Jobs/FolderAddEntity.php index 0a95c70..378ef78 100644 --- a/app/Jobs/FolderAddEntity.php +++ b/app/Jobs/FolderAddEntity.php @@ -8,6 +8,7 @@ use App\Models\Membership; use App\Notifications\EntityStateChanged; use App\Notifications\EntityUpdated; +use App\Services\FederationService; use App\Services\NotificationService; use App\Traits\HandlesJobsFailuresTrait; use Illuminate\Bus\Queueable; @@ -17,7 +18,6 @@ use Illuminate\Queue\Middleware\WithoutOverlapping; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Cache; -use Illuminate\Support\Facades\Storage; use Mockery\Exception; class FolderAddEntity implements ShouldQueue @@ -55,10 +55,14 @@ public function handle(): void $federation = Federation::where('id', $fedId->federation_id)->first(); - if (! Storage::disk($diskName)->exists($federation->name)) { - continue; + try { + $pathToDirectory = FederationService::getFederationFolder($federation); + } catch (\Exception $e) { + $this->fail($e); + + return; } - $pathToDirectory = Storage::disk($diskName)->path($federation->name); + $lockKey = 'directory-'.md5($pathToDirectory).'-lock'; $lock = Cache::lock($lockKey, 61); diff --git a/app/Jobs/FolderAddMembership.php b/app/Jobs/FolderAddMembership.php index e785474..bd676a0 100644 --- a/app/Jobs/FolderAddMembership.php +++ b/app/Jobs/FolderAddMembership.php @@ -8,6 +8,7 @@ use App\Models\Membership; use App\Notifications\EntityAddedToHfd; use App\Notifications\MembershipAccepted; +use App\Services\FederationService; use App\Services\NotificationService; use App\Traits\HandlesJobsFailuresTrait; use Illuminate\Bus\Queueable; @@ -17,7 +18,6 @@ use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Log; -use Illuminate\Support\Facades\Storage; use Mockery\Exception; class FolderAddMembership implements ShouldQueue @@ -43,11 +43,15 @@ public function handle(): void Log::info('MEMBERSHIP START'); $federation = Federation::find($this->membership->federation_id); $entity = Entity::find($this->membership->entity_id); - $diskName = config('storageCfg.name'); - if (! Storage::disk($diskName)->exists($federation->name)) { - $this->fail(); + + try { + $pathToDirectory = FederationService::getFederationFolder($federation); + } catch (\Exception $e) { + $this->fail($e); + + return; } - $pathToDirectory = Storage::disk($diskName)->path($federation->name); + $lockKey = 'directory-'.md5($pathToDirectory).'-lock'; $lock = Cache::lock($lockKey, 61); diff --git a/app/Jobs/FolderDeleteEntity.php b/app/Jobs/FolderDeleteEntity.php index fe348a6..0c5114e 100644 --- a/app/Jobs/FolderDeleteEntity.php +++ b/app/Jobs/FolderDeleteEntity.php @@ -5,6 +5,7 @@ use App\Facades\EntityFacade; use App\Models\Entity; use App\Notifications\EntityStateChanged; +use App\Services\FederationService; use App\Services\NotificationService; use App\Traits\HandlesJobsFailuresTrait; use Illuminate\Bus\Queueable; @@ -13,7 +14,6 @@ use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Cache; -use Illuminate\Support\Facades\Storage; use Mockery\Exception; class FolderDeleteEntity implements ShouldQueue @@ -45,10 +45,14 @@ public function handle(): void $federations = $entity->federations; $diskName = config('storageCfg.name'); foreach ($federations as $federation) { - if (! Storage::disk($diskName)->exists($federation->name)) { - continue; + + try { + $pathToDirectory = FederationService::getFederationFolder($federation); + } catch (\Exception $e) { + $this->fail($e); + + return; } - $pathToDirectory = Storage::disk($diskName)->path($federation->name); $lockKey = 'directory-'.md5($pathToDirectory).'-lock'; $lock = Cache::lock($lockKey, 61); try { diff --git a/app/Jobs/FolderDeleteMembership.php b/app/Jobs/FolderDeleteMembership.php index c7c5800..1ec40ba 100644 --- a/app/Jobs/FolderDeleteMembership.php +++ b/app/Jobs/FolderDeleteMembership.php @@ -6,6 +6,7 @@ use App\Models\Entity; use App\Models\Federation; use App\Notifications\MembershipRejected; +use App\Services\FederationService; use App\Services\NotificationService; use App\Traits\HandlesJobsFailuresTrait; use Illuminate\Bus\Queueable; @@ -45,10 +46,13 @@ public function handle(): void $federation = $this->federation; $entity = $this->entity; $diskName = config('storageCfg.name'); - if (! Storage::disk($diskName)->exists($federation->name)) { - $this->fail(); + + try { + $pathToFile = FederationService::getFederationFolder($federation).'/'.$entity->file; + } catch (\Exception $e) { + $this->fail($e); } - $pathToFile = $federation->name.'/'.$entity->file; + if (! Storage::disk($diskName)->exists($pathToFile)) { NotificationService::sendModelNotification($entity, new MembershipRejected($entity->entityid, $federation->name)); diff --git a/app/Observers/FederationObserver.php b/app/Observers/FederationObserver.php index 65f667a..ede271d 100644 --- a/app/Observers/FederationObserver.php +++ b/app/Observers/FederationObserver.php @@ -27,11 +27,8 @@ public function created(Federation $federation): void */ public function updated(Federation $federation): void { - $diskName = config('storageCfg.name'); if ($federation->approved && $federation->wasChanged('approved')) { - if (! Storage::disk($diskName)->exists($federation->name)) { - FederationService::createFederationFolder($federation->name); - } + FederationService::createFederationFolder($federation); } } @@ -62,7 +59,7 @@ public function restored(Federation $federation): void foreach ($memberships as $membership) { $jobs[] = new RestoreFederation($membership); } - FederationService::createFederationFolder($federation->xml_id); + FederationService::createFederationFolder($federation); $lockKey = 'directory-'.md5($pathToDirectory).'-lock'; $lock = Cache::lock($lockKey, 120); diff --git a/app/Services/EntityService.php b/app/Services/EntityService.php index cca6d64..3b60515 100644 --- a/app/Services/EntityService.php +++ b/app/Services/EntityService.php @@ -22,7 +22,7 @@ public function saveMetadataToFederationFolder($entity_id, $federation_id): void if (! $federation) { throw new Exception("Federation $federation_id not found"); } - $this->saveEntityMetadataToFolder($entity_id, $federation->name); + $this->saveEntityMetadataToFolder($entity_id, $federation->xml_id); } /** diff --git a/app/Services/FederationService.php b/app/Services/FederationService.php index e223903..cbcb8e2 100644 --- a/app/Services/FederationService.php +++ b/app/Services/FederationService.php @@ -7,10 +7,15 @@ class FederationService { - public static function createFederationFolder(string $name): void + public static function createFederationFolder(Federation $federation): void { - Storage::disk(config('storageCfg.name'))->makeDirectory($name); + Storage::disk(config('storageCfg.name'))->makeDirectory($federation->xml_id); + } + + public static function createEdu2EduGainFolder(): void + { + Storage::disk(config('storageCfg.name'))->makeDirectory(config('storageCfg.edu2edugain')); } public static function createFoldersToAllFederation(): void @@ -19,14 +24,29 @@ public static function createFoldersToAllFederation(): void foreach ($federations as $fed) { if (! Storage::disk(config('storageCfg.name'))->exists($fed['xml_id'])) { - self::createFederationFolder($fed['xml_id']); + self::createFederationFolder($fed); } } } - public static function DeleteFederationFolder(string $name): void + public static function deleteFederationFolder(Federation $federation): void { + $diskName = config('storageCfg.name'); - Storage::disk($diskName)->deleteDirectory($name); + $folderName = $federation->xml_id; + + Storage::disk($diskName)->deleteDirectory($folderName); + } + + public static function getFederationFolder(Federation $federation): string + { + $disk = Storage::disk(config('storageCfg.name')); + $folderPath = $disk->path($federation['xml_id']); + + if ($disk->exists($federation['xml_id'])) { + return $folderPath; + } else { + throw new \Exception('Directory does not exist.'); + } } } diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 6ebc835..325cd54 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -32,7 +32,7 @@ public function run() User::factory()->create(['active' => true]); User::factory(96)->create(); - FederationService::createFederationFolder(config('storageCfg.edu2edugain')); + FederationService::createEdu2EduGainFolder(); /* Federation::factory(20)->create(); Entity::factory(100)->create();*/