Skip to content

Commit

Permalink
Allow to set a DateTime to finish waiting for tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
hschimpf committed Apr 10, 2023
1 parent c212cfb commit c52bf3a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/Internals/Commands/Runner/WaitTasksCompletionMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]);
}

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

0 comments on commit c52bf3a

Please sign in to comment.