From 2956a69851115275bd53474ec4d0ef744f725798 Mon Sep 17 00:00:00 2001 From: "Hermann D. Schimpf" Date: Sun, 9 Apr 2023 13:04:17 -0400 Subject: [PATCH] Allow to set a custom validation to override await --- .../Commands/Runner/WaitTasksCompletionMessage.php | 8 ++++++-- src/Internals/Runner.php | 4 ++-- src/Scheduler.php | 8 ++++++-- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/Internals/Commands/Runner/WaitTasksCompletionMessage.php b/src/Internals/Commands/Runner/WaitTasksCompletionMessage.php index 8f1fad4..5b9ff19 100644 --- a/src/Internals/Commands/Runner/WaitTasksCompletionMessage.php +++ b/src/Internals/Commands/Runner/WaitTasksCompletionMessage.php @@ -2,6 +2,7 @@ namespace HDSSolutions\Console\Parallel\Internals\Commands\Runner; +use Closure; use HDSSolutions\Console\Parallel\Internals\Commands\ParallelCommandMessage; use HDSSolutions\Console\Parallel\Internals\Runner; @@ -10,8 +11,11 @@ */ final class WaitTasksCompletionMessage extends ParallelCommandMessage { - public function __construct() { - parent::__construct('await'); + /** + * @param Closure $or_until + */ + public function __construct(Closure $or_until) { + parent::__construct('await', [ $or_until ]); } } diff --git a/src/Internals/Runner.php b/src/Internals/Runner.php index 04ad4e0..6312092 100644 --- a/src/Internals/Runner.php +++ b/src/Internals/Runner.php @@ -197,9 +197,9 @@ protected function update(): void { } } - protected function await(): bool { + protected function await(Closure $or_until): bool { if (PARALLEL_EXT_LOADED) { - return $this->send($this->hasPendingTasks() || $this->hasRunningTasks()); + return $this->send($or_until() === false && ($this->hasPendingTasks() || $this->hasRunningTasks())); } return true; diff --git a/src/Scheduler.php b/src/Scheduler.php index ea64bdd..dfb9e60 100644 --- a/src/Scheduler.php +++ b/src/Scheduler.php @@ -110,9 +110,13 @@ public static function runTask(mixed ...$data): int { /** * Calling this method will pause execution until all tasks are finished. + * + * @param Closure|null $or_until Custom validation to stop waiting. */ - public static function awaitTasksCompletion(): bool { - $message = new Commands\Runner\WaitTasksCompletionMessage(); + public static function awaitTasksCompletion(Closure $or_until = null): bool { + $message = new Commands\Runner\WaitTasksCompletionMessage( + or_until: $or_until ?? static fn() => false, + ); if (PARALLEL_EXT_LOADED) { $has_pending_tasks = false;