Skip to content

Commit

Permalink
feat: use batch to upload assets
Browse files Browse the repository at this point in the history
  • Loading branch information
carlalexander committed Jul 7, 2022
1 parent 349844a commit 1981924
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
21 changes: 12 additions & 9 deletions src/Deployment/ProcessAssetsStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Ymir\Cli\Deployment;

use Illuminate\Support\Collection;
use Illuminate\Support\LazyCollection;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Finder\Finder;
use Ymir\Cli\ApiClient;
Expand Down Expand Up @@ -79,19 +80,19 @@ public function perform(Collection $deployment, OutputInterface $output)
return isset($signedAssetRequests['copy'][$asset['relative_path']]);
})->map(function (array $asset) use ($signedAssetRequests) {
return $signedAssetRequests['copy'][$asset['relative_path']];
})->all(), $output);
}), $output);

$this->uploadAssetFiles($assetFiles->filter(function (array $asset) use ($signedAssetRequests) {
return isset($signedAssetRequests['store'][$asset['relative_path']]);
})->mapWithKeys(function (array $asset) use ($signedAssetRequests) {
return [$asset['real_path'] => $signedAssetRequests['store'][$asset['relative_path']]];
})->all(), $output);
}), $output);
}

/**
* Send the given asset file copy requests.
*/
private function copyAssetFiles(array $requests, OutputInterface $output)
private function copyAssetFiles(Collection $requests, OutputInterface $output)
{
if (empty($requests)) {
return;
Expand Down Expand Up @@ -129,20 +130,22 @@ private function getAssetFiles(): Collection
/**
* Send the given asset file upload requests.
*/
private function uploadAssetFiles(array $requests, OutputInterface $output)
private function uploadAssetFiles(Collection $requests, OutputInterface $output)
{
if (empty($requests)) {
return;
}

$progressBar = new ProgressBar($output);
$progressBar->setFormat(' > Uploading new asset files (<comment>%current%/%max%</comment>)');
$progressBar->start(count($requests));

foreach ($requests as $realFilePath => $request) {
$this->uploader->uploadFile((string) $realFilePath, (string) $request['uri'], $request['headers']);
$progressBar->advance();
}
$requests = LazyCollection::make($requests)->map(function (array $request, string $realFilePath) {
$request['body'] = fopen($realFilePath, 'r+');

return $request;
});

$this->uploader->batch('PUT', $requests, $progressBar);

$output->newLine();
}
Expand Down
5 changes: 3 additions & 2 deletions src/FileUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Pool;
use GuzzleHttp\Psr7\Request;
use Illuminate\Support\Enumerable;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Helper\ProgressBar;

Expand Down Expand Up @@ -46,7 +47,7 @@ public function __construct(ClientInterface $client)
/**
* Sends multiple requests concurrently.
*/
public function batch(string $method, array $requests, ?ProgressBar $progressBar = null)
public function batch(string $method, Enumerable $requests, ?ProgressBar $progressBar = null)
{
if ($progressBar instanceof ProgressBar) {
$progressBar->start(count($requests));
Expand All @@ -58,7 +59,7 @@ public function batch(string $method, array $requests, ?ProgressBar $progressBar
$progressBar->advance();
}

yield new Request($method, $request['uri'], array_merge(self::DEFAULT_HEADERS, $request['headers']));
yield new Request($method, $request['uri'], array_merge(self::DEFAULT_HEADERS, $request['headers']), $request['body'] ?? null);
}
};

Expand Down

0 comments on commit 1981924

Please sign in to comment.