Skip to content

Commit

Permalink
rewrite to Locks Across Processes
Browse files Browse the repository at this point in the history
  • Loading branch information
temaotl committed Jul 9, 2024
1 parent 78a2bd6 commit 25e4537
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 51 deletions.
18 changes: 8 additions & 10 deletions app/Console/Commands/ValidateMetaConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Console\Commands;

use App\Jobs\FolderAddEntity;
use App\Models\Entity;
use App\Models\Federation;
use App\Traits\DumpFromGit\EntitiesHelp\FixEntityTrait;
Expand Down Expand Up @@ -76,35 +75,34 @@ private function meta()

private function runMDA(Federation $federation)
{
$filterArray = explode(", ", $federation->filters);
$filterArray = explode(', ', $federation->filters);

$scriptPath = config('storageCfg.mdaScript');
$command = "sh " . config('storageCfg.mdaScript');
$command = 'sh '.config('storageCfg.mdaScript');

$realScriptPath = realpath($scriptPath);

if ($realScriptPath === false) {
throw new Exception("file not exist" . $scriptPath);
throw new Exception('file not exist'.$scriptPath);
}

foreach ($filterArray as $filter) {
$file = escapeshellarg($filter) . '.xml';
$file = escapeshellarg($filter).'.xml';
$pipeline = 'main';
$command = 'sh ' . escapeshellarg($realScriptPath) . ' ' . $file . ' ' . $pipeline ;
$command = 'sh '.escapeshellarg($realScriptPath).' '.$file.' '.$pipeline;

$res = shell_exec($command);
$res = shell_exec($command);
dump($res);
}
}

public function handle()
{
$federation = Federation::where('id',1)->first();
$federation = Federation::where('id', 1)->first();
$this->runMDA($federation);


// $this->fixEntities();
// $this->doc();
// $this->doc();

}
}
49 changes: 16 additions & 33 deletions app/Jobs/FolderAddEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,9 @@ public function __construct(Entity $entity)

private function runMDA(Federation $federation)
{
$filterArray = explode(", ", $federation->filters);

$scriptPath = config('storageCfg.mdaScript');
$command = "sh " . config('storageCfg.mdaScript');

$realScriptPath = realpath($scriptPath);

if ($realScriptPath === false) {
throw new Exception("file not exist" . $scriptPath);
}

foreach ($filterArray as $filter) {
$file = escapeshellarg($filter) . '.xml';
$pipeline = 'main';
$command = 'sh ' . escapeshellarg($realScriptPath) . ' ' . $file . ' ' . $pipeline ;

$res = shell_exec($command);
dump($res);
}
}


/**
* Execute the job.
*/
Expand All @@ -69,25 +50,27 @@ public function handle(): void

foreach ($federationMembershipId as $fedId) {


$federation = Federation::where('id', $fedId->federation_id)->first();


if (!Storage::disk($diskName)->exists($federation->name)) {
if (! Storage::disk($diskName)->exists($federation->name)) {
continue;
}
$pathToDirectory = Storage::disk($diskName)->path($federation->name);
$lockKey = 'directory-' . md5($pathToDirectory) . '-lock';
$lock = Cache::lock($lockKey,120);

try {
EntityFacade::saveMetadataToFederationFolder($this->entity->id, $fedId->federation_id);
$this->runMDA($federation);
} catch (Exception $e)
{
Log::error($e->getMessage());
} finally {
$lock->release();
$lockKey = 'directory-'.md5($pathToDirectory).'-lock';
$lock = Cache::lock($lockKey, 120);

if ($lock->get()) {

try {
EntityFacade::saveMetadataToFederationFolder($this->entity->id, $fedId->federation_id);
RunMdaScript::dispatch($federation, $lock->owner());
$this->runMDA($federation);
} catch (Exception $e) {
Log::error($e->getMessage());
} finally {
$lock->release();
}

}

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

namespace App\Jobs;

use App\Models\Federation;
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 RunMdaScript implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public Federation $federation;

public string $owner;

/**
* Create a new job instance.
*/
public function __construct(Federation $federation, string $owner)
{
$this->federation = $federation;
$this->owner = $owner;
}

/**
* Execute the job.
*/
public function handle(): void
{

$diskName = config('storageCfg.name');
$pathToDirectory = Storage::disk($diskName)->path($this->federation->name);
$lockKey = 'directory-'.md5($pathToDirectory).'-lock';

$filterArray = explode(', ', $this->federation->filters);
$scriptPath = config('storageCfg.mdaScript');
$command = 'sh '.config('storageCfg.mdaScript');

$realScriptPath = realpath($scriptPath);

try {

foreach ($filterArray as $filter) {
$file = escapeshellarg($filter).'.xml';
$pipeline = 'main';
$command = 'sh '.escapeshellarg($realScriptPath).' '.$file.' '.$pipeline;

$res = shell_exec($command);
dump($res);
}

} catch (Exception $e) {
Log::error($e->getMessage());
} finally {
Cache::restoreLock($lockKey, $this->owner)->release();

}

}
}
2 changes: 1 addition & 1 deletion app/Listeners/SendUpdatedEntityToSaveJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct()
*/
public function handle(UpdateEntity $event): void
{
// Log::info('Listener triggered for UpdateEntity event', ['entity_id' => $event->entity->id]);
// Log::info('Listener triggered for UpdateEntity event', ['entity_id' => $event->entity->id]);
$ent = $event->entity;

if ($ent->wasChanged('xml_file')) {
Expand Down
1 change: 0 additions & 1 deletion app/Services/EntityService.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public function saveEntityMetadataToFolder($entity_id, $folderName): void
{
$diskName = config('storageCfg.name');


$entity = Entity::find($entity_id);
if (! $entity) {
throw new Exception("Entity not found with id $entity_id");
Expand Down
3 changes: 0 additions & 3 deletions app/Traits/FederationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

trait FederationTrait
{



public function createFederationFolder(string $name): void
{

Expand Down
7 changes: 4 additions & 3 deletions config/storageCfg.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

return [
'name' => 'metadata',
'mdaConfigFolder'=>'/home/artem/Desktop/cesnet/metadata/testdata/mda/config',
'mdaScript' => '/home/artem/Desktop/cesnet/metadata/testdata/mda/hello.sh'
/* 'mdaScript' => '/home/artem/Desktop/cesnet/metadata/testdata/mda/mda.sh',*/
'mdaConfigFolder' => '/home/artem/Desktop/cesnet/metadata/testdata/mda/config',
'mdaScript' => '/home/artem/Desktop/cesnet/metadata/testdata/mda/hello.sh',
/* 'mdaScript' => '/home/artem/Desktop/cesnet/metadata/testdata/mda/mda.sh',*/
];

0 comments on commit 25e4537

Please sign in to comment.