1 | <?php |
||
39 | class ProcessIterator implements \Iterator |
||
40 | { |
||
41 | |||
42 | /** |
||
43 | * Key of the processes, that are waiting to be executed. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $waitingQueue = array(); |
||
48 | |||
49 | /** |
||
50 | * Keys of the processes, that are currently running. |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $runningQueue = array(); |
||
55 | |||
56 | /** |
||
57 | * Processes to iterate over. |
||
58 | * |
||
59 | * @var Process[] |
||
60 | */ |
||
61 | protected $processes = array(); |
||
62 | |||
63 | /** |
||
64 | * All of the processes must be executed successfully. |
||
65 | * |
||
66 | * @var boolean |
||
67 | */ |
||
68 | protected $mustRun = false; |
||
69 | |||
70 | /** |
||
71 | * Last exception, thrown by each process. |
||
72 | * |
||
73 | * @var \Exception[] |
||
74 | */ |
||
75 | protected $exceptions = array(); |
||
76 | |||
77 | /** |
||
78 | * Current process key. |
||
79 | * |
||
80 | * @var mixed |
||
81 | */ |
||
82 | protected $key; |
||
83 | |||
84 | /** |
||
85 | * Maximal number of simultaneously executing processes. |
||
86 | * |
||
87 | * @var integer |
||
88 | */ |
||
89 | protected $limit; |
||
90 | |||
91 | /** |
||
92 | * Maximal amount of time to wait before iterator will yield the result. |
||
93 | * |
||
94 | * @var float |
||
95 | */ |
||
96 | protected $timeout; |
||
97 | |||
98 | /** |
||
99 | * Result waiting timeout was reached. |
||
100 | * |
||
101 | * @var boolean |
||
102 | */ |
||
103 | protected $isTimeout = false; |
||
104 | |||
105 | /** |
||
106 | * Create a new iterator over a list of processes. |
||
107 | * |
||
108 | * @param array $processes List of processes to execute. |
||
109 | * @param boolean $must_run All of the processes must be executed successfully. |
||
110 | * |
||
111 | * @throws \InvalidArgumentException When unknown elements are present in $processes array. |
||
112 | */ |
||
113 | public function __construct(array $processes, $must_run = false) |
||
129 | |||
130 | /** |
||
131 | * Block until all processes have executed. |
||
132 | * |
||
133 | * @return self |
||
134 | */ |
||
135 | 1 | public function runAll() |
|
143 | |||
144 | /** |
||
145 | * Add another process to the set of processes. This is useful if you have a |
||
146 | * set of processes to run mostly in parallel, but some processes depend on |
||
147 | * others. |
||
148 | * |
||
149 | * @param Process $process Process to add to iterator. |
||
150 | * @param mixed $key Key. |
||
151 | * |
||
152 | * @return self |
||
153 | * @throws \InvalidArgumentException When given key is already in use. |
||
154 | */ |
||
155 | 4 | public function addProcess(Process $process, $key = null) |
|
177 | |||
178 | /** |
||
179 | * Set a maximum amount of time you want to wait before the iterator will |
||
180 | * yield a result. If no process has executed yet, the iterator will yield |
||
181 | * empty string for key and null for value. Among other potential uses, you |
||
182 | * can use this to show some busy indicator: |
||
183 | * $processes = (new ProcessIterator($processes)) |
||
184 | * ->setUpdateInterval(1); |
||
185 | * foreach ($processes as $process) { |
||
186 | * if ($process === null) { |
||
187 | * echo "Still working...\n"; |
||
188 | * } else { |
||
189 | * // ... |
||
190 | * } |
||
191 | * } |
||
192 | * This will echo "Still working..." once per second as long as processes are |
||
193 | * resolving. By default, ProcessIterator never yields null. |
||
194 | * |
||
195 | * @param float $interval Maximum number of seconds to block waiting on processes before yielding null. |
||
196 | * |
||
197 | * @return self |
||
198 | */ |
||
199 | 1 | public function setUpdateInterval($interval) |
|
205 | |||
206 | /** |
||
207 | * Limit the number of simultaneously executing processes. |
||
208 | * $processes = (new ProcessIterator($processes)) |
||
209 | * ->limit(4); |
||
210 | * foreach ($processes as $process) { |
||
211 | * // Run no more than 4 processes simultaneously. |
||
212 | * } |
||
213 | * |
||
214 | * @param integer $max Maximum number of simultaneously running processes allowed. |
||
215 | * |
||
216 | * @return self |
||
217 | */ |
||
218 | 3 | public function limit($max) |
|
224 | |||
225 | /** |
||
226 | * Rewind the Iterator to the first element. |
||
227 | * |
||
228 | * @return void |
||
229 | */ |
||
230 | 10 | public function rewind() |
|
237 | |||
238 | /** |
||
239 | * Move forward to next element. |
||
240 | * |
||
241 | * @return void |
||
242 | */ |
||
|
|||
243 | 10 | public function next() |
|
304 | |||
305 | /** |
||
306 | * Remembers exception, associated with a process. |
||
307 | * |
||
308 | * @param mixed $key Process key. |
||
309 | * @param \Exception $exception Exception. |
||
310 | * |
||
311 | * @return void |
||
312 | */ |
||
313 | 2 | protected function setProcessException($key, \Exception $exception) |
|
317 | |||
318 | /** |
||
319 | * Gets exception, associated with a process. |
||
320 | * |
||
321 | * @return \Exception|null |
||
322 | */ |
||
323 | 4 | public function getProcessException() |
|
329 | |||
330 | /** |
||
331 | * Return the current element. |
||
332 | * |
||
333 | * @return Process|null |
||
334 | */ |
||
335 | 10 | public function current() |
|
343 | |||
344 | /** |
||
345 | * Return the key of the current element. |
||
346 | * |
||
347 | * @return mixed|null |
||
348 | */ |
||
349 | 8 | public function key() |
|
358 | |||
359 | /** |
||
360 | * Checks if current position is valid. |
||
361 | * |
||
362 | * @return boolean |
||
363 | */ |
||
364 | 10 | public function valid() |
|
372 | |||
373 | /** |
||
374 | * Ensure, that needed number of processes are running in parallel. |
||
375 | * |
||
376 | * @return void |
||
377 | */ |
||
378 | 10 | protected function updateWorkingSet() |
|
396 | |||
397 | } |
||
398 |