From 091c1de0a9e72eb4f2ca32f72626afba8e4de041 Mon Sep 17 00:00:00 2001 From: Pascal Baljet Date: Tue, 17 May 2022 23:38:51 +0200 Subject: [PATCH] Shutdown bugfix --- CHANGELOG.md | 8 ++++++++ src/UsesMinIOServer.php | 26 ++++++++++++++++++++++++-- tests/UsesMinIOServerTest.php | 5 ++++- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 63f769f..a6c9e52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ # Changelog All notable changes to `laravel-minio-testing-tools` will be documented in this file. + +## 1.0.1 - 2022-05-17 + +- Cleanup + bugfix + +## 1.0.9 - 2022-05-17 + +- First release \ No newline at end of file diff --git a/src/UsesMinIOServer.php b/src/UsesMinIOServer.php index aa71c1f..ef85932 100644 --- a/src/UsesMinIOServer.php +++ b/src/UsesMinIOServer.php @@ -17,6 +17,10 @@ trait UsesMinIOServer public Collection $minIODiskConfig; + public bool $minIODestroyed = false; + + public bool $minIOEnvironmentRestored = false; + public function bootUsesMinIOServer() { $this->startMinIOServer(); @@ -120,14 +124,24 @@ public function startMinIOServer(): bool $pid = $this->exec("minio server {$temporaryDirectory->path()} --address :{$this->minIOPort} > /dev/null 2>&1 & echo $!"); - register_shutdown_function(function () use ($pid, $temporaryDirectory) { + $killMinIOAndDeleteStorage = function () use ($pid, $temporaryDirectory) { + if ($this->minIODestroyed) { + return; + } + if ($pid) { $this->exec("kill {$pid}"); } // deleting the directory might fail when minio is not killed yet rescue(fn () => $temporaryDirectory->delete()); - }); + + $this->minIODestroyed = true; + }; + + $this->beforeApplicationDestroyed($killMinIOAndDeleteStorage); + + register_shutdown_function($killMinIOAndDeleteStorage); $tries = 0; @@ -216,6 +230,8 @@ public function updateEnvirionmentFile() file_put_contents($envFilename, $env); + $this->beforeApplicationDestroyed(fn () => $this->restoreEnvironmentFile()); + register_shutdown_function(fn () => $this->restoreEnvironmentFile()); } @@ -227,6 +243,10 @@ public function updateEnvirionmentFile() */ public function restoreEnvironmentFile() { + if ($this->minIOEnvironmentRestored) { + return; + } + $envFilename = base_path('.env'); if (!file_exists($envFilename)) { @@ -244,5 +264,7 @@ public function restoreEnvironmentFile() }); file_put_contents($envFilename, $env); + + $this->minIOEnvironmentRestored = true; } } diff --git a/tests/UsesMinIOServerTest.php b/tests/UsesMinIOServerTest.php index f22ed36..41252ac 100644 --- a/tests/UsesMinIOServerTest.php +++ b/tests/UsesMinIOServerTest.php @@ -1,11 +1,14 @@