Skip to content

Commit 84ccec5

Browse files
author
Harry Bragg
authored
coverage->100%, refactor priorities, add more tests to prioritypool (#19)
* coverage->100%, refactor priorities, add more tests to prioritypool * fix some scruitinzer stuff
1 parent b053045 commit 84ccec5

15 files changed

Lines changed: 370 additions & 92 deletions

composer.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/CallbackRun.php

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515

1616
use Exception;
1717
use Graze\ParallelProcess\Event\EventDispatcherTrait;
18+
use Graze\ParallelProcess\Event\PriorityChangedEvent;
1819
use Graze\ParallelProcess\Event\RunEvent;
1920
use Throwable;
2021

21-
class CallbackRun implements RunInterface, OutputterInterface
22+
class CallbackRun implements RunInterface, OutputterInterface, PrioritisedInterface
2223
{
2324
use EventDispatcherTrait;
2425
use RunningStateTrait;
26+
use PrioritisedTrait;
2527

2628
/** @var callable */
2729
private $callback;
@@ -33,8 +35,6 @@ class CallbackRun implements RunInterface, OutputterInterface
3335
private $exception = null;
3436
/** @var string */
3537
private $last;
36-
/** @var float */
37-
private $priority;
3838

3939
/**
4040
* Run constructor.
@@ -51,17 +51,6 @@ public function __construct(callable $callback, array $tags = [], $priority = 1.
5151
$this->priority = $priority;
5252
}
5353

54-
/**
55-
* @param float $priority
56-
*
57-
* @return CallbackRun
58-
*/
59-
public function setPriority($priority)
60-
{
61-
$this->priority = $priority;
62-
return $this;
63-
}
64-
6554
/**
6655
* @return string[]
6756
*/
@@ -73,6 +62,7 @@ protected function getEventNames()
7362
RunEvent::SUCCESSFUL,
7463
RunEvent::FAILED,
7564
RunEvent::UPDATED,
65+
PriorityChangedEvent::CHANGED,
7666
];
7767
}
7868

@@ -216,12 +206,4 @@ public function getLastMessageType()
216206
{
217207
return '';
218208
}
219-
220-
/**
221-
* @return float The priority for this run, where the larger the number the higher the priority
222-
*/
223-
public function getPriority()
224-
{
225-
return $this->priority;
226-
}
227209
}

src/Display/Table.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,14 @@ function (RunEvent $event) use ($index, &$bar, &$spinner) {
150150
*/
151151
private function getSummary()
152152
{
153-
if (!$this->pool instanceof RunInterface) {
154-
return '';
155-
}
156-
157-
if ($this->pool->hasStarted()) {
158-
if ($this->pool->isRunning()) {
153+
$running = count($this->pool->getRunning());
154+
$finished = count($this->pool->getFinished());
155+
if ($running > 0 || $finished > 0) {
156+
if ($running > 0) {
159157
return sprintf(
160158
'<comment>Total</comment>: %2d, <comment>Running</comment>: %2d, <comment>Waiting</comment>: %2d',
161159
$this->pool->count(),
162-
count($this->pool->getRunning()),
160+
$running,
163161
count($this->pool->getWaiting())
164162
);
165163
} else {

src/Event/PriorityChangedEvent.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* This file is part of graze/parallel-process.
4+
*
5+
* Copyright © 2018 Nature Delivered Ltd. <https://www.graze.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
* @license https://github.com/graze/parallel-process/blob/master/LICENSE.md
11+
* @link https://github.com/graze/parallel-process
12+
*/
13+
14+
namespace Graze\ParallelProcess\Event;
15+
16+
use Graze\ParallelProcess\PrioritisedInterface;
17+
use Symfony\Component\EventDispatcher\Event;
18+
19+
class PriorityChangedEvent extends Event
20+
{
21+
const CHANGED = 'priority.changed';
22+
23+
/** @var PrioritisedInterface */
24+
private $item;
25+
/** @var float */
26+
private $priority;
27+
/** @var float|null */
28+
private $oldPriority;
29+
30+
/**
31+
* RunEvent constructor.
32+
*
33+
* @param PrioritisedInterface $item
34+
* @param float $priority
35+
* @param float|null $oldPriority
36+
*/
37+
public function __construct(PrioritisedInterface $item, $priority, $oldPriority = null)
38+
{
39+
$this->item = $item;
40+
$this->priority = $priority;
41+
$this->oldPriority = $oldPriority;
42+
}
43+
44+
/**
45+
* @return PrioritisedInterface
46+
*/
47+
public function getItem()
48+
{
49+
return $this->item;
50+
}
51+
}

src/Monitor/PoolLogger.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ public function onRunStarted(RunEvent $event)
104104
public function onRunSuccessful(RunEvent $event)
105105
{
106106
$this->logger->debug(
107-
sprintf('run [%s:%s]: successfully finished', get_class($event->getRun()), spl_object_hash($event->getRun())),
107+
sprintf(
108+
'run [%s:%s]: successfully finished',
109+
get_class($event->getRun()),
110+
spl_object_hash($event->getRun())
111+
),
108112
$this->getTags($event->getRun())
109113
);
110114
}
@@ -137,7 +141,11 @@ function (Exception $e) {
137141
public function onRunCompleted(RunEvent $event)
138142
{
139143
$this->logger->debug(
140-
sprintf('run [%s:%s]: has finished running', get_class($event->getRun()), spl_object_hash($event->getRun())),
144+
sprintf(
145+
'run [%s:%s]: has finished running',
146+
get_class($event->getRun()),
147+
spl_object_hash($event->getRun())
148+
),
141149
$this->getTags($event->getRun())
142150
);
143151
}
@@ -151,10 +159,8 @@ private function getTags($item)
151159
{
152160
if ($item instanceof PoolInterface) {
153161
return $this->getPoolTags($item);
154-
} elseif ($item instanceof RunInterface) {
155-
return $this->getRunTags($item);
156162
}
157-
return [];
163+
return $this->getRunTags($item);
158164
}
159165

160166
/**

src/Pool.php

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Graze\DataStructure\Collection\Collection;
1818
use Graze\ParallelProcess\Event\EventDispatcherTrait;
1919
use Graze\ParallelProcess\Event\PoolRunEvent;
20+
use Graze\ParallelProcess\Event\PriorityChangedEvent;
2021
use Graze\ParallelProcess\Event\RunEvent;
2122
use InvalidArgumentException;
2223
use Symfony\Component\Process\Process;
@@ -28,12 +29,20 @@
2829
* A Pool is a arbitrary collection of runs that can be used to group runs together when displaying with a
2930
* Table
3031
*
32+
* A Pool can transition from `not_running` back to `running` again. But it cannot transition back to `not_started`.
33+
* This means that multiple `COMPLETED` and `STARTED` events can be sent out for a single pool.
34+
*
35+
* ```
36+
* not_started -> running <-> not_running
37+
* ```
38+
*
3139
* @package Graze\ParallelProcess
3240
*/
33-
class Pool extends Collection implements RunInterface, PoolInterface
41+
class Pool extends Collection implements RunInterface, PoolInterface, PrioritisedInterface
3442
{
3543
use EventDispatcherTrait;
3644
use RunningStateTrait;
45+
use PrioritisedTrait;
3746

3847
/** @var RunInterface[] */
3948
protected $items = [];
@@ -47,8 +56,6 @@ class Pool extends Collection implements RunInterface, PoolInterface
4756
private $exceptions = [];
4857
/** @var array */
4958
private $tags;
50-
/** @var float */
51-
private $priority;
5259

5360
/**
5461
* RunCollection constructor.
@@ -89,7 +96,7 @@ public function add($item, array $tags = [])
8996
$this->running[] = $item;
9097
} elseif ($item->hasStarted()) {
9198
$status = 'finished';
92-
$this->finished[] = $item;
99+
$this->complete[] = $item;
93100
} else {
94101
$this->waiting[] = $item;
95102
}
@@ -100,11 +107,13 @@ public function add($item, array $tags = [])
100107

101108
$this->dispatch(PoolRunEvent::POOL_RUN_ADDED, new PoolRunEvent($this, $item));
102109

103-
if ($status == 'running' || $status == 'finished') {
104-
if ($this->state == static::STATE_NOT_STARTED) {
105-
$this->setStarted();
106-
$this->dispatch(RunEvent::STARTED, new RunEvent($this));
107-
}
110+
if ($status != 'waiting' && $this->state != static::STATE_RUNNING) {
111+
$this->setStarted();
112+
$this->dispatch(RunEvent::STARTED, new RunEvent($this));
113+
}
114+
if ($status == 'finished' && $this->state != static::STATE_NOT_RUNNING) {
115+
$this->setFinished();
116+
$this->dispatch(RunEvent::COMPLETED, new RunEvent($this));
108117
}
109118

110119
return $this;
@@ -259,25 +268,6 @@ public function getProgress()
259268
return [count($this->complete), count($this->items), count($this->complete) / count($this->items)];
260269
}
261270

262-
/**
263-
* @return float
264-
*/
265-
public function getPriority()
266-
{
267-
return $this->priority;
268-
}
269-
270-
/**
271-
* @param float $priority
272-
*
273-
* @return $this
274-
*/
275-
public function setPriority($priority)
276-
{
277-
$this->priority = $priority;
278-
return $this;
279-
}
280-
281271
/**
282272
* @return string[]
283273
*/
@@ -290,6 +280,7 @@ protected function getEventNames()
290280
RunEvent::FAILED,
291281
RunEvent::UPDATED,
292282
PoolRunEvent::POOL_RUN_ADDED,
283+
PriorityChangedEvent::CHANGED,
293284
];
294285
}
295286

src/PrioritisedInterface.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Graze\ParallelProcess;
4+
5+
interface PrioritisedInterface
6+
{
7+
/**
8+
* Get the priority for this item. The higher the number the higher the priority
9+
*
10+
* @return float
11+
*/
12+
public function getPriority();
13+
14+
/**
15+
* Fluent call for setting the priority.
16+
*
17+
* @param float $priority The higher the number the higher the priority
18+
*
19+
* @return $this
20+
*/
21+
public function setPriority($priority);
22+
}

src/PrioritisedTrait.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Graze\ParallelProcess;
4+
5+
use Graze\ParallelProcess\Event\DispatcherInterface;
6+
use Graze\ParallelProcess\Event\PriorityChangedEvent;
7+
8+
trait PrioritisedTrait
9+
{
10+
/** @var float */
11+
protected $priority;
12+
13+
/**
14+
* @param float $priority
15+
*
16+
* @return $this
17+
*/
18+
public function setPriority($priority)
19+
{
20+
$oldPriority = $this->priority;
21+
$this->priority = $priority;
22+
if (!($this instanceof RunInterface && $this->hasStarted())
23+
&& method_exists($this, 'dispatch')
24+
&& $this instanceof PrioritisedInterface) {
25+
$this->dispatch(PriorityChangedEvent::CHANGED, new PriorityChangedEvent($this, $priority, $oldPriority));
26+
}
27+
return $this;
28+
}
29+
30+
/**
31+
* @return float
32+
*/
33+
public function getPriority()
34+
{
35+
return $this->priority;
36+
}
37+
}

0 commit comments

Comments
 (0)