From 25e453782d2dbc9a29ca1973e098f6c6504a854b Mon Sep 17 00:00:00 2001 From: Artem Otliaguzov Date: Tue, 9 Jul 2024 14:29:54 +0200 Subject: [PATCH] rewrite to Locks Across Processes --- app/Console/Commands/ValidateMetaConsole.php | 18 +++--- app/Jobs/FolderAddEntity.php | 49 +++++--------- app/Jobs/RunMdaScript.php | 68 ++++++++++++++++++++ app/Listeners/SendUpdatedEntityToSaveJob.php | 2 +- app/Services/EntityService.php | 1 - app/Traits/FederationTrait.php | 3 - config/storageCfg.php | 7 +- 7 files changed, 97 insertions(+), 51 deletions(-) create mode 100644 app/Jobs/RunMdaScript.php diff --git a/app/Console/Commands/ValidateMetaConsole.php b/app/Console/Commands/ValidateMetaConsole.php index d932106..f60d0ef 100644 --- a/app/Console/Commands/ValidateMetaConsole.php +++ b/app/Console/Commands/ValidateMetaConsole.php @@ -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; @@ -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(); } } diff --git a/app/Jobs/FolderAddEntity.php b/app/Jobs/FolderAddEntity.php index d406d0b..064c6b6 100644 --- a/app/Jobs/FolderAddEntity.php +++ b/app/Jobs/FolderAddEntity.php @@ -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. */ @@ -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(); + } + } } diff --git a/app/Jobs/RunMdaScript.php b/app/Jobs/RunMdaScript.php new file mode 100644 index 0000000..b71a677 --- /dev/null +++ b/app/Jobs/RunMdaScript.php @@ -0,0 +1,68 @@ +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(); + + } + + } +} diff --git a/app/Listeners/SendUpdatedEntityToSaveJob.php b/app/Listeners/SendUpdatedEntityToSaveJob.php index 98e06f1..26624b1 100644 --- a/app/Listeners/SendUpdatedEntityToSaveJob.php +++ b/app/Listeners/SendUpdatedEntityToSaveJob.php @@ -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')) { diff --git a/app/Services/EntityService.php b/app/Services/EntityService.php index 18975e9..1d0876d 100644 --- a/app/Services/EntityService.php +++ b/app/Services/EntityService.php @@ -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"); diff --git a/app/Traits/FederationTrait.php b/app/Traits/FederationTrait.php index a834978..be760cc 100644 --- a/app/Traits/FederationTrait.php +++ b/app/Traits/FederationTrait.php @@ -7,9 +7,6 @@ trait FederationTrait { - - - public function createFederationFolder(string $name): void { diff --git a/config/storageCfg.php b/config/storageCfg.php index 5188380..518618f 100644 --- a/config/storageCfg.php +++ b/config/storageCfg.php @@ -1,7 +1,8 @@ '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',*/ ];