Skip to content

Commit

Permalink
make better FolderDeleteEntity.php
Browse files Browse the repository at this point in the history
  • Loading branch information
temaotl committed Sep 25, 2024
1 parent ba425ee commit 6ede930
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
49 changes: 40 additions & 9 deletions app/Jobs/FolderDeleteEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Mockery\Exception;

class FolderDeleteEntity implements ShouldQueue
Expand All @@ -29,10 +30,32 @@ class FolderDeleteEntity implements ShouldQueue
/**
* Create a new job instance.
*/
public function __construct(public int $entityId,
public array $federationsIDs,
public string $file)
private int $entityId;

private array $federationsIDs;

private string $file;

public function __construct(int $entityId, array $federationsIDs, string $file)
{
$this->entityId = $entityId;
$this->federationsIDs = $federationsIDs;
$this->file = $file;
}

public function getEntityId(): int
{
return $this->entityId;
}

public function getFederationsIDs(): array
{
return $this->federationsIDs;
}

public function getFile(): string
{
return $this->file;
}

/**
Expand All @@ -41,7 +64,7 @@ public function __construct(public int $entityId,
public function handle(): void
{

foreach ($this->federationsIDs as $federationId) {
foreach ($this->getFederationsIDs() as $federationId) {

$federation = Federation::withTrashed()->find($federationId);
if (! $federation) {
Expand All @@ -50,27 +73,35 @@ public function handle(): void
try {
$pathToDirectory = FederationService::getFederationFolder($federation);
} catch (\Exception $e) {
$this->fail($e->getMessage());
$this->fail($e);

return;
}
$lockKey = 'directory-'.md5($pathToDirectory).'-lock';
$lock = Cache::lock($lockKey, 61);
$lock = Cache::lock($lockKey, config('constants.lock_constant'));
try {
$lock->block(61);
EntityFacade::deleteEntityMetadataFromFolder($this->file, $federation->xml_id);
$lock->block(config('constants.lock_constant'));
EntityFacade::deleteEntityMetadataFromFolder($this->getFile(), $federation->xml_id);

$entity = Entity::withTrashed()->find($this->entityId);
$entity = Entity::withTrashed()->find($this->getEntityId());
if ($entity) {
NotificationService::sendModelNotification($entity, new EntityStateChanged($entity));
}

if ($lock->owner() === null) {
Log::warning("Lock owner is null for key: $lockKey");

return;
}

RunMdaScript::dispatch($federation->id, $lock->owner());
} catch (Exception $e) {
$this->fail($e);
} finally {
if ($lock->isOwnedByCurrentProcess()) {
$lock->release();
} else {
Log::warning("Lock not owned by current process or lock lost for key: $lockKey");
}
}

Expand Down
6 changes: 6 additions & 0 deletions config/constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return [
'lock_constant' => 61,

];

0 comments on commit 6ede930

Please sign in to comment.