Passed
Pull Request — master (#10)
by Alice
02:01
created
src/AbstractThread.php 2 patches
Indentation   +83 added lines, -83 removed lines patch added patch discarded remove patch
@@ -6,88 +6,88 @@
 block discarded – undo
6 6
 
7 7
 abstract class AbstractThread extends AbstractThreadMediator
8 8
 {
9
-	const EXIT_STATUS_SUCCESS = 0;
10
-	const EXIT_STATUS_ERROR = 1;
11
-
12
-	/** @var int $pid */
13
-	private $pid;
14
-
15
-	/** @var string $processName */
16
-	private $processName;
17
-
18
-	/**
19
-	 * AbstractThread constructor.
20
-	 *
21
-	 * @param string $processName
22
-	 */
23
-	public function __construct(string $processName)
24
-	{
25
-		$this->processName = $processName;
26
-	}
27
-
28
-	/**
29
-	 * Return the name of the method to process during the thread
30
-	 *
31
-	 * @return string
32
-	 */
33
-	abstract protected function getMethodName(): string;
34
-
35
-	/**
36
-	 * Return the list of dependencies that will be passed as parameters of the method referenced by getMethodName
37
-	 *
38
-	 * @return array
39
-	 */
40
-	abstract protected function getDependencies(): array;
41
-
42
-	/**
43
-	 * @return int|null
44
-	 */
45
-	public function getPid(): ?int
46
-	{
47
-		return $this->pid;
48
-	}
49
-
50
-	/**
51
-	 * @param int $pid
52
-	 * @return AbstractThread
53
-	 */
54
-	public function setPid(int $pid): self
55
-	{
56
-		$this->pid = $pid;
57
-
58
-		return $this;
59
-	}
60
-
61
-	/**
62
-	 * @return string
63
-	 */
64
-	public function getProcessName(): ?string
65
-	{
66
-		return $this->processName;
67
-	}
68
-
69
-	/**
70
-	 * @return int
71
-	 * @throws ThreadException
72
-	 */
73
-	public function run(): int
74
-	{
75
-		if (false === method_exists($this, $this->getMethodName())) {
76
-			throw new ThreadException('No proper method defined for the thread');
77
-		}
78
-
79
-		$status = call_user_func_array(
80
-			[$this, $this->getMethodName()],
81
-			array_merge([$this->getProcessName()], $this->getDependencies())
82
-		);
83
-
84
-		if (null === $status) {
85
-			throw new ThreadException(
86
-				'Error. You must return a process status in '.get_class($this).':'.$this->getMethodName()
87
-			);
88
-		}
89
-
90
-		return $status;
91
-	}
9
+    const EXIT_STATUS_SUCCESS = 0;
10
+    const EXIT_STATUS_ERROR = 1;
11
+
12
+    /** @var int $pid */
13
+    private $pid;
14
+
15
+    /** @var string $processName */
16
+    private $processName;
17
+
18
+    /**
19
+     * AbstractThread constructor.
20
+     *
21
+     * @param string $processName
22
+     */
23
+    public function __construct(string $processName)
24
+    {
25
+        $this->processName = $processName;
26
+    }
27
+
28
+    /**
29
+     * Return the name of the method to process during the thread
30
+     *
31
+     * @return string
32
+     */
33
+    abstract protected function getMethodName(): string;
34
+
35
+    /**
36
+     * Return the list of dependencies that will be passed as parameters of the method referenced by getMethodName
37
+     *
38
+     * @return array
39
+     */
40
+    abstract protected function getDependencies(): array;
41
+
42
+    /**
43
+     * @return int|null
44
+     */
45
+    public function getPid(): ?int
46
+    {
47
+        return $this->pid;
48
+    }
49
+
50
+    /**
51
+     * @param int $pid
52
+     * @return AbstractThread
53
+     */
54
+    public function setPid(int $pid): self
55
+    {
56
+        $this->pid = $pid;
57
+
58
+        return $this;
59
+    }
60
+
61
+    /**
62
+     * @return string
63
+     */
64
+    public function getProcessName(): ?string
65
+    {
66
+        return $this->processName;
67
+    }
68
+
69
+    /**
70
+     * @return int
71
+     * @throws ThreadException
72
+     */
73
+    public function run(): int
74
+    {
75
+        if (false === method_exists($this, $this->getMethodName())) {
76
+            throw new ThreadException('No proper method defined for the thread');
77
+        }
78
+
79
+        $status = call_user_func_array(
80
+            [$this, $this->getMethodName()],
81
+            array_merge([$this->getProcessName()], $this->getDependencies())
82
+        );
83
+
84
+        if (null === $status) {
85
+            throw new ThreadException(
86
+                'Error. You must return a process status in '.get_class($this).':'.$this->getMethodName()
87
+            );
88
+        }
89
+
90
+        return $status;
91
+    }
92 92
 
93 93
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -83,7 +83,7 @@
 block discarded – undo
83 83
 
84 84
 		if (null === $status) {
85 85
 			throw new ThreadException(
86
-				'Error. You must return a process status in '.get_class($this).':'.$this->getMethodName()
86
+				'Error. You must return a process status in ' . get_class($this) . ':' . $this->getMethodName()
87 87
 			);
88 88
 		}
89 89
 
Please login to merge, or discard this patch.
src/AbstractThreadMediator.php 1 patch
Indentation   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -7,28 +7,28 @@
 block discarded – undo
7 7
 
8 8
 class AbstractThreadMediator
9 9
 {
10
-	/** @var Mediator */
11
-	private $mediator;
10
+    /** @var Mediator */
11
+    private $mediator;
12 12
 
13
-	/**
14
-	 * @return Mediator
15
-	 */
16
-	protected function getMediator()
17
-	{
18
-		return $this->mediator;
19
-	}
13
+    /**
14
+     * @return Mediator
15
+     */
16
+    protected function getMediator()
17
+    {
18
+        return $this->mediator;
19
+    }
20 20
 
21
-	public function setMediator(Mediator $mediator)
22
-	{
23
-		$this->mediator = $mediator;
24
-	}
21
+    public function setMediator(Mediator $mediator)
22
+    {
23
+        $this->mediator = $mediator;
24
+    }
25 25
 
26
-	/**
27
-	 * @param EventInterface $event
28
-	 */
29
-	protected function notify(EventInterface $event)
30
-	{
31
-		$this->getMediator()->notify($event);
32
-	}
26
+    /**
27
+     * @param EventInterface $event
28
+     */
29
+    protected function notify(EventInterface $event)
30
+    {
31
+        $this->getMediator()->notify($event);
32
+    }
33 33
 
34 34
 }
Please login to merge, or discard this patch.
src/ThreadPool.php 1 patch
Indentation   +267 added lines, -267 removed lines patch added patch discarded remove patch
@@ -7,272 +7,272 @@
 block discarded – undo
7 7
 
8 8
 class ThreadPool extends AbstractThreadPoolMediator
9 9
 {
10
-	// 0.2s
11
-	private const SLEEP_TIME_MS = 50000;
12
-
13
-	/** @var AbstractThread[] $childs */
14
-	private $threads;
15
-
16
-	/** @var AbstractThread[] $toRunThreads */
17
-	private $toRunThreads;
18
-
19
-	/** @var AbstractThread[] $runningChilds */
20
-	private $runningThreads;
21
-
22
-	/** @var bool $isRunning */
23
-	private $isRunning;
24
-
25
-	/** @var int $maxRunningThreadNb */
26
-	private $maxRunningThreadNb;
27
-
28
-	/**
29
-	 * ThreadPool constructor.
30
-	 */
31
-	public function __construct()
32
-	{
33
-		parent::__construct();
34
-		$this->threads = [];
35
-		$this->runningThreads = [];
36
-		$this->toRunThreads = [];
37
-		$this->isRunning = false;
38
-		$this->maxRunningThreadNb = 0;
39
-	}
40
-
41
-	/**
42
-	 *
43
-	 */
44
-	public function __destruct()
45
-	{
46
-		pcntl_waitpid(-1, $status, WNOHANG);
47
-	}
48
-
49
-	/**
50
-	 * @return AbstractThread[]
51
-	 */
52
-	public function getThreads(): array
53
-	{
54
-		return $this->threads;
55
-	}
56
-
57
-	/**
58
-	 * @param AbstractThread[] $threads
59
-	 * @return ThreadPool
60
-	 */
61
-	public function setThreads(array $threads): self
62
-	{
63
-		$this->threads = $threads;
64
-
65
-		return $this;
66
-	}
67
-
68
-	/**
69
-	 * @param AbstractThread $thread
70
-	 * @return ThreadPool
71
-	 */
72
-	public function addThread(AbstractThread $thread): self
73
-	{
74
-		$this->threads[] = $thread;
75
-
76
-		return $this;
77
-	}
78
-
79
-	/**
80
-	 * @return int
81
-	 */
82
-	public function getMaxRunningThreadNb(): int
83
-	{
84
-		return $this->maxRunningThreadNb;
85
-	}
86
-
87
-	/**
88
-	 * @param int $maxRunningThreadNb
89
-	 * @return ThreadPool
90
-	 */
91
-	public function setMaxRunningThreadNb(int $maxRunningThreadNb): self
92
-	{
93
-		$this->maxRunningThreadNb = $maxRunningThreadNb;
94
-
95
-		return $this;
96
-	}
97
-
98
-	/**
99
-	 * @return AbstractThread[]
100
-	 */
101
-	public function getToRunThreads(): array
102
-	{
103
-		return $this->toRunThreads;
104
-	}
105
-
106
-	/**
107
-	 * @return AbstractThread[]
108
-	 */
109
-	public function getRunningThreads(): array
110
-	{
111
-		return $this->runningThreads;
112
-	}
113
-
114
-	/**
115
-	 * @throws ThreadException
116
-	 */
117
-	public function run()
118
-	{
119
-		$this->checkEnv();
120
-		$this->initRun();
121
-
122
-		while ($this->isRunningThreads()) {
123
-			$this->waitOnThreads();
124
-		}
125
-
126
-		$this->resetRun();
127
-	}
128
-
129
-	/**
130
-	 * @return bool
131
-	 * @throws ThreadException
132
-	 */
133
-	private function isRunningThreads(): bool
134
-	{
135
-		if (count($this->toRunThreads) > 0) {
136
-			while (count($this->runningThreads) < $this->maxRunningThreadNb && count($this->toRunThreads) > 0) {
137
-				$this->createThreadProcess(array_shift($this->toRunThreads));
138
-			}
139
-		}
140
-
141
-		return count($this->runningThreads) > 0;
142
-	}
143
-
144
-	/**
145
-	 * can't test some part of it this since we can't unit-test in web and we're never in a child
146
-	 * process when pid 0 when unit-testing since the coverage is done by the parent thread
147
-	 *
148
-	 * @param AbstractThread $thread
149
-	 * @throws ThreadException
150
-	 */
151
-	private function createThreadProcess(AbstractThread $thread)
152
-	{
153
-		$pid = pcntl_fork();
154
-
155
-		switch ($pid) {
156
-			case -1: //error forking
157
-				// @codeCoverageIgnoreStart
158
-				throw new ThreadException('Error while trying to fork. Check your server installation');
159
-				// @codeCoverageIgnoreEnd
160
-			case 0: // child
161
-				// @codeCoverageIgnoreStart
162
-				$thread->setMediator($this->getMediator());
163
-				$this->processThread($thread);
164
-				break;
165
-				// @codeCoverageIgnoreEnd
166
-			default: //parent
167
-				$thread->setPid($pid);
168
-				$this->runningThreads[] = $thread;
169
-				$this->notify(PoolEvent::POOL_NEW_THREAD, $thread);
170
-				$this->startRunStatus();
171
-		}
172
-	}
173
-
174
-	/**
175
-	 *
176
-	 */
177
-	private function waitOnThreads()
178
-	{
179
-		$this->notify(PoolEvent::POOL_PRE_WAIT_TICK);
180
-		foreach ($this->runningThreads as $k => $thread) {
181
-
182
-			$res = pcntl_waitpid($thread->getPid(), $status, WNOHANG);
183
-			$this->notify(PoolEvent::POOL_WAIT_TICK_PID);
184
-
185
-			if ($res === -1 || $res > 0) {
186
-				$this->notify(PoolEvent::POOL_WAIT_TICK_PID_REMOVED, $thread);
187
-				unset($this->runningThreads[$k]);
188
-			}
189
-
190
-		}
191
-		$this->notify(PoolEvent::POOL_POST_WAIT_TICK);
192
-
193
-		usleep(self::SLEEP_TIME_MS);
194
-	}
195
-
196
-	/**
197
-	 * @codeCoverageIgnore Can't test since this is only run in a child thread.. which doesnt' go throug the
198
-	 * unit-test coverage which is only done in the main process
199
-	 * @param AbstractThread $thread
200
-	 * @throws ThreadException
201
-	 */
202
-	private function processThread(AbstractThread $thread)
203
-	{
204
-		$this->notify(PoolEvent::THREAD_PRE_PROCESS, $thread);
205
-		$response = $thread->run();
206
-		$this->notify(PoolEvent::THREAD_POST_PROCESS, $thread);
207
-
208
-		switch ($response) {
209
-			case AbstractThread::EXIT_STATUS_SUCCESS:
210
-				$this->notify(PoolEvent::THREAD_EXIT_SUCCESS, $thread);
211
-				break;
212
-			case AbstractThread::EXIT_STATUS_ERROR:
213
-				$this->notify(PoolEvent::THREAD_EXIT_ERROR, $thread);
214
-				break;
215
-			default:
216
-				$this->notify(PoolEvent::THREAD_EXIT_UNKNOWN, $thread);
217
-		}
218
-
219
-		exit($response);
220
-	}
221
-
222
-	/**
223
-	 * Can't test the exception is not in cli since php-unit is only run in cli environment
224
-	 * @throws ThreadException
225
-	 */
226
-	private function checkEnv()
227
-	{
228
-		if (false === $this->isCli()) {
229
-			// @codeCoverageIgnoreStart
230
-			throw new ThreadException('Error. It is not safe to use process forking in other way than php-cli');
231
-			// @codeCoverageIgnoreEnd
232
-		}
233
-		if (0 === count($this->threads)) {
234
-			throw new ThreadException('Error. Can\'t run child threads processes without any added in the Pool');
235
-		}
236
-	}
237
-
238
-	/**
239
-	 *
240
-	 */
241
-	private function initRun()
242
-	{
243
-		$this->resetRun();
244
-	}
245
-
246
-	/**
247
-	 * @return bool
248
-	 */
249
-	private function isCli(): bool
250
-	{
251
-		return PHP_SAPI === 'cli';
252
-	}
253
-
254
-	/**
255
-	 *
256
-	 */
257
-	private function startRunStatus()
258
-	{
259
-		if (false === $this->isRunning) {
260
-			$this->notify(PoolEvent::POOL_RUN_START);
261
-			$this->isRunning = true;
262
-		}
263
-	}
264
-
265
-	/**
266
-	 *
267
-	 */
268
-	private function resetRun()
269
-	{
270
-		if (true === $this->isRunning) {
271
-			$this->notify(PoolEvent::POOL_RUN_STOP);
272
-		}
273
-		$this->isRunning = false;
274
-		$this->toRunThreads = $this->threads;
275
-		$this->runningThreads = [];
276
-	}
10
+    // 0.2s
11
+    private const SLEEP_TIME_MS = 50000;
12
+
13
+    /** @var AbstractThread[] $childs */
14
+    private $threads;
15
+
16
+    /** @var AbstractThread[] $toRunThreads */
17
+    private $toRunThreads;
18
+
19
+    /** @var AbstractThread[] $runningChilds */
20
+    private $runningThreads;
21
+
22
+    /** @var bool $isRunning */
23
+    private $isRunning;
24
+
25
+    /** @var int $maxRunningThreadNb */
26
+    private $maxRunningThreadNb;
27
+
28
+    /**
29
+     * ThreadPool constructor.
30
+     */
31
+    public function __construct()
32
+    {
33
+        parent::__construct();
34
+        $this->threads = [];
35
+        $this->runningThreads = [];
36
+        $this->toRunThreads = [];
37
+        $this->isRunning = false;
38
+        $this->maxRunningThreadNb = 0;
39
+    }
40
+
41
+    /**
42
+     *
43
+     */
44
+    public function __destruct()
45
+    {
46
+        pcntl_waitpid(-1, $status, WNOHANG);
47
+    }
48
+
49
+    /**
50
+     * @return AbstractThread[]
51
+     */
52
+    public function getThreads(): array
53
+    {
54
+        return $this->threads;
55
+    }
56
+
57
+    /**
58
+     * @param AbstractThread[] $threads
59
+     * @return ThreadPool
60
+     */
61
+    public function setThreads(array $threads): self
62
+    {
63
+        $this->threads = $threads;
64
+
65
+        return $this;
66
+    }
67
+
68
+    /**
69
+     * @param AbstractThread $thread
70
+     * @return ThreadPool
71
+     */
72
+    public function addThread(AbstractThread $thread): self
73
+    {
74
+        $this->threads[] = $thread;
75
+
76
+        return $this;
77
+    }
78
+
79
+    /**
80
+     * @return int
81
+     */
82
+    public function getMaxRunningThreadNb(): int
83
+    {
84
+        return $this->maxRunningThreadNb;
85
+    }
86
+
87
+    /**
88
+     * @param int $maxRunningThreadNb
89
+     * @return ThreadPool
90
+     */
91
+    public function setMaxRunningThreadNb(int $maxRunningThreadNb): self
92
+    {
93
+        $this->maxRunningThreadNb = $maxRunningThreadNb;
94
+
95
+        return $this;
96
+    }
97
+
98
+    /**
99
+     * @return AbstractThread[]
100
+     */
101
+    public function getToRunThreads(): array
102
+    {
103
+        return $this->toRunThreads;
104
+    }
105
+
106
+    /**
107
+     * @return AbstractThread[]
108
+     */
109
+    public function getRunningThreads(): array
110
+    {
111
+        return $this->runningThreads;
112
+    }
113
+
114
+    /**
115
+     * @throws ThreadException
116
+     */
117
+    public function run()
118
+    {
119
+        $this->checkEnv();
120
+        $this->initRun();
121
+
122
+        while ($this->isRunningThreads()) {
123
+            $this->waitOnThreads();
124
+        }
125
+
126
+        $this->resetRun();
127
+    }
128
+
129
+    /**
130
+     * @return bool
131
+     * @throws ThreadException
132
+     */
133
+    private function isRunningThreads(): bool
134
+    {
135
+        if (count($this->toRunThreads) > 0) {
136
+            while (count($this->runningThreads) < $this->maxRunningThreadNb && count($this->toRunThreads) > 0) {
137
+                $this->createThreadProcess(array_shift($this->toRunThreads));
138
+            }
139
+        }
140
+
141
+        return count($this->runningThreads) > 0;
142
+    }
143
+
144
+    /**
145
+     * can't test some part of it this since we can't unit-test in web and we're never in a child
146
+     * process when pid 0 when unit-testing since the coverage is done by the parent thread
147
+     *
148
+     * @param AbstractThread $thread
149
+     * @throws ThreadException
150
+     */
151
+    private function createThreadProcess(AbstractThread $thread)
152
+    {
153
+        $pid = pcntl_fork();
154
+
155
+        switch ($pid) {
156
+            case -1: //error forking
157
+                // @codeCoverageIgnoreStart
158
+                throw new ThreadException('Error while trying to fork. Check your server installation');
159
+                // @codeCoverageIgnoreEnd
160
+            case 0: // child
161
+                // @codeCoverageIgnoreStart
162
+                $thread->setMediator($this->getMediator());
163
+                $this->processThread($thread);
164
+                break;
165
+                // @codeCoverageIgnoreEnd
166
+            default: //parent
167
+                $thread->setPid($pid);
168
+                $this->runningThreads[] = $thread;
169
+                $this->notify(PoolEvent::POOL_NEW_THREAD, $thread);
170
+                $this->startRunStatus();
171
+        }
172
+    }
173
+
174
+    /**
175
+     *
176
+     */
177
+    private function waitOnThreads()
178
+    {
179
+        $this->notify(PoolEvent::POOL_PRE_WAIT_TICK);
180
+        foreach ($this->runningThreads as $k => $thread) {
181
+
182
+            $res = pcntl_waitpid($thread->getPid(), $status, WNOHANG);
183
+            $this->notify(PoolEvent::POOL_WAIT_TICK_PID);
184
+
185
+            if ($res === -1 || $res > 0) {
186
+                $this->notify(PoolEvent::POOL_WAIT_TICK_PID_REMOVED, $thread);
187
+                unset($this->runningThreads[$k]);
188
+            }
189
+
190
+        }
191
+        $this->notify(PoolEvent::POOL_POST_WAIT_TICK);
192
+
193
+        usleep(self::SLEEP_TIME_MS);
194
+    }
195
+
196
+    /**
197
+     * @codeCoverageIgnore Can't test since this is only run in a child thread.. which doesnt' go throug the
198
+     * unit-test coverage which is only done in the main process
199
+     * @param AbstractThread $thread
200
+     * @throws ThreadException
201
+     */
202
+    private function processThread(AbstractThread $thread)
203
+    {
204
+        $this->notify(PoolEvent::THREAD_PRE_PROCESS, $thread);
205
+        $response = $thread->run();
206
+        $this->notify(PoolEvent::THREAD_POST_PROCESS, $thread);
207
+
208
+        switch ($response) {
209
+            case AbstractThread::EXIT_STATUS_SUCCESS:
210
+                $this->notify(PoolEvent::THREAD_EXIT_SUCCESS, $thread);
211
+                break;
212
+            case AbstractThread::EXIT_STATUS_ERROR:
213
+                $this->notify(PoolEvent::THREAD_EXIT_ERROR, $thread);
214
+                break;
215
+            default:
216
+                $this->notify(PoolEvent::THREAD_EXIT_UNKNOWN, $thread);
217
+        }
218
+
219
+        exit($response);
220
+    }
221
+
222
+    /**
223
+     * Can't test the exception is not in cli since php-unit is only run in cli environment
224
+     * @throws ThreadException
225
+     */
226
+    private function checkEnv()
227
+    {
228
+        if (false === $this->isCli()) {
229
+            // @codeCoverageIgnoreStart
230
+            throw new ThreadException('Error. It is not safe to use process forking in other way than php-cli');
231
+            // @codeCoverageIgnoreEnd
232
+        }
233
+        if (0 === count($this->threads)) {
234
+            throw new ThreadException('Error. Can\'t run child threads processes without any added in the Pool');
235
+        }
236
+    }
237
+
238
+    /**
239
+     *
240
+     */
241
+    private function initRun()
242
+    {
243
+        $this->resetRun();
244
+    }
245
+
246
+    /**
247
+     * @return bool
248
+     */
249
+    private function isCli(): bool
250
+    {
251
+        return PHP_SAPI === 'cli';
252
+    }
253
+
254
+    /**
255
+     *
256
+     */
257
+    private function startRunStatus()
258
+    {
259
+        if (false === $this->isRunning) {
260
+            $this->notify(PoolEvent::POOL_RUN_START);
261
+            $this->isRunning = true;
262
+        }
263
+    }
264
+
265
+    /**
266
+     *
267
+     */
268
+    private function resetRun()
269
+    {
270
+        if (true === $this->isRunning) {
271
+            $this->notify(PoolEvent::POOL_RUN_STOP);
272
+        }
273
+        $this->isRunning = false;
274
+        $this->toRunThreads = $this->threads;
275
+        $this->runningThreads = [];
276
+    }
277 277
 
278 278
 }
Please login to merge, or discard this patch.