Skip to content

Commit a7d15d5

Browse files
Set error_reporting to E_ALL in the worker script
1 parent 79068fd commit a7d15d5

File tree

4 files changed

+35
-19
lines changed

4 files changed

+35
-19
lines changed

src/Crusse/JobServer/EventLoop.php

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ function __construct( $serverSocketAddr ) {
2727
function __destruct() {
2828

2929
foreach ( $this->sockets as $socket )
30-
$this->closeConnection( $socket );
30+
$this->disconnect( $socket );
3131

3232
// If we were listening on a socket, remove the socket file
3333
if ( $this->serverSocket ) {
34-
$this->closeConnection( $this->serverSocket );
34+
$this->disconnect( $this->serverSocket );
3535
unlink( $this->serverSocketAddr );
3636
}
3737
}
@@ -69,7 +69,7 @@ private function addClientSocket( $socket ) {
6969
// array_unshift when removing sockets, as it's slow, so we use unset()
7070
// instead.
7171
for ( $i = 0; $i < $socketCount; $i++ ) {
72-
if ( !isset( $this->sockets[ $i ] ) ) {
72+
if ( empty( $this->sockets[ $i ] ) ) {
7373
$foundSpot = true;
7474
$this->sockets[ $i ] = $socket;
7575
break;
@@ -137,8 +137,10 @@ function run() {
137137
while ( true ) {
138138

139139
// We have no more sockets to poll, all have disconnected
140-
if ( !$this->sockets && !$this->serverSocket )
140+
if ( !$this->sockets && !$this->serverSocket ) {
141+
$this->log( 'No more sockets to poll, exiting run() loop' );
141142
break;
143+
}
142144

143145
$readables = $this->sockets;
144146
if ( $this->serverSocket )
@@ -173,16 +175,18 @@ function run() {
173175
if ( $writables )
174176
$this->handleWritableSockets( $writables );
175177

176-
if ( $this->stop )
178+
if ( $this->stop ) {
179+
$this->log( 'stop() was called, exiting run() loop' );
177180
break;
181+
}
178182
}
179183

180184
foreach ( $this->sockets as $socket )
181-
$this->closeConnection( $socket );
185+
$this->disconnect( $socket );
182186
$this->sockets = array();
183187

184188
if ( $this->serverSocket )
185-
$this->closeConnection( $this->serverSocket );
189+
$this->disconnect( $this->serverSocket );
186190
}
187191

188192
function stop() {
@@ -205,16 +209,18 @@ private function handleReadableSockets( $sockets ) {
205209
if ( !$messages )
206210
continue;
207211

208-
//$this->log( 'Buffer had '. count( $messages ) .' messages' );
212+
$this->log( 'Buffer had '. count( $messages ) .' messages' );
209213

210214
foreach ( $messages as $message ) {
211215
foreach ( $this->callbacks as $callback ) {
212216
call_user_func( $callback, $message, $this, $socket );
213217
}
214218
}
215219

216-
if ( $this->stop )
220+
if ( $this->stop ) {
221+
$this->log( 'stop() was called, so will not read from other sockets' );
217222
break;
223+
}
218224
}
219225
}
220226

@@ -236,7 +242,7 @@ private function handleWritableSockets( $sockets ) {
236242
throw new \Exception( 'Could not write to socket' );
237243
}
238244

239-
//$this->log( 'Sent '. $sentBytes .' b to '. $socketIndex );
245+
$this->log( 'Sent '. $sentBytes .' b to '. $socketIndex );
240246
$this->writeBuffer[ $socketIndex ] = substr( $buffer, $sentBytes );
241247
}
242248
}
@@ -251,7 +257,7 @@ private function acceptClient() {
251257
}
252258

253259
$this->addClientSocket( $socket );
254-
//$this->log( 'Accepted client '. ( count( $this->sockets ) - 1 ) );
260+
$this->log( 'Accepted client '. ( count( $this->sockets ) - 1 ) );
255261

256262
return $socket;
257263
}
@@ -282,12 +288,12 @@ private function getMessagesFromSocket( $socket ) {
282288
// Connection was dropped by peer
283289
if ( $dataLen === 0 ) {
284290
// Don't read/write from/to this socket in the future
285-
$this->closeConnection( $this->sockets[ $socketIndex ] );
291+
$this->disconnect( $this->sockets[ $socketIndex ] );
286292
unset( $this->sockets[ $socketIndex ] );
287293
return array();
288294
}
289295

290-
//$this->log( 'Recvd '. $dataLen .' b from '. $socketIndex );
296+
$this->log( 'Recvd '. $dataLen .' b from '. $socketIndex );
291297
$this->populateMessageBuffer( $data, $buffer );
292298

293299
// Get finished Message objects from the MessageBuffer
@@ -304,7 +310,7 @@ private function getMessagesFromSocket( $socket ) {
304310
// partially) other messages' data
305311
if ( $overflowBytes > 0 ) {
306312

307-
//$this->log( 'Recvd multiple messages from socket (overflow: '. $overflowBytes .' b)' );
313+
$this->log( 'Recvd multiple messages from socket (overflow: '. $overflowBytes .' b)' );
308314

309315
$overflow = substr( $buffer->message->body, -$overflowBytes );
310316
$buffer->message->body .= substr( $buffer->message->body, 0, -$overflowBytes );
@@ -365,7 +371,7 @@ private function populateMessageBuffer( $data, MessageBuffer &$buffer ) {
365371
}
366372
}
367373

368-
private function closeConnection( $socket ) {
374+
private function disconnect( $socket ) {
369375

370376
// Close the connection until the worker client sends us a new result. We
371377
// silence any errors so that we don't have to test the connection status

src/Crusse/JobServer/worker_process.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
if ( php_sapi_name() !== 'cli' )
44
exit( 1 );
55

6+
error_reporting( E_ALL );
7+
68
require_once dirname( __FILE__ ) .'/Worker.php';
79

810
$worker = new \Crusse\JobServer\Worker( $argv[ 1 ] );

tests/functions.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22

33
function job_test( $message ) {
4+
5+
usleep( rand( 50000, 500000 ) );
6+
47
return strtoupper( $message );
58
}
69

tests/test.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<?php
22

3+
ini_set('display_errors', 1);
4+
ini_set('display_startup_errors', 1);
5+
ini_set('error_log', dirname(__FILE__) .'/php_errors.log');
6+
error_reporting(E_ALL);
7+
38
require_once __DIR__ .'/../vendor/autoload.php';
49

510
function generateString($length) {
@@ -19,10 +24,10 @@ function generateString($length) {
1924

2025
$timeTotal = microtime( true );
2126

22-
$server = new Crusse\JobServer\Server( 4 );
27+
$server = new Crusse\JobServer\Server( 6 );
2328
$server->addWorkerInclude( __DIR__ .'/functions.php' );
2429
$server->setWorkerTimeout( 2 );
25-
for ( $i = 0; $i < 50; $i++ )
30+
for ( $i = 0; $i < 1000; $i++ )
2631
$server->addJob( 'job_test', 'Job '. $i .': '. generateString( 100 * 250 ) );
2732

2833
echo 'Results with callback:'. PHP_EOL . PHP_EOL;
@@ -33,10 +38,10 @@ function generateString($length) {
3338

3439
echo PHP_EOL .'Ordered results:'. PHP_EOL . PHP_EOL;
3540

36-
$server = new Crusse\JobServer\Server( 4 );
41+
$server = new Crusse\JobServer\Server( 6 );
3742
$server->addWorkerInclude( __DIR__ .'/functions.php' );
3843
$server->setWorkerTimeout( 2 );
39-
for ( $i = 0; $i < 50; $i++ )
44+
for ( $i = 0; $i < 1000; $i++ )
4045
$server->addJob( 'job_test', 'Job '. $i .': '. generateString( 100 * 250 ) );
4146

4247
$time = microtime( true );

0 commit comments

Comments
 (0)