spiral/roadrunner-services

RoadRunner服务管理器

v2.2.0 2024-04-11 18:04 UTC

This package is auto-updated.

Last update: 2024-09-11 18:56:11 UTC


README

Roadrunner服务管理器

PHP Version Require Latest Stable Version phpunit psalm Total Downloads

此包将帮助您管理Roadrunner服务

需求

请确保您的服务器已配置以下PHP版本和扩展

  • PHP 8.1+

安装

您可以通过composer安装此包

composer require spiral/roadrunner-services

用法

这样的配置运行起来相当可行

rpc:
  listen: tcp://127.0.0.1:6001

service: {}

然后您需要创建一个Spiral\RoadRunner\Services\Manager实例

use Spiral\RoadRunner\Services\Manager;
use Spiral\Goridge\RPC\RPC;

$rpc = RPC::create('tcp://127.0.0.1:6001'));
$manager = new Manager($rpc);

创建新服务

use Spiral\RoadRunner\Services\Exception\ServiceException;

try {
    $result = $manager->create(
        name: 'listen-jobs', 
        command: 'php app.php queue:listen',
        processNum: 3,
        execTimeout: 0,
        remainAfterExit: false,
        env: ['APP_ENV' => 'production'],
        restartSec: 30
    );
    
    if (!$result) {
        throw new ServiceException('Service creation failed.');
    }
} catch (ServiceException $e) {
    // handle exception
}

检查服务状态

use Spiral\RoadRunner\Services\Exception\ServiceException;

try {
    $status = $manager->statuses(name: 'listen-jobs');
    
    // Will return an array with statuses of every run process
    // [
    //    [
    //      'cpu_percent' => 59.5,
    //      'pid' => 33,
    //      'memory_usage' => 200,
    //      'command' => 'foo/bar',
    //      'error' => null
    //    ],
    //    [
    //      'cpu_percent' => 60.2,
    //      'pid' => 34,
    //      'memory_usage' => 189,
    //      'command' => 'foo/bar'
    //      'error' => [
    //          'code' => 1,
    //          'message' => 'Process exited with code 1'
    //          'details' => [...] // array with details
    //      ]
    //    ],
    // ] 
} catch (ServiceException $e) {
    // handle exception
}

重启服务

use Spiral\RoadRunner\Services\Exception\ServiceException;

try {
    $result = $manager->restart(name: 'listen-jobs');
    
    if (!$result) {
        throw new ServiceException('Service restart failed.');
    }
} catch (ServiceException $e) {
    // handle exception
}

终止服务

use Spiral\RoadRunner\Services\Exception\ServiceException;

try {
    $result = $manager->terminate(name: 'listen-jobs');
    
    if (!$result) {
        throw new ServiceException('Service termination failed.');
    }
} catch (ServiceException $e) {
    // handle exception
}

所有服务的列表

use Spiral\RoadRunner\Services\Exception\ServiceException;

try {
    $services = $manager->list();
    
    // Will return an array with services names
    // ['listen-jobs', 'websocket-connection'] 
} catch (ServiceException $e) {
    // handle exception
}