Skip to content

Commit

Permalink
bug fix: destroy entity without complete FolderDeleteEntity.php job
Browse files Browse the repository at this point in the history
  • Loading branch information
temaotl committed Aug 27, 2024
1 parent 83b1227 commit a4480ba
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 29 deletions.
1 change: 0 additions & 1 deletion app/Jobs/DeleteFederation.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public function handle(): void

return;
}
dump($pathToDirectory);

$lockKey = 'directory-'.md5($pathToDirectory).'-lock';
$lock = Cache::lock($lockKey, 61);
Expand Down
25 changes: 15 additions & 10 deletions app/Jobs/FolderDeleteEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Facades\EntityFacade;
use App\Models\Entity;
use App\Models\Federation;
use App\Notifications\EntityStateChanged;
use App\Services\FederationService;
use App\Services\NotificationService;
Expand All @@ -25,14 +26,13 @@ class FolderDeleteEntity implements ShouldQueue
*/
use HandlesJobsFailuresTrait;

public Entity $entity;

/**
* Create a new job instance.
*/
public function __construct(Entity $entity)
public function __construct(public int $entityId,
public array $federationsIDs,
public string $file)
{
$this->entity = $entity;
}

/**
Expand All @@ -41,24 +41,29 @@ public function __construct(Entity $entity)
public function handle(): void
{

$entity = $this->entity;
$federations = $entity->federations;
foreach ($federations as $federation) {
foreach ($this->federationsIDs as $federationId) {

$federation = Federation::withTrashed()->find($federationId);
if (! $federation) {
continue;
}
try {
$pathToDirectory = FederationService::getFederationFolder($federation);
} catch (\Exception $e) {
$this->fail($e);
$this->fail($e->getMessage());

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

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

RunMdaScript::dispatch($federation, $lock->owner());
} catch (Exception $e) {
Expand Down
23 changes: 5 additions & 18 deletions app/Observers/EntityObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@

class EntityObserver implements ShouldHandleEventsAfterCommit
{
/**
* Handle the Entity "created" event.
*/
public function created(Entity $entity): void
{
//
}

/**
* Handle the Entity "updated" event.
*/
Expand Down Expand Up @@ -49,8 +41,11 @@ public function updated(Entity $entity): void
*/
public function deleted(Entity $entity): void
{
if (! $entity->isForceDeleting()) {
FolderDeleteEntity::dispatch($entity);
$ent = Entity::withTrashed()->find($entity->id);
if ($ent) {
$federationIDs = $entity->federations->pluck('id')->toArray();

FolderDeleteEntity::dispatch($entity->id, $federationIDs, $entity->file);

if ($entity->edugain == 1) {
EduGainDeleteEntity::dispatch($entity);
Expand All @@ -71,12 +66,4 @@ public function restored(Entity $entity): void
}
}
}

/**
* Handle the Entity "force deleted" event.
*/
public function forceDeleted(Entity $entity): void
{
//
}
}

0 comments on commit a4480ba

Please sign in to comment.