Hoa central
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Attributes | List of all members
Hoa\Worker\Backend\Shared Class Reference
Inheritance diagram for Hoa\Worker\Backend\Shared:

Public Member Functions

 __construct ($workerId, $password)
 
 on ($listenerId, $callable)
 
 run ()
 
 stop ()
 

Static Public Member Functions

static start ($socket, $workerPath, Array $fastcgiParameters=[])
 
static pack ($type, $message)
 

Public Attributes

const TYPE_STOP = 0
 
const TYPE_MESSAGE = 1
 
const TYPE_INFORMATIONS = 2
 

Protected Attributes

 $_socket = null
 
 $_wid = null
 
 $_on = null
 
 $_password = null
 
 $_startTime = 0
 
 $_messages = 0
 
 $_lastMessage = 0
 

Detailed Description

Class .

A shared worker is like a daemon, but it builds an internal server that receives messages. According to these messages, an action is performed by the help of the “message” listener. A shared worker behaves like a daemon without the need of fork. It must run behind PHP-FPM because we need the fastcgi_finish_request() function that close the current FastCGI request but not the program execution. Then, the internal server is started and continues to live in a PHP-FPM process. Well, we have a daemon :-). How to use it? Easy. Your program: • __construct; • run. Your “worker starter” (see $ hoa worker:start): • start. Your “worker stopper” (see $ hoa worker:stop): • __construct; • stop. To construct the worker, we need a socketable object for the internal server. To start the worker, we need a socketable object to the PHP-FPM server. When the shared worker is stopped, the associated .wid file (if exists) is removed.

Definition at line 73 of file Shared.php.

Constructor & Destructor Documentation

Hoa\Worker\Backend\Shared::__construct (   $workerId,
  $password 
)

Construct a worker.

Parameters
mixed$workerIdWorker ID or a socket client (i.e. a object).
string$passwordWorker's password.
Returns
void
Exceptions

Definition at line 157 of file Shared.php.

158  {
159  if (!is_string($workerId) &&
160  !($workerId instanceof Socket\Client)) {
161  throw new Exception(
162  'Either you give a worker ID or you give an object of type ' .
163  '\Hoa\Socket\Client, but not anything else; given %s',
164  0,
165  is_object($workerId)
166  ? get_class($workerId)
167  : $workerId
168  );
169  }
170 
171  if (is_string($workerId)) {
172  $this->_wid = $workerId;
173  $handle = Worker\Run::get($workerId);
174  $workerId = $handle['socket'];
175  }
176 
177  set_time_limit(0);
178 
179  $this->_socket = $workerId;
180  $this->_on = new Core\Event\Listener($this, ['message']);
181  $this->_password = sha1($password);
182  $this->_startTime = microtime(true);
183 
184  return;
185  }
static get($workerId)
Definition: Run.php:101

Here is the call graph for this function:

Member Function Documentation

Hoa\Worker\Backend\Shared::on (   $listenerId,
  $callable 
)

Attach a callable to this listenable object.

Parameters
string$listenerIdListener ID.
mixed$callableCallable.
Returns
Exceptions

Implements Hoa\Core\Event\Listenable.

Definition at line 195 of file Shared.php.

196  {
197  $this->_on->attach($listenerId, $callable);
198 
199  return $this;
200  }
static Hoa\Worker\Backend\Shared::pack (   $type,
  $message 
)
static

Pack a message.

Parameters
int$typePlease, see self::TYPE_* constants.
mixed$messageWhatever you want.
Returns
string

Definition at line 344 of file Shared.php.

345  {
346  $message = serialize($message);
347 
348  return
349  pack('n', $type) .
350  pack('N', strlen($message)) .
351  $message .
352  pack('C', 0);
353  }
static pack($type, $message)
Definition: Shared.php:344

Here is the caller graph for this function:

Hoa\Worker\Backend\Shared::run ( )

Run the shared worker. It creates a zombie with .

Returns
void
Exceptions

Definition at line 209 of file Shared.php.

210  {
211  $server = new Socket\Server($this->_socket);
212  $server->connectAndWait();
213 
214  Zombie::fork();
215 
216  $_eom = pack('C', 0);
217 
218  while (true) {
219  foreach ($server->select() as $node) {
220  $request = unpack('nr', $server->read(2));
221  $length = unpack('Nl', $server->read(4));
222  $message = unserialize($server->read($length['l']));
223  $eom = unpack('Ce', $server->read(1));
224 
225  if ($eom['e'] != $_eom) {
226  $server->disconnect();
227 
228  continue;
229  }
230 
231  switch ($request['r']) {
232  case static::TYPE_MESSAGE:
233  $this->_on->fire(
234  'message',
235  new Core\Event\Bucket([
236  'message' => $message
237  ])
238  );
240  $this->_lastMessage = time();
241 
242  break;
243 
244  case static::TYPE_STOP:
245  if ($this->_password === $message) {
246  $server->disconnect();
247 
248  break 3;
249  }
250 
251  break;
252 
253  case static::TYPE_INFORMATIONS:
254  $message = [
255  'id' => $this->_wid,
256  'socket' => $this->_socket,
257  'start' => $this->_startTime,
258  'pid' => getmypid(),
259  'memory' => memory_get_usage(true),
260  'memory_allocated' => memory_get_usage(),
261  'memory_peak' => memory_get_peak_usage(true),
262  'memory_allocated_peak' => memory_get_usage(),
263  'messages' => $this->_messages,
264  'last_message' => $this->_lastMessage,
265  'filename' => $_SERVER['SCRIPT_FILENAME']
266  ];
267  $server->writeAll(
268  static::pack(static::TYPE_MESSAGE, $message)
269  );
270 
271  break;
272  }
273 
274  $server->disconnect();
275  }
276  }
277 
278  $server->disconnect();
279 
280  if (null !== $this->_wid) {
281  Worker\Run::unregister($this->_wid);
282  }
283 
284  return;
285  }
static unregister($workerId)
Definition: Run.php:85
static pack($type, $message)
Definition: Shared.php:344
static fork()
Definition: Zombie.php:66

Here is the call graph for this function:

static Hoa\Worker\Backend\Shared::start (   $socket,
  $workerPath,
Array  $fastcgiParameters = [] 
)
static

Start the shared worker.

Parameters
string$socketSocket URI to PHP-FPM server.
string$workerPathPath to the shared worker program.
array$fastcgiParametersAdditional parameters for FastCGI.
Returns
bool

Definition at line 295 of file Shared.php.

296  {
297  $server = new Fastcgi\Responder(
298  new Socket\Client($socket)
299  );
300 
301  $headers = [
302  'GATEWAY_INTERFACE' => 'FastCGI/1.0',
303  'SERVER_PROTOCOL' => 'HTTP/1.1',
304  'REQUEST_URI' => $workerPath,
305  'SCRIPT_FILENAME' => $workerPath,
306  'SCRIPT_NAME' => DS . dirname($workerPath)
307  ];
308 
309  $defaultFastcgiParameters = [
310  'REQUEST_METHOD' => 'GET'
311  ];
312 
313  return $server->send(
314  array_merge(
315  $defaultFastcgiParameters,
316  $fastcgiParameters,
317  $headers
318  )
319  );
320  }

Here is the caller graph for this function:

Hoa\Worker\Backend\Shared::stop ( )

Stop the shared worker.

Returns
bool

Definition at line 327 of file Shared.php.

328  {
329  $client = new Socket\Client($this->_socket);
330  $client->connect();
331  $client->writeAll(static::pack(static::TYPE_STOP, $this->_password));
332  $client->disconnect();
333 
334  return true;
335  }

Member Data Documentation

Hoa\Worker\Backend\Shared::$_lastMessage = 0
protected

Definition at line 143 of file Shared.php.

Hoa\Worker\Backend\Shared::$_messages = 0
protected

Definition at line 136 of file Shared.php.

Hoa\Worker\Backend\Shared::$_on = null
protected

Definition at line 115 of file Shared.php.

Hoa\Worker\Backend\Shared::$_password = null
protected

Definition at line 122 of file Shared.php.

Hoa\Worker\Backend\Shared::$_socket = null
protected

Definition at line 101 of file Shared.php.

Hoa\Worker\Backend\Shared::$_startTime = 0
protected

Definition at line 129 of file Shared.php.

Hoa\Worker\Backend\Shared::$_wid = null
protected

Definition at line 108 of file Shared.php.

const Hoa\Worker\Backend\Shared::TYPE_INFORMATIONS = 2

Message type: informations.

int

Definition at line 94 of file Shared.php.

const Hoa\Worker\Backend\Shared::TYPE_MESSAGE = 1

Message type: message (normal).

int

Definition at line 87 of file Shared.php.

const Hoa\Worker\Backend\Shared::TYPE_STOP = 0

Message type: stop.

int

Definition at line 80 of file Shared.php.


The documentation for this class was generated from the following file: