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

AbortableProcess   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 3
c 1
b 1
f 0
lcom 1
cbo 1
dl 0
loc 18
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 16 3
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