Skip to content

Commit

Permalink
rewrite work with federation folder to make less depends on naming
Browse files Browse the repository at this point in the history
  • Loading branch information
temaotl committed Jul 29, 2024
1 parent b44e559 commit 3c922e5
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 48 deletions.
14 changes: 10 additions & 4 deletions app/Jobs/DeleteFederation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
12 changes: 4 additions & 8 deletions app/Jobs/EduGainAddEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 3 additions & 8 deletions app/Jobs/EduGainDeleteEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 8 additions & 4 deletions app/Jobs/FolderAddEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);

Expand Down
14 changes: 9 additions & 5 deletions app/Jobs/FolderAddMembership.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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);

Expand Down
12 changes: 8 additions & 4 deletions app/Jobs/FolderDeleteEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 7 additions & 3 deletions app/Jobs/FolderDeleteMembership.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));

Expand Down
7 changes: 2 additions & 5 deletions app/Observers/FederationObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion app/Services/EntityService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
30 changes: 25 additions & 5 deletions app/Services/FederationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.');
}
}
}
2 changes: 1 addition & 1 deletion database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();*/
Expand Down

0 comments on commit 3c922e5

Please sign in to comment.