Skip to content

Commit a616e9d

Browse files
committed
Fixed error that finished processes are not cleaned correctly.
Removed processSuccessCallback and processFailCallback: $process->isSuccessful() can be used in processFinishCallback.
1 parent a9a6aa6 commit a616e9d

File tree

3 files changed

+18
-102
lines changed

3 files changed

+18
-102
lines changed

README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ The process manager allows for some callbacks to be specified, which get called
4141

4242
* **processStartCallback:** Triggered before a process is started.
4343
* **processFinishCallback:** Triggered when a process has finished.
44-
* **processSuccessCallback:** Triggered when a processes has finished with an exit code of 0.
45-
* **processFailCallback:** Triggered when a processes has failed with an exit code other than 0.
46-
47-
_Note:_ Each process will trigger either the `processSuccessCallback` or the `processFailCallback` depending of its exit
48-
code. The `processFinishCallback` will always be triggered afterwards, ignoring the exit code.
4944

5045
Each callback gets the process instance which triggered the event passed as only parameter. Here is an example of
5146
setting a `processStartCallback`:

src/ProcessManager.php

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,6 @@ class ProcessManager
5050
*/
5151
protected $processFinishCallback;
5252

53-
/**
54-
* The callback for when a process has successfully finished with exit code 0.
55-
* @var callable|null
56-
*/
57-
protected $processSuccessCallback;
58-
59-
/**
60-
* The callback for when a process has failed to finish with exit code 0.
61-
* @var callable|null
62-
*/
63-
protected $processFailCallback;
64-
6553
/**
6654
* ProcessManager constructor.
6755
* @param int $numberOfParallelProcesses The number of processes to run in parallel.
@@ -95,28 +83,6 @@ public function setProcessFinishCallback(?callable $processFinishCallback)
9583
return $this;
9684
}
9785

98-
/**
99-
* Sets the callback for when a process has failed to finish with exit code 0.
100-
* @param callable|null $processSuccessCallback The callback, accepting a Process as only argument.
101-
* @return $this
102-
*/
103-
public function setProcessSuccessCallback(?callable $processSuccessCallback)
104-
{
105-
$this->processSuccessCallback = $processSuccessCallback;
106-
return $this;
107-
}
108-
109-
/**
110-
* Sets the callback for when a process has successfully finished with exit code 0.
111-
* @param callable|null $processFailCallback The callback, accepting a Process as only argument.
112-
* @return $this
113-
*/
114-
public function setProcessFailCallback(?callable $processFailCallback)
115-
{
116-
$this->processFailCallback = $processFailCallback;
117-
return $this;
118-
}
119-
12086
/**
12187
* Invokes the callback if it is an callable.
12288
* @param callable|null $callback
@@ -173,26 +139,23 @@ protected function canExecuteNextPendingRequest(): bool
173139
*/
174140
protected function checkRunningProcesses(): void
175141
{
176-
foreach ($this->runningProcesses as $process) {
177-
$this->checkRunningProcess($process);
142+
foreach ($this->runningProcesses as $pid => $process) {
143+
$this->checkRunningProcess($pid, $process);
178144
}
179145
}
180146

181147
/**
182148
* Checks the process whether it has finished.
149+
* @param int $pid
183150
* @param Process $process
184151
*/
185-
protected function checkRunningProcess(Process $process): void
152+
protected function checkRunningProcess(int $pid, Process $process): void
186153
{
187154
$process->checkTimeout();
188155
if (!$process->isRunning()) {
189-
$this->invokeCallback(
190-
$process->isSuccessful() ? $this->processSuccessCallback : $this->processFailCallback,
191-
$process
192-
);
193156
$this->invokeCallback($this->processFinishCallback, $process);
194157

195-
unset($this->runningProcesses[$process->getPid()]);
158+
unset($this->runningProcesses[$pid]);
196159
$this->executeNextPendingProcess();
197160
}
198161
}

test/src/ProcessManagerTest.php

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -67,36 +67,6 @@ public function testSetProcessFinishCallback(): void
6767
$this->assertSame($callback, $this->extractProperty($manager, 'processFinishCallback'));
6868
}
6969

70-
/**
71-
* Tests the setProcessSuccessCallback method.
72-
* @covers ::setProcessSuccessCallback
73-
* @throws ReflectionException
74-
*/
75-
public function testSetProcessSuccessCallback(): void
76-
{
77-
$callback = 'strval';
78-
79-
$manager = new ProcessManager();
80-
$result = $manager->setProcessSuccessCallback($callback);
81-
$this->assertSame($manager, $result);
82-
$this->assertSame($callback, $this->extractProperty($manager, 'processSuccessCallback'));
83-
}
84-
85-
/**
86-
* Tests the setProcessFailCallback method.
87-
* @covers ::setProcessFailCallback
88-
* @throws ReflectionException
89-
*/
90-
public function testSetProcessFailCallback(): void
91-
{
92-
$callback = 'strval';
93-
94-
$manager = new ProcessManager();
95-
$result = $manager->setProcessFailCallback($callback);
96-
$this->assertSame($manager, $result);
97-
$this->assertSame($callback, $this->extractProperty($manager, 'processFailCallback'));
98-
}
99-
10070
/**
10171
* Provides the data for the invokeCallback test.
10272
* @return array
@@ -109,7 +79,6 @@ public function provideInvokeCallback(): array
10979
];
11080
}
11181

112-
11382
/**
11483
* Tests the invokeCallback method.
11584
* @param bool $withCallback
@@ -292,10 +261,10 @@ public function testCheckRunningProcesses(): void
292261
$manager->expects($this->exactly(2))
293262
->method('checkRunningProcess')
294263
->withConsecutive(
295-
[$process1],
296-
[$process2]
264+
[42, $process1],
265+
[1337, $process2]
297266
);
298-
$this->injectProperty($manager, 'runningProcesses', [$process1, $process2]);
267+
$this->injectProperty($manager, 'runningProcesses', [42 => $process1, 1337 => $process2]);
299268

300269
$this->invokeMethod($manager, 'checkRunningProcesses');
301270
}
@@ -307,39 +276,33 @@ public function testCheckRunningProcesses(): void
307276
public function provideCheckRunningProcess(): array
308277
{
309278
return [
310-
[true, null, false],
311-
[false, true, true],
312-
[false, false, true],
279+
[true, false],
280+
[false, true],
281+
[false, true],
313282
];
314283
}
315284

316285
/**
317286
* Tests the checkRunningProcess method.
318287
* @param bool $resultIsRunning
319-
* @param bool|null $resultIsSuccessful
320288
* @param bool $expectFinish
321289
* @throws ReflectionException
322290
* @covers ::checkRunningProcess
323291
* @dataProvider provideCheckRunningProcess
324292
*/
325-
public function testCheckRunningProcess(bool $resultIsRunning, ?bool $resultIsSuccessful, bool $expectFinish): void
293+
public function testCheckRunningProcess(bool $resultIsRunning, bool $expectFinish): void
326294
{
295+
$pid = 42;
327296
/* @var Process|MockObject $process */
328297
$process = $this->getMockBuilder(Process::class)
329-
->setMethods(['checkTimeout', 'isRunning', 'isSuccessful', 'getPid'])
298+
->setMethods(['checkTimeout', 'isRunning'])
330299
->disableOriginalConstructor()
331300
->getMock();
332301
$process->expects($this->once())
333302
->method('checkTimeout');
334303
$process->expects($this->once())
335304
->method('isRunning')
336305
->willReturn($resultIsRunning);
337-
$process->expects($resultIsSuccessful === null ? $this->never() : $this->once())
338-
->method('isSuccessful')
339-
->willReturn($resultIsSuccessful);
340-
$process->expects($expectFinish ? $this->once() : $this->never())
341-
->method('getPid')
342-
->willReturn(42);
343306

344307
/* @var Process $process2 */
345308
$process2 = $this->createMock(Process::class);
@@ -351,20 +314,15 @@ public function testCheckRunningProcess(bool $resultIsRunning, ?bool $resultIsSu
351314
->setMethods(['invokeCallback', 'executeNextPendingProcess'])
352315
->disableOriginalConstructor()
353316
->getMock();
354-
$manager->expects($expectFinish ? $this->exactly(2) : $this->never())
317+
$manager->expects($expectFinish ? $this->once() : $this->never())
355318
->method('invokeCallback')
356-
->withConsecutive(
357-
[(bool) $resultIsSuccessful ? 'intval' : 'floatval', $process],
358-
['strval', $process]
359-
);
319+
->with('strval', $process);
360320
$manager->expects($expectFinish ? $this->once() : $this->never())
361321
->method('executeNextPendingProcess');
362-
$manager->setProcessFinishCallback('strval')
363-
->setProcessSuccessCallback('intval')
364-
->setProcessFailCallback('floatval');
322+
$manager->setProcessFinishCallback('strval');
365323
$this->injectProperty($manager, 'runningProcesses', $runningProcesses);
366324

367-
$this->invokeMethod($manager, 'checkRunningProcess', $process);
325+
$this->invokeMethod($manager, 'checkRunningProcess', $pid, $process);
368326
$this->assertEquals($expectedRunningProcesses, $this->extractProperty($manager, 'runningProcesses'));
369327
}
370328

0 commit comments

Comments
 (0)