gitkv / laravel-gearman-rpc by gitkv

Laravel/Lumen Gearman rpc. Based from https://github.com/mhlavac/gearman
64
8
2
Package Data
Maintainer Username: gitkv
Maintainer Contact: gitkv@ya.ru (Nikolay Volkov)
Package Create Date: 2018-05-03
Package Last Update: 2018-05-07
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-11-15 15:15:11
Package Statistics
Total Downloads: 64
Monthly Downloads: 2
Daily Downloads: 0
Total Stars: 8
Total Watchers: 2
Total Forks: 0
Total Open Issues: 0

Laravel Gearman rpc

Build Status

Laraval / Lumen Gearman Remote Procedure Call

Requires:

  • Laravel / Lumen >=5.5
  • PHP >= 7.1
  • Gearman PHP extension
  • Socket PHP extension

Installation

  • Run:
composer require "gitkv/laravel-gearman-rpc"
  • Install gearman job server as PHP-extension: http://gearman.org/getting-started/#gearman_php_extension
  • Install supervisor:
apt-get install supervisor

Configuration

Laravel:

Add service provider to /config/app.php:

'providers' => [
    gitkv\GearmanRpc\GearmanRpcServiceProvider::class
],
'aliases' => [
    'GearmanRpc' => gitkv\GearmanRpc\Facade\GearmanRpc::class,
],

Publish config/gearman-rpc.php

php artisan vendor:publish --provider="gitkv\GearmanRpc\GearmanRpcServiceProvider" --tag=config

Usage

Worker:

Create handler:

Create a file in the directory app\Rpc\MyRpcHandler.php

<?php

namespace App\Rpc;


use gitkv\GearmanRpc\HandlerContract;

class MyRpcHandler implements HandlerContract {

    public function handle($payload) {
        return [
            'status'  => 'success',
            'payload' => $payload,
        ];
    }

}

Add your handler to the handlers section in the config/gearman-rpc.php file

'MyExampleFunction' => \App\Rpc\MyRpcHandler::class,

Configure supervisor

Example supervisor config

[program:app-rpc-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/app/artisan gearman-rpc
autostart=true
autorestart=true
user = www
numprocs=1
redirect_stderr=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

Client:

Synch call

<?php
$result = GearmanRpc::doNormal('MyExampleFunction', json_encode(['test'=>'data']));

Asynch call

<?php
GearmanRpc::doBackground('MyExampleFunction', json_encode(['test'=>'data']));