Skip to content

Commit

Permalink
add restore federation job
Browse files Browse the repository at this point in the history
  • Loading branch information
temaotl committed Jul 26, 2024
1 parent c5f2ada commit b44e559
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 2 deletions.
49 changes: 49 additions & 0 deletions app/Jobs/RestoreFederation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Jobs;

use App\Facades\EntityFacade;
use App\Models\Membership;
use App\Traits\HandlesJobsFailuresTrait;
use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Mockery\Exception;

class RestoreFederation implements ShouldQueue
{
use Batchable, 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
{
if ($this->batch()->cancelled()) {
$this->fail(new Exception('batch was cancelled'));
}
try {
$federation = $this->membership->federation;
$entity = $this->membership->entity;
EntityFacade::saveMetadataToFederationFolder($entity->id, $federation->id);

} catch (Exception $e) {
$this->fail($e);
}

}
}
5 changes: 5 additions & 0 deletions app/Models/Federation.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public function entities()
->withTimestamps();
}

public function memberships()
{
return $this->hasMany(Membership::class);
}

public function scopeVisibleTo($query, User $user)
{
if ($user->admin) {
Expand Down
42 changes: 40 additions & 2 deletions app/Observers/FederationObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

namespace App\Observers;

use App\Jobs\DeleteFederation;
use App\Jobs\RestoreFederation;
use App\Jobs\RunMdaScript;
use App\Models\Federation;
use App\Services\FederationService;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;

class FederationObserver
Expand Down Expand Up @@ -34,15 +40,47 @@ public function updated(Federation $federation): void
*/
public function deleted(Federation $federation): void
{
//
if ($federation->approved) {
DeleteFederation::dispatch($federation);
}
}

/**
* Handle the Federation "restored" event.
*/
public function restored(Federation $federation): void
{
//
$memberships = $federation->memberships;
if ($memberships->count() == 0) {
return;
}

$jobs = [];
$diskName = config('storageCfg.name');
$pathToDirectory = Storage::disk($diskName)->path($federation->name);

foreach ($memberships as $membership) {
$jobs[] = new RestoreFederation($membership);
}
FederationService::createFederationFolder($federation->xml_id);
$lockKey = 'directory-'.md5($pathToDirectory).'-lock';
$lock = Cache::lock($lockKey, 120);

Log::info('Branch Start');
try {
$lock->block(120);
Bus::batch($jobs)->then(function () use ($federation, $lock) {
Log::info('Federation restored');
RunMdaScript::dispatch($federation, $lock->owner());
})->dispatch();
} catch (\Exception $e) {
Log::error($e->getMessage());
} finally {
if ($lock->isOwnedByCurrentProcess()) {
$lock->release();
}
}

}

/**
Expand Down
35 changes: 35 additions & 0 deletions database/migrations/2024_07_26_083341_create_job_batches_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('job_batches', function (Blueprint $table) {
$table->string('id')->primary();
$table->string('name');
$table->integer('total_jobs');
$table->integer('pending_jobs');
$table->integer('failed_jobs');
$table->longText('failed_job_ids');
$table->mediumText('options')->nullable();
$table->integer('cancelled_at')->nullable();
$table->integer('created_at');
$table->integer('finished_at')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('job_batches');
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,7 @@ public function an_admin_can_change_an_existing_federations_entities()
public function an_admin_can_purge_an_existing_federation()
{
Notification::fake();
Queue::fake();

$admin = User::factory()->create(['admin' => true]);
User::factory()->create(['admin' => true]);
Expand Down

0 comments on commit b44e559

Please sign in to comment.