From c52bf3a9e50e95b8e4b5768e09e8ac0349a5e69c Mon Sep 17 00:00:00 2001 From: "Hermann D. Schimpf" Date: Sun, 9 Apr 2023 22:05:25 -0400 Subject: [PATCH] Allow to set a DateTime to finish waiting for tasks --- .../Commands/Runner/WaitTasksCompletionMessage.php | 6 +++--- src/Internals/Runner.php | 4 ++-- src/Scheduler.php | 8 +++++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Internals/Commands/Runner/WaitTasksCompletionMessage.php b/src/Internals/Commands/Runner/WaitTasksCompletionMessage.php index 5b9ff19..cac1fb8 100644 --- a/src/Internals/Commands/Runner/WaitTasksCompletionMessage.php +++ b/src/Internals/Commands/Runner/WaitTasksCompletionMessage.php @@ -12,10 +12,10 @@ final class WaitTasksCompletionMessage extends ParallelCommandMessage { /** - * @param Closure $or_until + * @param Closure $should_keep_waiting */ - public function __construct(Closure $or_until) { - parent::__construct('await', [ $or_until ]); + public function __construct(Closure $should_keep_waiting) { + parent::__construct('await', [ $should_keep_waiting ]); } } diff --git a/src/Internals/Runner.php b/src/Internals/Runner.php index 2e1a4c3..f78175a 100644 --- a/src/Internals/Runner.php +++ b/src/Internals/Runner.php @@ -198,9 +198,9 @@ protected function update(): void { } } - protected function await(Closure $or_until): bool { + protected function await(Closure $should_keep_waiting): bool { if (PARALLEL_EXT_LOADED) { - return $this->send($or_until() === false && ($this->hasPendingTasks() || $this->hasRunningTasks())); + return $this->send($should_keep_waiting() && ($this->hasPendingTasks() || $this->hasRunningTasks())); } return true; diff --git a/src/Scheduler.php b/src/Scheduler.php index dfb9e60..0b0e13f 100644 --- a/src/Scheduler.php +++ b/src/Scheduler.php @@ -3,6 +3,7 @@ namespace HDSSolutions\Console\Parallel; use Closure; +use DateTime; use Generator; use HDSSolutions\Console\Parallel\Contracts\Task; use HDSSolutions\Console\Parallel\Exceptions\ParallelException; @@ -111,11 +112,12 @@ 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. + * @param DateTime|null $wait_until Should wait until specified DateTime or all tasks finished. + * @param Closure|null $should_keep_waiting Custom validation to stop waiting. */ - public static function awaitTasksCompletion(Closure $or_until = null): bool { + public static function awaitTasksCompletion(DateTime $wait_until = null, Closure $should_keep_waiting = null): bool { $message = new Commands\Runner\WaitTasksCompletionMessage( - or_until: $or_until ?? static fn() => false, + should_keep_waiting: $should_keep_waiting ?? static fn(): bool => $wait_until === null || new DateTime() < $wait_until, ); if (PARALLEL_EXT_LOADED) {