From ce4749dbfd3ffda76ebd4ec3214ec3e8646a89d4 Mon Sep 17 00:00:00 2001 From: Michel Bardelmeijer Date: Sun, 13 Oct 2019 14:17:31 +0200 Subject: [PATCH 1/6] Add graceful handler for SIGINT --- src/Console/DuskCommand.php | 61 ++++++++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/src/Console/DuskCommand.php b/src/Console/DuskCommand.php index 0a7820489..5c2661643 100644 --- a/src/Console/DuskCommand.php +++ b/src/Console/DuskCommand.php @@ -7,6 +7,7 @@ use Illuminate\Support\Str; use Symfony\Component\Finder\Finder; use Symfony\Component\Process\Exception\RuntimeException; +use Symfony\Component\Process\Exception\ProcessSignaledException; use Symfony\Component\Process\Process; class DuskCommand extends Command @@ -68,9 +69,15 @@ public function handle() $this->output->writeln('Warning: '.$e->getMessage()); } - return $process->run(function ($type, $line) { - $this->output->write($line); - }); + try { + return $process->run(function ($type, $line) { + $this->output->write($line); + }); + } catch (ProcessSignaledException $e) { + if ($e->getSignal() !== 2) { + throw $e; + } + } }); } @@ -158,6 +165,22 @@ protected function purgeConsoleLogs() * @return mixed */ protected function withDuskEnvironment($callback) + { + $this->setupDuskEnvironment(); + + try { + $callback(); + } finally { + $this->teardownDuskEnviroment(); + } + } + + /** + * Restore the original environment. + * + * @return void + */ + protected function setupDuskEnvironment() { if (file_exists(base_path($this->duskFile()))) { if (file_get_contents(base_path('.env')) !== file_get_contents(base_path($this->duskFile()))) { @@ -169,15 +192,37 @@ protected function withDuskEnvironment($callback) $this->writeConfiguration(); - return tap($callback(), function () { - $this->removeConfiguration(); + $this->setupSignalHandler(); + } - if (file_exists(base_path($this->duskFile())) && file_exists(base_path('.env.backup'))) { - $this->restoreEnvironment(); - } + /** + * Setup the SIGINT signal handler for CTRL+C exits. + * + * @return void + */ + protected function setupSignalHandler() + { + declare(ticks=1); + + pcntl_signal(SIGINT, function () { + $this->teardownDuskEnviroment(); }); } + /** + * Setup the dusk environment. + * + * @return void + */ + protected function teardownDuskEnviroment() + { + $this->removeConfiguration(); + + if (file_exists(base_path($this->duskFile())) && file_exists(base_path('.env.backup'))) { + $this->restoreEnvironment(); + } + } + /** * Backup the current environment file. * From abbb3f79501eb8efcbadfcf0114511fa72f9b7c4 Mon Sep 17 00:00:00 2001 From: Michel Bardelmeijer Date: Sun, 13 Oct 2019 14:22:06 +0200 Subject: [PATCH 2/6] Codestyle --- src/Console/DuskCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Console/DuskCommand.php b/src/Console/DuskCommand.php index 5c2661643..aab9b2964 100644 --- a/src/Console/DuskCommand.php +++ b/src/Console/DuskCommand.php @@ -6,8 +6,8 @@ use Illuminate\Console\Command; use Illuminate\Support\Str; use Symfony\Component\Finder\Finder; -use Symfony\Component\Process\Exception\RuntimeException; use Symfony\Component\Process\Exception\ProcessSignaledException; +use Symfony\Component\Process\Exception\RuntimeException; use Symfony\Component\Process\Process; class DuskCommand extends Command From c1573eae63fee62df9fa180a504a770ed64103cb Mon Sep 17 00:00:00 2001 From: Michel Bardelmeijer Date: Sun, 13 Oct 2019 15:31:12 +0200 Subject: [PATCH 3/6] Update DuskCommand.php --- src/Console/DuskCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Console/DuskCommand.php b/src/Console/DuskCommand.php index aab9b2964..a2aa47024 100644 --- a/src/Console/DuskCommand.php +++ b/src/Console/DuskCommand.php @@ -288,8 +288,8 @@ protected function writeConfiguration() */ protected function removeConfiguration() { - if (! $this->hasPhpUnitConfiguration) { - unlink(base_path('phpunit.dusk.xml')); + if (! $this->hasPhpUnitConfiguration && file_exists($file = base_path('phpunit.dusk.xml'))) { + unlink($file); } } From b090c24f0cb1488d6679ee8788a1a021f4cdc3af Mon Sep 17 00:00:00 2001 From: Michel Bardelmeijer Date: Mon, 14 Oct 2019 15:15:55 +0200 Subject: [PATCH 4/6] Require pcntl extension in composer.json --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index e97e5df40..14b2601ca 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,7 @@ "php": ">=7.1.0", "ext-json": "*", "ext-zip": "*", + "ext-pcntl": "*", "facebook/webdriver": "^1.7", "nesbot/carbon": "^1.20|^2.0", "illuminate/console": "~5.7.0|~5.8.0|^6.0|^7.0", From d72878493f610bb7f2b5ac100cbb511597ba2f49 Mon Sep 17 00:00:00 2001 From: Michel Bardelmeijer Date: Mon, 14 Oct 2019 15:16:07 +0200 Subject: [PATCH 5/6] Use constant for SIGINT instead of magic variable --- src/Console/DuskCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Console/DuskCommand.php b/src/Console/DuskCommand.php index a2aa47024..85553131e 100644 --- a/src/Console/DuskCommand.php +++ b/src/Console/DuskCommand.php @@ -74,7 +74,7 @@ public function handle() $this->output->write($line); }); } catch (ProcessSignaledException $e) { - if ($e->getSignal() !== 2) { + if ($e->getSignal() !== SIGINT) { throw $e; } } From 874df3f490f4378edbd1d33125b75c3c6467f3f4 Mon Sep 17 00:00:00 2001 From: Michel Bardelmeijer Date: Wed, 16 Oct 2019 17:04:16 +0200 Subject: [PATCH 6/6] Use async signals instead of declare ticks --- src/Console/DuskCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Console/DuskCommand.php b/src/Console/DuskCommand.php index 85553131e..82c22d57d 100644 --- a/src/Console/DuskCommand.php +++ b/src/Console/DuskCommand.php @@ -202,7 +202,7 @@ protected function setupDuskEnvironment() */ protected function setupSignalHandler() { - declare(ticks=1); + pcntl_async_signals(true); pcntl_signal(SIGINT, function () { $this->teardownDuskEnviroment();