tourze/workerman-process-worker

0.0.3 2025-03-29 10:55 UTC

This package is auto-updated.

Last update: 2025-04-01 07:30:18 UTC


README

Latest Version Total Downloads License

A Workerman extension that allows you to run and monitor external processes within the Workerman environment.

Inspired by https://github.com/workbunny

Features

  • Run any external command as a Workerman process
  • Monitor process output in real-time
  • Handle process exit events
  • Easily integrate with existing Workerman applications
  • Supports reloading

Installation

composer require tourze/workerman-process-worker

Quick Start

<?php

use Tourze\Workerman\ProcessWorker\ProcessWorker;
use Workerman\Worker;

require_once __DIR__ . '/vendor/autoload.php';

// Create a ProcessWorker with a command to run
$processWorker = new ProcessWorker('ping google.com');

// Handle process output
$processWorker->onProcessOutput = function ($output) {
    echo "Process output: $output";
};

// Handle process exit
$processWorker->onProcessExit = function () {
    echo "Process has exited\n";
};

// Start Workerman
Worker::runAll();

Detailed Usage

Creating a ProcessWorker

The ProcessWorker constructor takes a command string that will be executed:

$processWorker = new ProcessWorker('your_command_here');

Event Callbacks

ProcessWorker provides two main callbacks:

  1. onProcessOutput: Called whenever the process outputs data

    $processWorker->onProcessOutput = function ($output) {
        // Handle the output
    };
  2. onProcessExit: Called when the process terminates

    $processWorker->onProcessExit = function () {
        // Handle process exit
    };

Worker Configuration

Since ProcessWorker extends Workerman's Worker class, you can configure it just like any other Worker:

$processWorker->count = 1; // Number of processes to start
$processWorker->name = 'MyProcessWorker'; // Name for the worker

Contributing

Please feel free to submit pull requests or create issues to improve this package.

License

The MIT License (MIT). Please see License File for more information.