PHP Class PHPPM\ProcessManager

Show file Open project: php-pm/php-pm Class Usage Examples

Protected Properties

Property Type Description
$appBootstrap string
$appenv string | null
$bridge string
$concurrentRequestsPerWorker boolean This can lead to issues when the application does not support it (like when they operate on globals at the same time)
$controller PHPPM\React\Server
$controllerHost string
$debug boolean
$emergencyMode boolean Whether we are in the emergency mode or not. True means we need to close all workers due a fatal error and waiting for file changes to be able to restart workers.
$filesLastMTime
$filesLastMd5
$filesToTrack
$handledRequests integer Counter of handled clients.
$host string
$inReload boolean Whether the server is in the reload phase.
$inShutdown boolean
$index integer
$isRunning boolean Whether the server is up and thus creates new slaves when they die or not.
$lastWorkerErrorPrintBy null | integer
$logging boolean
$loop React\EventLoop\LibEventLoop | React\EventLoop\StreamSelectLoop
$maxRequests integer How many requests each worker is allowed to handle until it will be restarted.
$output Symfony\Component\Console\Output\OutputInterface
$phpCgiExecutable string Full path to the php-cgi executable. If not set, we try to determine the path automatically.
$port integer
$ports string[] $object_hash => port
$servingStatic boolean
$slaveCount integer
$slaves array
$tcpConnector React\SocketClient\TcpConnector
$timeout integer Timeout in seconds for master to worker connection.
$waitForSlaves boolean
$web React\Socket\Server

Public Methods

Method Description
__construct ( Symfony\Component\Console\Output\OutputInterface $output, integer $port = 8080, string $host = '127.0.0.1', integer $slaveCount = 8 ) ProcessManager constructor.
getAppBootstrap ( ) : string
getAppEnv ( ) : string
getBridge ( ) : string
handleSigchld ( ) Handling zombie processes on SIGCHLD
isDebug ( ) : boolean
isLogging ( ) : boolean
isServingStatic ( ) : boolean
onSlaveConnection ( Connection $conn ) Handles data communication from slave -> master
onWeb ( Connection $incoming ) Handles incoming connections from $this->port. Basically redirects to a slave.
restartWorker ( ) Closes all slaves, so we automatically reconnect. Necessary when watched files have changed.
run ( ) Starts the main loop. Blocks.
setAppBootstrap ( string $appBootstrap )
setAppEnv ( string | null $appenv )
setBridge ( string $bridge )
setConcurrentRequestsPerWorker ( boolean $concurrentRequestsPerWorker )
setDebug ( boolean $debug )
setLogging ( boolean $logging )
setMaxRequests ( integer $maxRequests )
setPhpCgiExecutable ( string $phpCgiExecutable )
setServingStatic ( boolean $servingStatic )
shutdown ( ) Handles termination signals, so we can gracefully stop all servers.

Protected Methods

Method Description
bootstrapFailed ( Connection $conn ) Handles failed application bootstraps.
checkChangedFiles ( boolean $restartWorkers = true ) : boolean Checks if tracked files have changed. If so, restart all slaves.
commandFiles ( array $data, Connection $conn )
commandLog ( array $data, Connection $conn ) Prints logs.
commandReady ( array $data, Connection $conn ) A slave sent a ready commands which basically says that the slave bootstrapped successfully the application and is ready to accept connections.
commandRegister ( array $data, Connection $conn ) A slave sent a register command.
commandStatus ( array $data, Connection $conn ) A slave sent a status command.
getNextSlave ( $cb ) : integer Returns the next free slave. This method is async, so be aware of async calls between this call.
getPort ( Connection $conn ) : null | integer
isConnectionRegistered ( Connection $conn ) : boolean Whether the given connection is registered.
isHeaderEnd ( string $buffer ) : boolean Checks whether the end of the header is in $buffer.
newInstance ( integer $port ) Creates a new ProcessSlave instance.
replaceHeader ( string $header, string[] $headersToReplace ) : string Replaces or injects header

Method Details

__construct() public method

ProcessManager constructor.
public __construct ( Symfony\Component\Console\Output\OutputInterface $output, integer $port = 8080, string $host = '127.0.0.1', integer $slaveCount = 8 )
$output Symfony\Component\Console\Output\OutputInterface
$port integer
$host string
$slaveCount integer

bootstrapFailed() protected method

Handles failed application bootstraps.
protected bootstrapFailed ( Connection $conn )
$conn React\Socket\Connection

checkChangedFiles() protected method

This approach uses simple filemtime to check against modifications. It is using this technique because all other file watching stuff have either big dependencies or do not work under all platforms without installing a pecl extension. Also this way is interestingly fast and is only used when debug=true.
protected checkChangedFiles ( boolean $restartWorkers = true ) : boolean
$restartWorkers boolean
return boolean

commandFiles() protected method

protected commandFiles ( array $data, Connection $conn )
$data array
$conn React\Socket\Connection

commandLog() protected method

Prints logs.
protected commandLog ( array $data, Connection $conn )
$data array
$conn React\Socket\Connection

commandReady() protected method

A slave sent a ready commands which basically says that the slave bootstrapped successfully the application and is ready to accept connections.
protected commandReady ( array $data, Connection $conn )
$data array
$conn React\Socket\Connection

commandRegister() protected method

A slave sent a register command.
protected commandRegister ( array $data, Connection $conn )
$data array
$conn React\Socket\Connection

commandStatus() protected method

A slave sent a status command.
protected commandStatus ( array $data, Connection $conn )
$data array
$conn React\Socket\Connection

getAppBootstrap() public method

public getAppBootstrap ( ) : string
return string

getAppEnv() public method

public getAppEnv ( ) : string
return string

getBridge() public method

public getBridge ( ) : string
return string

getNextSlave() protected method

Returns the next free slave. This method is async, so be aware of async calls between this call.
protected getNextSlave ( $cb ) : integer
return integer

getPort() protected method

protected getPort ( Connection $conn ) : null | integer
$conn React\Socket\Connection
return null | integer

handleSigchld() public method

Handling zombie processes on SIGCHLD
public handleSigchld ( )

isConnectionRegistered() protected method

Whether the given connection is registered.
protected isConnectionRegistered ( Connection $conn ) : boolean
$conn React\Socket\Connection
return boolean

isDebug() public method

public isDebug ( ) : boolean
return boolean

isHeaderEnd() protected method

Checks whether the end of the header is in $buffer.
protected isHeaderEnd ( string $buffer ) : boolean
$buffer string
return boolean

isLogging() public method

public isLogging ( ) : boolean
return boolean

isServingStatic() public method

public isServingStatic ( ) : boolean
return boolean

newInstance() protected method

Creates a new ProcessSlave instance.
protected newInstance ( integer $port )
$port integer

onSlaveConnection() public method

Handles data communication from slave -> master
public onSlaveConnection ( Connection $conn )
$conn React\Socket\Connection

onWeb() public method

Handles incoming connections from $this->port. Basically redirects to a slave.
public onWeb ( Connection $incoming )
$incoming React\Socket\Connection incoming connection from react

replaceHeader() protected method

Replaces or injects header
protected replaceHeader ( string $header, string[] $headersToReplace ) : string
$header string
$headersToReplace string[]
return string

restartWorker() public method

Closes all slaves, so we automatically reconnect. Necessary when watched files have changed.
public restartWorker ( )

run() public method

Starts the main loop. Blocks.
public run ( )

setAppBootstrap() public method

public setAppBootstrap ( string $appBootstrap )
$appBootstrap string

setAppEnv() public method

public setAppEnv ( string | null $appenv )
$appenv string | null

setBridge() public method

public setBridge ( string $bridge )
$bridge string

setConcurrentRequestsPerWorker() public method

public setConcurrentRequestsPerWorker ( boolean $concurrentRequestsPerWorker )
$concurrentRequestsPerWorker boolean

setDebug() public method

public setDebug ( boolean $debug )
$debug boolean

setLogging() public method

public setLogging ( boolean $logging )
$logging boolean

setMaxRequests() public method

public setMaxRequests ( integer $maxRequests )
$maxRequests integer

setPhpCgiExecutable() public method

public setPhpCgiExecutable ( string $phpCgiExecutable )
$phpCgiExecutable string

setServingStatic() public method

public setServingStatic ( boolean $servingStatic )
$servingStatic boolean

shutdown() public method

Handles termination signals, so we can gracefully stop all servers.
public shutdown ( )

Property Details

$appBootstrap protected property

protected string $appBootstrap
return string

$appenv protected property

protected string|null $appenv
return string | null

$bridge protected property

protected string $bridge
return string

$concurrentRequestsPerWorker protected property

This can lead to issues when the application does not support it (like when they operate on globals at the same time)
protected bool $concurrentRequestsPerWorker
return boolean

$controller protected property

protected Server,PHPPM\React $controller
return PHPPM\React\Server

$controllerHost protected property

protected string $controllerHost
return string

$debug protected property

protected bool $debug
return boolean

$emergencyMode protected property

Whether we are in the emergency mode or not. True means we need to close all workers due a fatal error and waiting for file changes to be able to restart workers.
protected bool $emergencyMode
return boolean

$filesLastMTime protected property

protected $filesLastMTime

$filesLastMd5 protected property

protected $filesLastMd5

$filesToTrack protected property

protected $filesToTrack

$handledRequests protected property

Counter of handled clients.
protected int $handledRequests
return integer

$host protected property

protected string $host
return string

$inReload protected property

Whether the server is in the reload phase.
protected bool $inReload
return boolean

$inShutdown protected property

protected bool $inShutdown
return boolean

$index protected property

protected int $index
return integer

$isRunning protected property

Whether the server is up and thus creates new slaves when they die or not.
protected bool $isRunning
return boolean

$lastWorkerErrorPrintBy protected property

protected null|int $lastWorkerErrorPrintBy
return null | integer

$logging protected property

protected bool $logging
return boolean

$loop protected property

protected LibEventLoop,React\EventLoop|StreamSelectLoop,React\EventLoop $loop
return React\EventLoop\LibEventLoop | React\EventLoop\StreamSelectLoop

$maxRequests protected property

How many requests each worker is allowed to handle until it will be restarted.
protected int $maxRequests
return integer

$output protected property

protected OutputInterface,Symfony\Component\Console\Output $output
return Symfony\Component\Console\Output\OutputInterface

$phpCgiExecutable protected property

Full path to the php-cgi executable. If not set, we try to determine the path automatically.
protected string $phpCgiExecutable
return string

$port protected property

protected int $port
return integer

$ports protected property

$object_hash => port
protected string[] $ports
return string[]

$servingStatic protected property

protected bool $servingStatic
return boolean

$slaveCount protected property

protected int $slaveCount
return integer

$slaves protected property

protected array $slaves
return array

$tcpConnector protected property

protected TcpConnector,React\SocketClient $tcpConnector
return React\SocketClient\TcpConnector

$timeout protected property

Timeout in seconds for master to worker connection.
protected int $timeout
return integer

$waitForSlaves protected property

protected bool $waitForSlaves
return boolean

$web protected property

protected Server,React\Socket $web
return React\Socket\Server