Skip to content

Commit

Permalink
Just use timestamp to simplify stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
hschimpf committed Apr 12, 2023
1 parent c52bf3a commit 8d27192
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
9 changes: 5 additions & 4 deletions src/Internals/Commands/Runner/WaitTasksCompletionMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace HDSSolutions\Console\Parallel\Internals\Commands\Runner;

use Closure;
use DateInterval;
use DateTime;
use HDSSolutions\Console\Parallel\Internals\Commands\ParallelCommandMessage;
use HDSSolutions\Console\Parallel\Internals\Runner;

Expand All @@ -12,10 +13,10 @@
final class WaitTasksCompletionMessage extends ParallelCommandMessage {

/**
* @param Closure $should_keep_waiting
* @param DateInterval|null $wait_until
*/
public function __construct(Closure $should_keep_waiting) {
parent::__construct('await', [ $should_keep_waiting ]);
public function __construct(?DateInterval $wait_until = null) {
parent::__construct('await', [ $wait_until === null ? null : (new DateTime())->add($wait_until)->getTimestamp() ]);
}

}
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 $should_keep_waiting): bool {
protected function await(?int $wait_until = null): bool {
if (PARALLEL_EXT_LOADED) {
return $this->send($should_keep_waiting() && ($this->hasPendingTasks() || $this->hasRunningTasks()));
return $this->send(time() <= ($wait_until ?? time()) && ($this->hasPendingTasks() || $this->hasRunningTasks()));
}

return true;
Expand Down
17 changes: 7 additions & 10 deletions src/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace HDSSolutions\Console\Parallel;

use Closure;
use DateTime;
use DateInterval;
use Generator;
use HDSSolutions\Console\Parallel\Contracts\Task;
use HDSSolutions\Console\Parallel\Exceptions\ParallelException;
Expand Down Expand Up @@ -112,22 +112,19 @@ public static function runTask(mixed ...$data): int {
/**
* Calling this method will pause execution until all tasks are finished.
*
* @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.
* @param DateInterval|null $wait_until Should wait until specified DateInterval or until all tasks finished.
*/
public static function awaitTasksCompletion(DateTime $wait_until = null, Closure $should_keep_waiting = null): bool {
$message = new Commands\Runner\WaitTasksCompletionMessage(
should_keep_waiting: $should_keep_waiting ?? static fn(): bool => $wait_until === null || new DateTime() < $wait_until,
);
public static function awaitTasksCompletion(DateInterval $wait_until = null): bool {
$message = new Commands\Runner\WaitTasksCompletionMessage($wait_until);

if (PARALLEL_EXT_LOADED) {
$has_pending_tasks = false;
$should_keep_waiting = false;
do {
self::instance()->send($message);
if ($has_pending_tasks) {
if ($should_keep_waiting) {
usleep(25_000);
}
} while ($has_pending_tasks = self::instance()->recv());
} while ($should_keep_waiting = self::instance()->recv());

return true;
}
Expand Down

0 comments on commit 8d27192

Please sign in to comment.