Search by

spiral / roadrunner-services

wolfy-j

Manage RoadRunner services from PHP: create, start, stop and inspect processes supervised by the RoadRunner service plugin

Package info

github.com/roadrunner-php/services

Homepage

Issues

Chat

Forum

Documentation

pkg:composer/spiral/roadrunner-services

Fund package maintenance!

roadrunner-server

Statistics

Installs: 117 283

Dependents: 1

Suggesters: 0

Stars: 9


README

RoadRunner

Manage RoadRunner services from PHP

Documentation Sponsor

Psalm Level Type Coverage Codecov Mutation testing badge


This package lets a PHP application create, restart, terminate and inspect RoadRunner services at runtime over RPC.

Get Started

Installation

composer require roadrunner/services

PHP Latest Version on Packagist License Total Downloads

Configuration

Enable the RPC and service plugins in .rr.yaml:

rpc:
  listen: tcp://127.0.0.1:6001

service: {}

Connecting to RoadRunner

Create an instance of 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);

Managing Services

Create a new service

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,
        serviceNameInLogs: true,
        stopTimeout: 5,
    );

    if (!$result) {
        throw new ServiceException('Service creation failed.');
    }
} catch (ServiceException $e) {
    // handle exception
}

Check service status

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
}

Restart service

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
}

Terminate service

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
}

List of all services

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
}