-
-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add status support #16
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f692041
Add debugger
brendt e005ca7
Add PoolStatus
brendt 390d4f8
Apply fixes from StyleCI
brendt 640d828
Merge pull request #20 from spatie/analysis-z4ZGmE
brendt 040e8b4
Add failed test
brendt 5b0da19
Merge branch 'debugger' of github.com:spatie/async into debugger
brendt 529e4bc
Refactor to PoolStatusTest
brendt edb12f6
Apply fixes from StyleCI
brendt 00c9345
Merge pull request #21 from spatie/analysis-XVx3DD
brendt f6bbb1a
Code cleanup
brendt caac1e3
Code cleanup
brendt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!