Skip to content
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

Draft: Keep JobCollection but use Iterator #19

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Infrastructure/ForkingProcessingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function process(JobCollection $jobs): void
{
$pidList = [];
$joblist = [];
foreach ($jobs->all() as $job) {
foreach ($jobs as $job) {
while (count($pidList) === $this->maxForks) {
foreach ($pidList as $pos => $pId) {
$code = pcntl_waitpid($pId, $status, WNOHANG);
Expand Down
2 changes: 1 addition & 1 deletion src/Infrastructure/SingleProcessingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(Executor $executor)

public function process(JobCollection $jobs): void
{
foreach ($jobs->all() as $job) {
foreach ($jobs as $job) {
$this->executor->process($job);
}
}
Expand Down
38 changes: 36 additions & 2 deletions src/Job/JobCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

namespace simpleQueue\Job;

class JobCollection
use ArrayIterator;

class JobCollection implements JobIterator
{
private array $items = [];

public function add(Job $job)
private int $position = 0;

public function add(Job $job): void
{
$this->items[] = $job;
}
Expand All @@ -20,4 +24,34 @@ public function all(): array
{
return $this->items;
}

public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->items);
}

public function current(): mixed
{
return $this->items[$this->position];
}

public function next(): void
{
$this->position++;
}

public function key(): int
{
return $this->position;
}

public function valid(): bool
{
return isset($this->items[$this->position]);
}

public function rewind(): void
{
$this->position = 0;
}
}
9 changes: 9 additions & 0 deletions src/Job/JobIterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace simpleQueue\Job;

interface JobIterator extends \Iterator

Check failure on line 7 in src/Job/JobIterator.php

View workflow job for this annotation

GitHub Actions / Static Code Analyzer (8.0, locked)

MissingTemplateParam

src/Job/JobIterator.php:7:31: MissingTemplateParam: simpleQueue\Job\JobIterator has missing template params when extending Iterator, expecting 2 (see https://psalm.dev/182)
{
}
Loading