Skip to content

Commit

Permalink
add new save membership job
Browse files Browse the repository at this point in the history
  • Loading branch information
temaotl committed Jul 18, 2024
1 parent d0a1cff commit 89f8ca6
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 24 deletions.
6 changes: 0 additions & 6 deletions app/Events/AddMembership.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
namespace App\Events;

use App\Models\Membership;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

Expand All @@ -17,13 +13,11 @@ class AddMembership

public Membership $membership;


/**
* Create a new event instance.
*/
public function __construct(Membership $membership)
{
$this->membership = $membership;
}

}
4 changes: 1 addition & 3 deletions app/Jobs/FolderAddEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Mockery\Exception;

Expand Down Expand Up @@ -50,7 +49,6 @@ public function handle(): void
->where('approved', 1)
->get();


$diskName = config('storageCfg.name');

foreach ($federationMembershipId as $fedId) {
Expand Down Expand Up @@ -80,7 +78,7 @@ public function handle(): void

RunMdaScript::dispatch($federation, $lock->owner());
} catch (Exception $e) {
Log::error($e->getMessage());
$this->fail($e);
} finally {
if ($lock->isOwnedByCurrentProcess()) {
$lock->release();
Expand Down
64 changes: 64 additions & 0 deletions app/Jobs/FolderAddMembership.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace App\Jobs;

use App\Facades\EntityFacade;
use App\Models\Entity;
use App\Models\Federation;
use App\Models\Membership;
use App\Traits\HandlesJobsFailuresTrait;
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;
use Mockery\Exception;

class FolderAddMembership implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
use HandlesJobsFailuresTrait;

public Membership $membership;

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

/**
* Execute the job.
*/
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();
}
$pathToDirectory = Storage::disk($diskName)->path($federation->name);
$lockKey = 'directory-'.md5($pathToDirectory).'-lock';
$lock = Cache::lock($lockKey, 61);

try {
$lock->block(61);
EntityFacade::saveMetadataToFederationFolder($entity->id, $federation->id);
RunMdaScript::dispatch($federation, $lock->owner());
} catch (Exception $e) {
$this->fail($e);
} finally {
if ($lock->isOwnedByCurrentProcess()) {
$lock->release();
}
}

}
}
30 changes: 30 additions & 0 deletions app/Jobs/FolderDeleteMembership.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

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

/**
* Create a new job instance.
*/
public function __construct()
{
//
}

/**
* Execute the job.
*/
public function handle(): void
{
//
}
}
14 changes: 3 additions & 11 deletions app/Listeners/SendNewMemberToSaveJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
namespace App\Listeners;

use App\Events\AddMembership;
use App\Jobs\EduGainAddEntity;
use App\Jobs\FolderAddEntity;
use App\Models\Entity;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;
use App\Jobs\FolderAddMembership;

class SendNewMemberToSaveJob
{
Expand All @@ -25,12 +20,9 @@ public function __construct()
*/
public function handle(AddMembership $event): void
{
$entityId = $event->membership->entity_id;
$entity = Entity::find($entityId);


if ($entity->approved == 1 && $event->membership->approved == 1 ) {
Log::info(" dispatch FolderAddEntity from Listener SendNewMemberToSaveJob");
if ($event->membership->approved == 1) {
FolderAddMembership::dispatch($event->membership);
}

}
Expand Down
5 changes: 3 additions & 2 deletions app/Listeners/SendUpdatedEntityToSaveJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Jobs\EduGainDeleteEntity;
use App\Jobs\FolderAddEntity;
use App\Services\NotificationService;
use Illuminate\Support\Facades\Log;

class SendUpdatedEntityToSaveJob
{
Expand All @@ -26,11 +27,11 @@ public function handle(UpdateEntity $event): void

$entity = $event->entity;

if ($entity->wasChanged('xml_file') ||
($entity->wasChanged('approved') && $entity->approved == 1)
if ($entity->wasChanged('xml_file')
) {
FolderAddEntity::dispatch($event->entity);
} elseif ($entity->approved == 1 && ! $entity->wasChanged('edugain')) {
Log::info('update some entity in SendUpdatedEntityToSaveJob');
NotificationService::sendUpdateNotification($entity);
}
if ($entity->wasChanged('edugain')) {
Expand Down
3 changes: 1 addition & 2 deletions app/Models/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public function approver()

protected $dispatchesEvents = [
'updated' => AddMembership::class,
// 'deleted' => DeleteEntity::class,
// 'deleted' => DeleteEntity::class,

];

}

0 comments on commit 89f8ca6

Please sign in to comment.