-
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from spatie/debugger
Add status support
- Loading branch information
Showing
4 changed files
with
140 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Spatie\Async; | ||
|
||
use Spatie\Async\Output\SerializableException; | ||
|
||
class PoolStatus | ||
{ | ||
protected $pool; | ||
|
||
public function __construct(Pool $pool) | ||
{ | ||
$this->pool = $pool; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->lines( | ||
$this->summaryToString(), | ||
$this->failedToString() | ||
); | ||
} | ||
|
||
protected function lines(string ...$lines): string | ||
{ | ||
return implode(PHP_EOL, $lines); | ||
} | ||
|
||
protected function summaryToString(): string | ||
{ | ||
$finished = $this->pool->getFinished(); | ||
$failed = $this->pool->getFailed(); | ||
$timeouts = $this->pool->getTimeouts(); | ||
|
||
return 'finished: '.count($finished) | ||
.' - failed: '.count($failed) | ||
.' - timeout: '.count($timeouts); | ||
} | ||
|
||
protected function failedToString(): string | ||
{ | ||
return (string) array_reduce($this->pool->getFailed(), function ($currentStatus, ParallelProcess $process) { | ||
$output = $process->getErrorOutput(); | ||
|
||
if ($output instanceof SerializableException) { | ||
$output = get_class($output->asThrowable()).': '.$output->asThrowable()->getMessage(); | ||
} | ||
|
||
return $this->lines((string) $currentStatus, "{$process->getPid()} failed with {$output}"); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Spatie\Async; | ||
|
||
use Exception; | ||
use Spatie\Async\Tests\MyTask; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class PoolStatusTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_can_show_a_textual_status() | ||
{ | ||
$pool = Pool::create(); | ||
|
||
$pool->add(new MyTask()); | ||
|
||
$this->assertContains('finished: 0', (string) $pool->status()); | ||
|
||
await($pool); | ||
|
||
$this->assertContains('finished: 1', (string) $pool->status()); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_show_a_textual_failed_status() | ||
{ | ||
$pool = Pool::create(); | ||
|
||
foreach (range(1, 5) as $i) { | ||
$pool->add(function () { | ||
throw new Exception('Test'); | ||
})->catch(function () { | ||
// Do nothing | ||
}); | ||
} | ||
|
||
$pool->wait(); | ||
|
||
$this->assertContains('finished: 0', (string) $pool->status()); | ||
$this->assertContains('failed: 5', (string) $pool->status()); | ||
$this->assertContains('failed with Exception: Test', (string) $pool->status()); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_show_timeout_status() | ||
{ | ||
$pool = Pool::create()->timeout(0); | ||
|
||
foreach (range(1, 5) as $i) { | ||
$pool->add(function () { | ||
sleep(1000); | ||
}); | ||
} | ||
|
||
$pool->wait(); | ||
|
||
$this->assertContains('timeout: 5', (string) $pool->status()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters