1717use Graze \DataStructure \Collection \Collection ;
1818use Graze \ParallelProcess \Event \EventDispatcherTrait ;
1919use Graze \ParallelProcess \Event \PoolRunEvent ;
20+ use Graze \ParallelProcess \Event \PriorityChangedEvent ;
2021use Graze \ParallelProcess \Event \RunEvent ;
2122use InvalidArgumentException ;
2223use Symfony \Component \Process \Process ;
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
0 commit comments