From c30ff76aac18af16aad3bcabe4f5826f48d54dfa Mon Sep 17 00:00:00 2001 From: Chris Morrell Date: Fri, 5 Nov 2021 13:07:47 -0400 Subject: [PATCH 1/4] Ensure mutex is always removed --- src/Illuminate/Console/Scheduling/Event.php | 41 ++++++++++++++++++--- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Console/Scheduling/Event.php b/src/Illuminate/Console/Scheduling/Event.php index 58cee5281cb1..1ca7a224db56 100644 --- a/src/Illuminate/Console/Scheduling/Event.php +++ b/src/Illuminate/Console/Scheduling/Event.php @@ -17,6 +17,7 @@ use Illuminate\Support\Traits\ReflectsClosures; use Psr\Http\Client\ClientExceptionInterface; use Symfony\Component\Process\Process; +use Throwable; class Event { @@ -218,11 +219,17 @@ public function mutexName() */ protected function runCommandInForeground(Container $container) { - $this->callBeforeCallbacks($container); + try { + $this->callBeforeCallbacks($container); - $this->exitCode = Process::fromShellCommandline($this->buildCommand(), base_path(), null, null, null)->run(); + $this->exitCode = Process::fromShellCommandline( + $this->buildCommand(), base_path(), null, null, null + )->run(); - $this->callAfterCallbacks($container); + $this->callAfterCallbacks($container); + } finally { + $this->removeMutex(); + } } /** @@ -233,9 +240,15 @@ protected function runCommandInForeground(Container $container) */ protected function runCommandInBackground(Container $container) { - $this->callBeforeCallbacks($container); + try { + $this->callBeforeCallbacks($container); + + Process::fromShellCommandline($this->buildCommand(), base_path(), null, null, null)->run(); + } catch (Throwable $exception) { + $this->removeMutex(); - Process::fromShellCommandline($this->buildCommand(), base_path(), null, null, null)->run(); + throw $exception; + } } /** @@ -275,7 +288,11 @@ public function callAfterCallbacksWithExitCode(Container $container, $exitCode) { $this->exitCode = (int) $exitCode; - $this->callAfterCallbacks($container); + try { + $this->callAfterCallbacks($container); + } finally { + $this->removeMutex(); + } } /** @@ -635,6 +652,18 @@ public function evenInMaintenanceMode() return $this; } + /** + * Clear the mutex for the event. + * + * @return void + */ + protected function removeMutex() + { + if ($this->withoutOverlapping) { + $this->mutex->forget($this); + } + } + /** * Do not allow the event to overlap each other. * From 7cc932cf8efb29367a626c1485694ab73137ae51 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 5 Nov 2021 12:40:36 -0500 Subject: [PATCH 2/4] formatting --- src/Illuminate/Console/Scheduling/Event.php | 30 ++++++++++----------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Illuminate/Console/Scheduling/Event.php b/src/Illuminate/Console/Scheduling/Event.php index 1ca7a224db56..79ee0a866de2 100644 --- a/src/Illuminate/Console/Scheduling/Event.php +++ b/src/Illuminate/Console/Scheduling/Event.php @@ -228,7 +228,7 @@ protected function runCommandInForeground(Container $container) $this->callAfterCallbacks($container); } finally { - $this->removeMutex(); + $this->deleteMutex(); } } @@ -245,7 +245,7 @@ protected function runCommandInBackground(Container $container) Process::fromShellCommandline($this->buildCommand(), base_path(), null, null, null)->run(); } catch (Throwable $exception) { - $this->removeMutex(); + $this->deleteMutex(); throw $exception; } @@ -291,7 +291,7 @@ public function callAfterCallbacksWithExitCode(Container $container, $exitCode) try { $this->callAfterCallbacks($container); } finally { - $this->removeMutex(); + $this->deleteMutex(); } } @@ -652,18 +652,6 @@ public function evenInMaintenanceMode() return $this; } - /** - * Clear the mutex for the event. - * - * @return void - */ - protected function removeMutex() - { - if ($this->withoutOverlapping) { - $this->mutex->forget($this); - } - } - /** * Do not allow the event to overlap each other. * @@ -944,4 +932,16 @@ public function preventOverlapsUsing(EventMutex $mutex) return $this; } + + /** + * Delete the mutex for the event. + * + * @return void + */ + protected function deleteMutex() + { + if ($this->withoutOverlapping) { + $this->mutex->forget($this); + } + } } From b9d05b8e1ff92fe3941628c8b24a217fd3d3d02f Mon Sep 17 00:00:00 2001 From: Chris Morrell Date: Fri, 5 Nov 2021 13:49:36 -0400 Subject: [PATCH 3/4] Remove after callback that duplicates work --- src/Illuminate/Console/Scheduling/Event.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Illuminate/Console/Scheduling/Event.php b/src/Illuminate/Console/Scheduling/Event.php index 1ca7a224db56..352ca83bfbc1 100644 --- a/src/Illuminate/Console/Scheduling/Event.php +++ b/src/Illuminate/Console/Scheduling/Event.php @@ -676,9 +676,7 @@ public function withoutOverlapping($expiresAt = 1440) $this->expiresAt = $expiresAt; - return $this->then(function () { - $this->mutex->forget($this); - })->skip(function () { + return $this->skip(function () { return $this->mutex->exists($this); }); } From 1e24a50dc56637628ad7cf7043ee8460d2b443e4 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 5 Nov 2021 13:39:49 -0500 Subject: [PATCH 4/4] rename method --- src/Illuminate/Console/Scheduling/Event.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Console/Scheduling/Event.php b/src/Illuminate/Console/Scheduling/Event.php index bd9f5de97ff7..4de88f163dbf 100644 --- a/src/Illuminate/Console/Scheduling/Event.php +++ b/src/Illuminate/Console/Scheduling/Event.php @@ -228,7 +228,7 @@ protected function runCommandInForeground(Container $container) $this->callAfterCallbacks($container); } finally { - $this->deleteMutex(); + $this->removeMutex(); } } @@ -245,7 +245,7 @@ protected function runCommandInBackground(Container $container) Process::fromShellCommandline($this->buildCommand(), base_path(), null, null, null)->run(); } catch (Throwable $exception) { - $this->deleteMutex(); + $this->removeMutex(); throw $exception; } @@ -291,7 +291,7 @@ public function callAfterCallbacksWithExitCode(Container $container, $exitCode) try { $this->callAfterCallbacks($container); } finally { - $this->deleteMutex(); + $this->removeMutex(); } } @@ -936,7 +936,7 @@ public function preventOverlapsUsing(EventMutex $mutex) * * @return void */ - protected function deleteMutex() + protected function removeMutex() { if ($this->withoutOverlapping) { $this->mutex->forget($this);