Skip to content

Commit

Permalink
Merge pull request #14 from hschimpf/2.x-dev
Browse files Browse the repository at this point in the history
New option to override awaitTasksCompletion() validation
  • Loading branch information
hschimpf authored Apr 9, 2023
2 parents ad20526 + 2956a69 commit 0c54075
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/Internals/Commands/Runner/WaitTasksCompletionMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 ]);
}

}
4 changes: 2 additions & 2 deletions src/Internals/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions src/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0c54075

Please sign in to comment.