diff --git a/src/Illuminate/Console/Scheduling/Event.php b/src/Illuminate/Console/Scheduling/Event.php index 58cee5281cb1..4de88f163dbf 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(); + } } /** @@ -647,9 +664,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); }); } @@ -915,4 +930,16 @@ public function preventOverlapsUsing(EventMutex $mutex) return $this; } + + /** + * Delete the mutex for the event. + * + * @return void + */ + protected function removeMutex() + { + if ($this->withoutOverlapping) { + $this->mutex->forget($this); + } + } }