Skip to content

Commit

Permalink
Merge pull request #144 from hirak/revival-progress
Browse files Browse the repository at this point in the history
Show errors and progress
  • Loading branch information
hirak authored May 27, 2017
2 parents e4a7cdb + a3dfa67 commit 45fcc8a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/CurlMulti.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function wait()

public function getFinishedResults()
{
$urls = array();
$urls = $errors = array();
$successCnt = $failureCnt = 0;
do {
if ($raised = curl_multi_info_read($this->mh, $remains)) {
Expand All @@ -140,14 +140,15 @@ public function getFinishedResults()
$urls[] = $request->getMaskedURL();
} else {
++$failureCnt;
$errors[$request->getMaskedURL()] = "$errno: $error";
}
unset($this->using[$index], $this->runningRequests[$index], $request);
curl_multi_remove_handle($this->mh, $ch);
$this->unused[] = $ch;
}
} while ($remains > 0);

return compact('successCnt', 'failureCnt', 'urls');
return compact('successCnt', 'failureCnt', 'urls', 'errors');
}

public function remain()
Expand Down
8 changes: 6 additions & 2 deletions src/Prefetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,19 @@ public function fetchAll(IO\IOInterface $io, array $requests)
$successCnt += $result['successCnt'];
$failureCnt += $result['failureCnt'];
foreach ($result['urls'] as $url) {
$io->writeError(" <comment>$successCnt/$totalCnt</comment>:\t$url", true, IO\IOInterface::VERBOSE);
if (isset($result['errors'][$url])) {
$io->writeError(" <warning>{$result['errors'][$url]}</warning>:\t$url", true, IO\IOInterface::NORMAL);
} else {
$io->writeError(" <comment>$successCnt/$totalCnt</comment>:\t$url", true, IO\IOInterface::NORMAL);
}
}
} while ($multi->remain());
} catch (FetchException $e) {
// do nothing
}

$skippedCnt = $totalCnt - $successCnt - $failureCnt;
$io->writeError(" Finished: <comment>success: $successCnt, skipped: $skippedCnt, failure: $failureCnt, total: $totalCnt</comment>", true, IO\IOInterface::VERBOSE);
$io->writeError(" Finished: <comment>success: $successCnt, skipped: $skippedCnt, failure: $failureCnt, total: $totalCnt</comment>", true, IO\IOInterface::NORMAL);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/PrefetcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ protected function setUp()
public function testFetchAllOnFailure()
{
$reqp = $this->prophesize('Hirak\Prestissimo\CopyRequest');
$reqp->getMaskedURL()->willReturn('file://' . __DIR__ . '/test.txt');
$reqp->getCurlOptions()->willReturn(array(
CURLOPT_URL => 'file://uso800.txt',
CURLOPT_FILE => tmpfile(),
));
$this->iop->writeError(" Finished: <comment>success: 0, skipped: 0, failure: 1, total: 1</comment>", true, IOInterface::VERBOSE)->shouldBeCalledTimes(1);
$this->iop->writeError(" Finished: <comment>success: 0, skipped: 0, failure: 1, total: 1</comment>", true, IOInterface::NORMAL)->shouldBeCalledTimes(1);

$fetcher = new Prefetcher;
$fetcher->fetchAll($this->iop->reveal(), array($reqp->reveal()));
Expand All @@ -40,7 +41,7 @@ public function testFetchAllOnSuccess()
));
$reqp->makeSuccess()->willReturn(null);
$reqp->getMaskedURL()->willReturn('file://' . __DIR__ . '/test.txt');
$this->iop->writeError(arg::type('string'), true, IOInterface::VERBOSE)->shouldBeCalled();
$this->iop->writeError(arg::type('string'), true, IOInterface::NORMAL)->shouldBeCalled();

$fetcher = new Prefetcher;
$fetcher->fetchAll($this->iop->reveal(), array($reqp->reveal()));
Expand Down

0 comments on commit 45fcc8a

Please sign in to comment.