PHP code example of tourze / workerman-process-worker
1. Go to this page and download the library: Download tourze/workerman-process-worker library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tourze / workerman-process-worker example snippets
use Tourze\Workerman\ProcessWorker\ProcessWorker;
use Workerman\Worker;
rker('ping -c 3 google.com');
// Handle process output
$processWorker->onProcessOutput = function ($worker, $output) {
echo "Process output: $output";
};
// Handle process exit
$processWorker->onProcessExit = function ($worker) {
echo "Process has exited\n";
};
// Start Workerman
Worker::runAll();
use Tourze\Workerman\ProcessWorker\ProcessWorker;
use Tourze\Workerman\ProcessWorker\Event\ProcessStartEvent;
use Tourze\Workerman\ProcessWorker\Event\ProcessOutputEvent;
use Tourze\Workerman\ProcessWorker\Event\ProcessExitEvent;
use Workerman\Worker;
$processWorker->addListener(ProcessOutputEvent::NAME, function (ProcessOutputEvent $event) {
echo "Output: " . $event->getOutput();
});
$processWorker->addListener(ProcessExitEvent::NAME, function (ProcessExitEvent $event) {
echo "Process exited\n";
});
Worker::runAll();
public function __construct(
string $runCommand,
?ProcessHandlerInterface $processHandler = null,
?EventDispatcherInterface $eventDispatcher = null
)
$processWorker->onProcessStart = function ($worker) {
echo "Process started\n";
};
$processWorker->onProcessOutput = function ($worker, $output) {
echo "Output: $output";
};
$processWorker->onProcessExit = function ($worker) {
echo "Process exited\n";
};
use Tourze\Workerman\ProcessWorker\Event\ProcessStartEvent;
use Tourze\Workerman\ProcessWorker\Event\ProcessOutputEvent;
use Tourze\Workerman\ProcessWorker\Event\ProcessExitEvent;
// Add multiple listeners with different priorities
$processWorker->addListener(ProcessOutputEvent::NAME, function (ProcessOutputEvent $event) {
file_put_contents('process.log', $event->getOutput(), FILE_APPEND);
}, 100); // High priority
$processWorker->addListener(ProcessOutputEvent::NAME, function (ProcessOutputEvent $event) {
echo "Output: " . $event->getOutput();
}, 0); // Default priority
use Tourze\Workerman\ProcessWorker\Contract\ProcessHandlerInterface;
class MyProcessHandler implements ProcessHandlerInterface
{
public function __construct(private string $command) {}
public function start(): mixed
{
// Custom process starting logic
return popen($this->command, 'r');
}
public function stop(mixed $process): void
{
// Custom cleanup logic
if (is_resource($process)) {
fclose($process);
}
}
public function isRunning(mixed $process): bool
{
// Custom running check
return is_resource($process) && !feof($process);
}
public function getCommand(): string
{
return $this->command;
}
}
// Use custom handler
$processWorker = new ProcessWorker('my_command', new MyProcessHandler('my_command'));
$processWorker->count = 1; // Number of processes to start
$processWorker->name = 'MyProcessWorker'; // Name for the worker
$processWorker->reloadable = true; // Enable reload support (default: true)
use Tourze\Workerman\ProcessWorker\ProcessWorker;
use Tourze\Workerman\ProcessWorker\Event\ProcessStartEvent;
use Tourze\Workerman\ProcessWorker\Event\ProcessOutputEvent;
use Tourze\Workerman\ProcessWorker\Event\ProcessExitEvent;
use Workerman\Worker;
event) {
echo "[" . date('Y-m-d H:i:s') . "] Log monitoring started\n";
});
// Process log entries
$logMonitor->addListener(ProcessOutputEvent::NAME, function (ProcessOutputEvent $event) {
$line = trim($event->getOutput());
if (empty($line)) {
return;
}
// Parse log line and handle according to your needs
if (strpos($line, 'ERROR') !== false) {
// Handle error logs
echo "[ERROR] $line\n";
} else {
// Handle regular logs
echo "[INFO] $line\n";
}
});
// Handle process exit
$logMonitor->addListener(ProcessExitEvent::NAME, function (ProcessExitEvent $event) {
echo "[" . date('Y-m-d H:i:s') . "] Log monitoring stopped\n";
});
Worker::runAll();
public function __construct(
string $runCommand,
?ProcessHandlerInterface $processHandler = null,
?EventDispatcherInterface $eventDispatcher = null
)
public function start(): mixed; // Start the process
public function stop(mixed $process): void; // Stop the process
public function isRunning(mixed $process): bool; // Check if running
public function getCommand(): string; // Get the command
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.