Completed
Pull Request — master (#597)
by Mateusz
03:39
created

AbortableProcess::run()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 16
rs 9.4285
cc 3
eloc 10
nc 4
nop 1
1
<?php
2
/**
3
 * AbortableProcess implements a workaround to make Symfony Process not kill itself when we
4
 * trap a Unix signal.
5
 *
6
 * Process is relying on error_get_last to learn if the stream reading was interrupted by a signal
7
 * (see UnixSignal's readAndWrite, the hasSystemCallBeenInterrupted call). Unfortunately this doesn't
8
 * work if we trapp the signal, which wipes out the error, which makes Process think it's some
9
 * other kind of error which makes it commit suicide for no good reason.
10
 *
11
 * Workaround is to avoid blocking stream_select as ->run nor ->wait do. Fortunately we can call
12
 * ->isRunning instead, which uses a non-blocking readPipes via updateStatus. This means our callback
13
 * will still get called if we loop over ->isRunning.
14
 */
15
class AbortableProcess extends \Symfony\Component\Process\Process {
16
	public function run($callback = null) {
17
		$this->start($callback);
18
		while($this->isRunning()) {
19
			$this->checkTimeout();
20
			sleep(1);
21
		}
22
23
		if ($this->hasBeenSignaled()) {
24
			throw new RuntimeException(sprintf(
25
				'The process has been signaled with signal "%s".',
26
				$this->getTermSignal())
27
			);
28
		}
29
30
		return $this->getExitCode();
31
	}
32
}
33