Skip to content

Commit

Permalink
Merge master to 1.x (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Jul 11, 2021
1 parent 60f99ee commit 873650e
Show file tree
Hide file tree
Showing 11 changed files with 1,210 additions and 130 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ env:

matrix:
include:
- php: '7.2'
env: PREFER_LOWEST='--prefer-lowest'
- php: '7.3'
env: PREFER_LOWEST='--prefer-lowest'
- php: '7.4'
env: SYMFONY_VERSION='~4.4.0'
- php: '7.4'
env: SYMFONY_VERSION='~5.0.0'
- php: '8.0'
env: SYMFONY_VERSION='~5.0.0'
fast_finish: true

before_install:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],

"require": {
"php": "^7.2",
"php": "^7.3 || ^8.0",
"symfony/console": "^3.0 || ^4.0 || ^5.0",
"symfony/dependency-injection": "^3.0 || ^4.1.12 || ^5.0",
"symfony/process": "^3.0 || ^4.0 || ^5.0",
Expand Down
99 changes: 99 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

/*
* This file is part of the Webmozarts Console Parallelization package.
*
* (c) Webmozarts GmbH <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Webmozarts\Console\Parallelization;

use Webmozart\Assert\Assert;
use function ceil;
use function sprintf;

final class Configuration
{
private $segmentSize;
private $rounds;
private $batches;

public function __construct(
bool $numberOfProcessesDefined,
int $numberOfProcesses,
int $numberOfItems,
int $segmentSize,
int $batchSize
) {
Assert::greaterThan(
$numberOfProcesses,
0,
sprintf(
'Expected the number of processes to be 1 or greater. Got "%s"',
$numberOfProcesses
)
);
Assert::natural(
$numberOfItems,
sprintf(
'Expected the number of items to be 0 or greater. Got "%s"',
$numberOfItems
)
);
Assert::greaterThan(
$segmentSize,
0,
sprintf(
'Expected the segment size to be 1 or greater. Got "%s"',
$segmentSize
)
);
Assert::greaterThan(
$batchSize,
0,
sprintf(
'Expected the batch size to be 1 or greater. Got "%s"',
$batchSize
)
);

// We always check those (and not the calculated ones) since they come from the command
// configuration so an issue there hints on a misconfiguration which should be fixed.
Assert::greaterThanEq(
$segmentSize,
$batchSize,
sprintf(
'Expected the segment size ("%s") to be greater or equal to the batch size ("%s")',
$segmentSize,
$batchSize
)
);

$this->segmentSize = 1 === $numberOfProcesses && !$numberOfProcessesDefined
? $numberOfItems
: $segmentSize
;
$this->rounds = (int) (1 === $numberOfProcesses ? 1 : ceil($numberOfItems / $segmentSize));
$this->batches = (int) (ceil($segmentSize / $batchSize) * $this->rounds);
}

public function getSegmentSize(): int
{
return $this->segmentSize;
}

public function getNumberOfSegments(): int
{
return $this->rounds;
}

public function getNumberOfBatches(): int
{
return $this->batches;
}
}
131 changes: 131 additions & 0 deletions src/ItemBatchIterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

/*
* This file is part of the Webmozarts Console Parallelization package.
*
* (c) Webmozarts GmbH <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Webmozarts\Console\Parallelization;

use Closure;
use Webmozart\Assert\Assert;
use function array_chunk;
use function array_values;
use function count;
use function get_class;
use function gettype;
use function is_numeric;
use function is_object;
use function sprintf;

final class ItemBatchIterator
{
private $items;
private $numberOfItems;
private $batchSize;
private $itemsChunks;

/**
* @param Closure(): list<string> $fetchItems
*/
public static function create(?string $item, Closure $fetchItems, int $batchSize): self
{
if (null !== $item) {
$items = [$item];
} else {
$items = $fetchItems();

Assert::isArray(
$items,
sprintf(
'Expected the fetched items to be a list of strings. Got "%s"',
gettype($items)
)
);
}

return new self($items, $batchSize);
}

/**
* @return list<string>
*/
private static function normalizeItems($items): array
{
foreach ($items as $index => $item) {
if (is_numeric($item)) {
$items[$index] = (string) $item;

continue;
}

Assert::string(
$item,
sprintf(
'The items are potentially passed to the child processes via the STDIN. For this reason they are expected to be string values. Got "%s" for the item "%s"',
is_object($item) ? get_class($item) : gettype($item),
$index
)
);
}

return array_values($items);
}

/**
* @param list<string> $items
* @param int $batchSize
*/
public function __construct(array $items, int $batchSize)
{
Assert::greaterThan(
$batchSize,
0,
sprintf(
'Expected the batch size to be 1 or greater. Got "%s"',
$batchSize
)
);

$this->items = self::normalizeItems($items);
$this->itemsChunks = array_chunk(
$this->items,
$batchSize,
false
);
$this->numberOfItems = count($this->items);
$this->batchSize = $batchSize;
}

/**
* @return list<string>
*/
public function getItems(): array
{
return $this->items;
}

public function getNumberOfItems(): int
{
return $this->numberOfItems;
}

public function getBatchSize(): int
{
return $this->batchSize;
}

/**
* @return array<list<string>>
*/
public function getItemBatches(): array
{
return $this->itemsChunks;
}
}
Loading

0 comments on commit 873650e

Please sign in to comment.