From ed12e8917783dbdae50d7f28a63546b2f09a88a6 Mon Sep 17 00:00:00 2001 From: Tillmann Schiffler Date: Sun, 11 Feb 2024 17:26:21 +0100 Subject: [PATCH 1/2] Draft: Keep JobCollection but use Iterator --- .../ForkingProcessingStrategy.php | 2 +- .../SingleProcessingStrategy.php | 2 +- src/Job/JobCollection.php | 34 ++++++++++++++++++- src/Job/JobIterator.php | 9 +++++ 4 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 src/Job/JobIterator.php diff --git a/src/Infrastructure/ForkingProcessingStrategy.php b/src/Infrastructure/ForkingProcessingStrategy.php index efffccb..8f5cecb 100644 --- a/src/Infrastructure/ForkingProcessingStrategy.php +++ b/src/Infrastructure/ForkingProcessingStrategy.php @@ -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); diff --git a/src/Infrastructure/SingleProcessingStrategy.php b/src/Infrastructure/SingleProcessingStrategy.php index c669408..57eb7fe 100644 --- a/src/Infrastructure/SingleProcessingStrategy.php +++ b/src/Infrastructure/SingleProcessingStrategy.php @@ -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); } } diff --git a/src/Job/JobCollection.php b/src/Job/JobCollection.php index c7b0617..163c7ed 100644 --- a/src/Job/JobCollection.php +++ b/src/Job/JobCollection.php @@ -4,10 +4,12 @@ namespace simpleQueue\Job; -class JobCollection +class JobCollection implements JobIterator { private array $items = []; + private int $position = 0; + public function add(Job $job) { $this->items[] = $job; @@ -20,4 +22,34 @@ public function all(): array { return $this->items; } + + public function getIterator() + { + 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; + } } diff --git a/src/Job/JobIterator.php b/src/Job/JobIterator.php new file mode 100644 index 0000000..5b9ba54 --- /dev/null +++ b/src/Job/JobIterator.php @@ -0,0 +1,9 @@ + Date: Sun, 11 Feb 2024 17:56:22 +0100 Subject: [PATCH 2/2] Draft: Missed some changes --- src/Job/JobCollection.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Job/JobCollection.php b/src/Job/JobCollection.php index 163c7ed..e101046 100644 --- a/src/Job/JobCollection.php +++ b/src/Job/JobCollection.php @@ -4,13 +4,15 @@ namespace simpleQueue\Job; +use ArrayIterator; + class JobCollection implements JobIterator { private array $items = []; private int $position = 0; - public function add(Job $job) + public function add(Job $job): void { $this->items[] = $job; } @@ -23,9 +25,9 @@ public function all(): array return $this->items; } - public function getIterator() + public function getIterator(): ArrayIterator { - return new \ArrayIterator($this->items); + return new ArrayIterator($this->items); } public function current(): mixed