Skip to content

Commit

Permalink
write working EduToEdugain jobs but without mda script
Browse files Browse the repository at this point in the history
  • Loading branch information
temaotl committed Jul 13, 2024
1 parent 9e99685 commit e612537
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/Jobs/EduGainAddEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function handle(): void
$lock = Cache::lock($lockKey, 61);
try {
$lock->block(61);
EntityFacade::saveMetadataToFederationFolder($this->entity->id,$folderName);
EntityFacade::saveEntityMetadataToFolder($this->entity->id,$folderName);

//TODO write custom function to run special MDA script (ask about this)

Expand Down
69 changes: 69 additions & 0 deletions app/Jobs/EduGainDeleteEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace App\Jobs;

use App\Facades\EntityFacade;
use App\Models\Entity;
use App\Traits\HandlesJobsFailuresTrait;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;

class EduGainDeleteEntity implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
* trait with failure function
*/
use HandlesJobsFailuresTrait;

public Entity $entity;

/**
* Create a new job instance.
*/
public function __construct(Entity $entity)
{
$this->entity = $entity;
}

/**
* Execute the job.
*/
public function handle(): void
{
$diskName = config('storageCfg.name');
$folderName = config('storageCfg.edu2edugain');
try {
if (! Storage::disk($diskName)->exists($folderName)) {
throw new Exception("No $folderName in $diskName");
}
} catch (Exception $e) {
$this->fail($e);
}
$pathToDirectory = Storage::disk($diskName)->path($folderName);
$lockKey = 'directory-'.md5($pathToDirectory).'-lock';
$lock = Cache::lock($lockKey, 61);
try {
$lock->block(61);
EntityFacade::deleteEntityMetadataFromFolder($this->entity->file,$folderName);

//TODO write custom function to run special MDA script (ask about this)

// RunMdaScript::dispatch($federation, $lock->owner());
} catch (Exception $e) {
Log::error($e->getMessage());
} finally {
if ($lock->isOwnedByCurrentProcess()) {
$lock->release();
}
}
}
}
2 changes: 1 addition & 1 deletion app/Jobs/FolderDeleteEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function handle(): void
$lockKey = 'directory-'.md5($pathToDirectory).'-lock';
$lock = Cache::lock($lockKey, 61);
try {
$lock->block(120);
$lock->block(61);
EntityFacade::deleteEntityMetadataFromFolder($entity->file, $federation->xml_id);

NotificationService::sendEntityNotification($entity,EntityStateChanged::class);
Expand Down
6 changes: 6 additions & 0 deletions app/Listeners/SendCreatedEntityToSaveJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Listeners;

use App\Events\CreateEntity;
use App\Jobs\EduGainAddEntity;
use App\Jobs\FolderAddEntity;

class SendCreatedEntityToSaveJob
Expand All @@ -23,6 +24,11 @@ public function handle(CreateEntity $event): void

if ($event->entity->approved == 1) {
FolderAddEntity::dispatch($event->entity);

if($event->entity->edugain == 1){
EduGainAddEntity::dispatch($event->entity);
}

}
}
}
12 changes: 9 additions & 3 deletions app/Listeners/SendDeletedEntityToDeleteJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace App\Listeners;

use App\Events\DeleteEntity;
use App\Facades\EntityFacade;
use App\Jobs\EduGainDeleteEntity;
use App\Jobs\FolderDeleteEntity;
use Illuminate\Support\Facades\Log;


class SendDeletedEntityToDeleteJob
{
Expand All @@ -22,6 +22,12 @@ public function __construct()
*/
public function handle(DeleteEntity $event): void
{
FolderDeleteEntity::dispatch($event->entity);
FolderDeleteEntity::dispatch($event->entity);

if($event->entity->edugain == 1){
EduGainDeleteEntity::dispatch($event->entity);
}


}
}
17 changes: 14 additions & 3 deletions app/Listeners/SendUpdatedEntityToSaveJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Listeners;

use App\Events\UpdateEntity;
use App\Jobs\EduGainAddEntity;
use App\Jobs\EduGainDeleteEntity;
use App\Jobs\FolderAddEntity;
use App\Models\User;
use App\Notifications\EntityUpdated;
Expand All @@ -27,17 +29,26 @@ public function handle(UpdateEntity $event): void

$entity = $event->entity;



if ($entity->wasChanged('xml_file') ||
($entity->wasChanged('approved') && $entity->approved == 1)
)
{
FolderAddEntity::dispatch($event->entity);
}
elseif ($entity->approved == 1)
elseif ($entity->approved == 1 && !$entity->wasChanged('edugain'))
{
NotificationService::sendEntityNotification($entity,EntityUpdated::class);
}
if($entity->wasChanged('edugain'))
{
if($entity->edugain == 1) {
EduGainAddEntity::dispatch($entity);
} else {
EduGainDeleteEntity::dispatch($entity);
}
}



}
}

0 comments on commit e612537

Please sign in to comment.