diff --git a/lib/Runner.php b/lib/Runner.php index 2cbe0a6..00ca07c 100644 --- a/lib/Runner.php +++ b/lib/Runner.php @@ -192,8 +192,8 @@ protected function check_workers() { } $pipes = array(); - foreach ( $this->workers as $id => $worker ) { - $pipes[ $id ] = $worker->pipes[1]; + foreach ( $this->workers as $worker ) { + $pipes[] = $worker->pipes[1]; } // Grab all the pipes ready to close @@ -212,8 +212,18 @@ protected function check_workers() { $logger = new Logger( $this->db, $this->table_prefix ); // Clean up all of the finished workers - foreach ( $pipes as $id => $stream ) { - $worker = $this->workers[ $id ]; + foreach ( $pipes as $stream ) { + // Find which worker it was + $matched = array_filter( $this->workers, function ( $worker ) use ( $stream ) { + return $worker->pipes[1] === $stream; + }); + if ( count( $matched ) !== 1 ) { + // This is weird... + printf( '[!!] Could not match pipe to worker!' ); + continue; + } + + $worker = $matched[0]; if ( ! $worker->is_done() ) { // Process hasn't exited yet, keep rocking on continue; @@ -227,6 +237,7 @@ protected function check_workers() { $logger->log_job_completed( $worker->job ); } + $id = array_search( $worker, $this->workers, true ); unset( $this->workers[ $id ] ); } }