derywat/php-processes

PHP process management library

Installs: 4

Dependents: 1

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/derywat/php-processes

0.1.2 2025-12-12 16:10 UTC

This package is auto-updated.

Last update: 2025-12-12 16:11:46 UTC


README

Library allows to fork existing php process and provide interprocess messaging.

Using ForkManager

Forking new process

use derywat\processes\ForkManager;

$pid = ForkManager::run('uniqueProcessNameOrId',function($socket){
	//new process code...
});

Sending exit signal to child process

Internal implementation: SIGTERM signal will be send to child process. Signal will be handled automatically by ForkManager.

use derywat\processes\ForkManager;

ForkManager::stop('uniqueProcessNameOrId');

Reacting to exit signal in child process

Child process can periodically check if exit was requested.

use derywat\processes\ForkManager;

$pid = ForkManager::run('uniqueProcessNameOrId',function($socket){
	//new process code...

	if(ForkManager::childShouldExit()){
		//custom pre-exit code
		exit();
	}

});

Interprocess messaging

Sending message

Any serializable (by using php internal serialize) data can be passed in message.
Messages can be sent both ways - parent -> child and child->parent.

use derywat\processes\ForkManager;

ForkManager::sendMessage($socket,$data);

Receiving messages

Queued messages can be read periodically by using processMessages method.
Messages array contains objects of type InterprocessMessageInterface. Messages array is ordered by the time of sending message - oldest messages first.

Process finished message is sent on forked child process end.

	$messages = $ForkManager::processMessages();
	if(!empty($messages)){
		foreach($messages as $message){
			//check if process finished
			if($message->isProcessFinished()){
				//this message was sent on process finish
				//handle finished processes here
			} else {
				//message sent by process running at the time of sending message
				$processNameOrId = $message->getName();
				$processPid = $message->getPid();
				$messageData = $message->getData();
				//handle message and its data here
			}
		}
	}

Interprocess commands

Important

added in version 0.1.1

Interprocess commands handling is higher level abstraction for implementing command dispatching and handling in application.

Defining custom application commands

Define custom command as class implementing CommandMessageInterface.
__tostring method must be implemented, but it may return any string.

Example class of log command.

use derywat\processes\commandMessages\CommandMessageInterface;

class CommandMessageLog implements CommandMessageInterface {

	protected $level;
	protected $message;

	public function __construct($level, $message){
		$this->level = $level;
		$this->message = $message;
	}

	public function getLevel() {
		return $this->level;
	}

	public function getMessage() {
		return $this->message;
	}

	//mandatory method
	public function __tostring(){
		$message = $this->getMessage();
		return "command message - log: {$message}";
	}

}

Using CommandMessagesDispatcher

CommandMessagesDispatcher provide single point of commands or commands groups definitions in app.
Implement own dispatcher by extending CommandMessagesDispatcher class.

use derywat\processes\commandMessages\CommandMessagesDispatcher;

class MyCommandMessagesDispatcher extends CommandMessagesDispatcher {

	public static function log($socket, $level, $logMessage){
		static::send($socket,new CommandMessageLog($level,$logMessage));
	}

}

Dispatch command with dispatcher in process code. Provide socket for sending message.

MyCommandMessageDispatcher::log($socket,'info','log message');

Using CommandMessagesHandler in application

CommandMessagesHandler handles commands in application.
Register command class by using class name and providing handling closure.

use derywat\processes\commandMessages\CommandMessagesHandler;

$logger = new MyLoggingClass();

$commandMessagesHandler = (new CommandMessagesHandler())
	->registerCommand(
		CommandMessageLog::class,
		function(CommandMessageLog $command) use ($logger):void {
			$level = $command->getLevel();
			$logMessage = $command->getMessage();
			//call logging code here...
			$logger->log($level,$logMessage);
		}
	);

Access original interprocess message in handler

Important

added in version 0.1.2

Optional second parameter of type InterprocessMessageInterface may be added to command handling closure for accessing interprocess message data.

use derywat\processes\commandMessages\CommandMessagesHandler;

$logger = new MyLoggingClass();

$commandMessagesHandler = (new CommandMessagesHandler())
	->registerCommand(
		CommandMessageLog::class,
		function(CommandMessageLog $command, InterprocessMessageInterface $message) use ($logger):void {
			$level = $command->getLevel();
			$logMessage = $command->getMessage();

			//use message to pass sending process name as log source
			$logSource = $message->getName();

			//call logging code here...
			$logger->log($level,$logMessage,$logSource);
		}
	);