protected function onWorkerStart($socket, $parentPid)
    {
        declare(ticks = 1);
        // set signal handler
        pcntl_signal(SIGTERM, [$this, 'onSigTerm']);

        $this->toParent = $socket;
        $this->parentPid = $parentPid;
        socket_set_nonblock($this->toParent);
        $i = 0;
        while (!$this->termAccepted) {
            // echo ($i++).' try'.PHP_EOL;
            // sleep for sometime
            if (strlen($msg_size_bytes = socket_read($this->toParent, 4)) != 4) {
                usleep($this->checkMicroTime);
                continue;
            }
            $data = unpack('N', $msg_size_bytes);
            $msg_size = $data[1];

            $msg = socket_read($this->toParent, $msg_size);
            $data = unserialize($msg);

            // echo '[Payload] Msg('.$msg_size.'): '.$msg.PHP_EOL;

            // launch worker to do it's work
            $result = call_user_func([$this, 'onPayload'], $data);
            $data = serialize($result);

            // write result to socket
            socket_write($this->toParent, pack('N', strlen($data)));
            // echo '[Finish] Msg('.strlen($data).'): '.$data.PHP_EOL;
            socket_write($this->toParent, $data);

            // send SIGUSR1 to parent to indicate that's we've finished
            if ($this->useSignals)
                posix_kill($this->parentPid, SIGUSR1);
        }
        socket_close($this->toParent);
        // notify we ended
        if ($this->useSignals)
            posix_kill($this->parentPid, SIGUSR2);
    }
protected function onWorkerStart($socket, $parentPid)
    {
        declare(ticks = 1);
        // set signal handler
        pcntl_signal(SIGTERM, [$this, 'onSigTerm']);

        $this->toParent = $socket;
        $this->parentPid = $parentPid;
        socket_set_nonblock($this->toParent);
        $i = 0;
        while (!$this->termAccepted) {
            // echo ($i++).' try'.PHP_EOL;
            // sleep for sometime
            if (strlen($msg_size_bytes = socket_read($this->toParent, 4)) != 4) {
                usleep($this->checkMicroTime);
                continue;
            }
            $data = unpack('N', $msg_size_bytes);
            $msg_size = $data[1];

            $msg = socket_read($this->toParent, $msg_size);
            $data = unserialize($msg);

            // echo '[Payload] Msg('.$msg_size.'): '.$msg.PHP_EOL;

            // launch worker to do it's work
            $result = call_user_func([$this, 'onPayload'], $data);
            $data = serialize($result);

            // write result to socket
            socket_write($this->toParent, pack('N', strlen($data)));
            // echo '[Finish] Msg('.strlen($data).'): '.$data.PHP_EOL;
            socket_write($this->toParent, $data);

            // send SIGUSR1 to parent to indicate that's we've finished
            if ($this->useSignals)
                posix_kill($this->parentPid, SIGUSR1);
        }
        socket_close($this->toParent);
        // notify we ended
        if ($this->useSignals)
            posix_kill($this->parentPid, SIGUSR2);
    }
protected function onWorkerStart ( $ socket , $ parentPid ) { declare ( ticks = 1 ) ; // set signal handler pcntl_signal ( SIGTERM , [ $ this , 'onSigTerm' ] ) ; $ this -> toParent = $ socket ; $ this -> parentPid = $ parentPid ; socket_set_nonblock ( $ this -> toParent ) ; $ i = 0 ; while ( ! $ this -> termAccepted ) { // echo ($i++).' try'.PHP_EOL; // sleep for sometime if ( strlen ( $ msg_size_bytes = socket_read ( $ this -> toParent , 4 ) ) != 4 ) { usleep ( $ this -> checkMicroTime ) ; continue ; } $ data = unpack ( 'N' , $ msg_size_bytes ) ; $ msg_size = $ data [ 1 ] ; $ msg = socket_read ( $ this -> toParent , $ msg_size ) ; $ data = unserialize ( $ msg ) ; // echo '[Payload] Msg('.$msg_size.'): '.$msg.PHP_EOL; // launch worker to do it's work $ result = call_user_func ( [ $ this , 'onPayload' ] , $ data ) ; $ data = serialize ( $ result ) ; // write result to socket socket_write ( $ this -> toParent , pack ( 'N' , strlen ( $ data ) ) ) ; // echo '[Finish] Msg('.strlen($data).'): '.$data.PHP_EOL; socket_write ( $ this -> toParent , $ data ) ; // send SIGUSR1 to parent to indicate that's we've finished if ( $ this -> useSignals ) posix_kill ( $ this -> parentPid , SIGUSR1 ) ; } socket_close ( $ this -> toParent ) ; // notify we ended if ( $ this -> useSignals ) posix_kill ( $ this -> parentPid , SIGUSR2 ) ; }
The first method that is being called when the worker starts.
@param resource $socket Socket to communicate with master process and transport payload / result.
@param integer $parentPid The process ID of the parent
The first method that is being called when the worker starts .