17
17
use Illuminate \Support \Traits \ReflectsClosures ;
18
18
use Psr \Http \Client \ClientExceptionInterface ;
19
19
use Symfony \Component \Process \Process ;
20
+ use Throwable ;
20
21
21
22
class Event
22
23
{
@@ -218,11 +219,17 @@ public function mutexName()
218
219
*/
219
220
protected function runCommandInForeground (Container $ container )
220
221
{
221
- $ this ->callBeforeCallbacks ($ container );
222
+ try {
223
+ $ this ->callBeforeCallbacks ($ container );
222
224
223
- $ this ->exitCode = Process::fromShellCommandline ($ this ->buildCommand (), base_path (), null , null , null )->run ();
225
+ $ this ->exitCode = Process::fromShellCommandline (
226
+ $ this ->buildCommand (), base_path (), null , null , null
227
+ )->run ();
224
228
225
- $ this ->callAfterCallbacks ($ container );
229
+ $ this ->callAfterCallbacks ($ container );
230
+ } finally {
231
+ $ this ->removeMutex ();
232
+ }
226
233
}
227
234
228
235
/**
@@ -233,9 +240,15 @@ protected function runCommandInForeground(Container $container)
233
240
*/
234
241
protected function runCommandInBackground (Container $ container )
235
242
{
236
- $ this ->callBeforeCallbacks ($ container );
243
+ try {
244
+ $ this ->callBeforeCallbacks ($ container );
245
+
246
+ Process::fromShellCommandline ($ this ->buildCommand (), base_path (), null , null , null )->run ();
247
+ } catch (Throwable $ exception ) {
248
+ $ this ->removeMutex ();
237
249
238
- Process::fromShellCommandline ($ this ->buildCommand (), base_path (), null , null , null )->run ();
250
+ throw $ exception ;
251
+ }
239
252
}
240
253
241
254
/**
@@ -275,7 +288,11 @@ public function callAfterCallbacksWithExitCode(Container $container, $exitCode)
275
288
{
276
289
$ this ->exitCode = (int ) $ exitCode ;
277
290
278
- $ this ->callAfterCallbacks ($ container );
291
+ try {
292
+ $ this ->callAfterCallbacks ($ container );
293
+ } finally {
294
+ $ this ->removeMutex ();
295
+ }
279
296
}
280
297
281
298
/**
@@ -647,9 +664,7 @@ public function withoutOverlapping($expiresAt = 1440)
647
664
648
665
$ this ->expiresAt = $ expiresAt ;
649
666
650
- return $ this ->then (function () {
651
- $ this ->mutex ->forget ($ this );
652
- })->skip (function () {
667
+ return $ this ->skip (function () {
653
668
return $ this ->mutex ->exists ($ this );
654
669
});
655
670
}
@@ -915,4 +930,16 @@ public function preventOverlapsUsing(EventMutex $mutex)
915
930
916
931
return $ this ;
917
932
}
933
+
934
+ /**
935
+ * Delete the mutex for the event.
936
+ *
937
+ * @return void
938
+ */
939
+ protected function removeMutex ()
940
+ {
941
+ if ($ this ->withoutOverlapping ) {
942
+ $ this ->mutex ->forget ($ this );
943
+ }
944
+ }
918
945
}
0 commit comments