demi/php-gearman

Gearman job server workers helper

1.1.5 2018-01-29 18:55 UTC

This package is auto-updated.

Last update: 2024-10-27 21:26:16 UTC


README

Gearman job server workers helper

Installation

  • Run:
composer require "demi/php-gearman" "~1.0"
apt-get install supervisor

Configuration

Yii1/Yii2:

supervisor.php at you common config dir:

return [
    'configFile' => '/etc/supervisor/conf.d/workers.conf',
    'workersDirectory' => realpath(__DIR__ . '/../../'),
    'restartSleepingTime' => 5,
    'all' => [
        'crop_image' => ['numprocs' => 0, 'command' => '/usr/bin/php yii workers/crop-image'],
        'bad_worker' => ['numprocs' => 0, 'command' => '/usr/bin/php yii workers/bad-worker'],
    ],
    'sets' => [
        'general' => [
            'crop_image' => 5,
        ],
        'minimal' => [
            'crop_image' => 50,
            'bad_worker' => 50,
        ],
        'maximal' => [
            'crop_image' => 100,
            'bad_worker' => 100,
        ],
    ],
];

Gearman component configuration

Yii2

/common/config/main.php:

'components' => [
    'gearman' => [
        'class' => '\demi\gearman\yii2\Component',
        'host' => '127.0.0.1',
        'port' => 4730,
        'supervisorConfig' => require(__DIR__ . '/supervisor.php'),
    ],
],

/console/config/main.php:

return [
    'controllerMap' => [
        'gearman' => [
            'class' => '\demi\gearman\yii2\SupervisorController',
            'gearmanComponentName' => 'gearman', // name of component: Yii::$app->gearman (from previous config listing)
        ],
    ],
],

Yii1

/protected/config/main.php:

'components' => [
    'gearman' => [
        'class' => '\demi\gearman\yii1\GearmanComponent',
        'host' => '127.0.0.1',
        'port' => 4730,
        'supervisorConfig' => require(__DIR__ . '/supervisor.php'),
    ],
],

/protected/config/console.php:

$mainConfig = require(dirname(__FILE__) . '/main.php');
return [
    'commandMap' => [
        'gearman' => [
            'class' => '\demi\gearman\yii1\SupervisorCommand',
            'gearmanComponentName' => 'gearman', // name of component: Yii::app()->gearman (from previous config listing)
        ],
    ],
    'components' => [
        'gearman' => $mainConfig['components']['gearman'],
    ],
],

Laravel:

Add service provider to /config/app.php:

'providers' => [
    // Gearman helper
    demi\gearman\laravel5\GearmanServiceProvider::class
],
'aliases' => [
    // Gearman helper
    'Gearman' => demi\gearman\laravel5\GearmanFacade::class,
],

Publish /config/gearman.php

php artisan vendor:publish --provider="demi\gearman\laravel5\GearmanServiceProvider" --tag=config

Usage

Running workers:

Gearman workers - it is simple looped console commands

Yii2:

Create new console controller
/console/controllers/WorkersController.php:

<?php

namespace console\controllers;

use Yii;
use GearmanJob;

/**
 * Gearman workers
 */
class WorkersController extends \yii\console\Controller
{
    /**
     * Crop image worker
     */
    public function actionCropImage()
    {
        Yii::$app->gearman->runWorker('crop_image', function (GearmanJob $job) {
            $workload = Yii::$app->gearman->deserializeWorkload($job->workload());
            $imagePath = $workload['image_path'];
            if (empty($imagePath)) {
                return Yii::$app->gearman->serializeWorkload(['status' => 'error', 'message' => 'No image']);
            }

            // Do some job...

            return Yii::$app->gearman->serializeWorkload(['status' => 'success', 'foo' => 'bar']);
        });
    }
}
Yii1:

Create new console command
/protected/commands/WorkersCommand.php:

<?php

/**
 * Gearman workers
 */
class WorkersCommand extends CConsoleCommand
{
    /**
     * Crop image worker
     */
    public function actionCropImage()
    {
        Yii::app()->gearman->runWorker('crop_image', function (GearmanJob $job) {
            $workload = Yii::app()->gearman->deserializeWorkload($job->workload());
            $imagePath = $workload['image_path'];
            if (empty($imagePath)) {
                return Yii::app()->gearman->serializeWorkload(['status' => 'error', 'message' => 'No image']);
            }

            // Do some job...

            return Yii::app()->gearman->serializeWorkload(['status' => 'success', 'foo' => 'bar']);
        });
    }
}
Laravel:

Create new console command
/app/Console/Commands/CropImage.php:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use GearmanJob;
use Gearman;

/**
 * Gearman crop image worker
 */
class CropImage extends Command
{
    /**
     * @inheritdoc
     */
    protected $name = 'worker:crop-image';

    /**
     * @inheritdoc
     */
    protected $description = 'Worker for cropping image';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        Gearman::runWorker('crop_image', function (GearmanJob $job) {
            $workload = Gearman::deserializeWorkload($job->workload());
            $imagePath = $workload['image_path'];
            if (empty($imagePath)) {
                return Gearman::serializeWorkload(['status' => 'error', 'message' => 'No image']);
            }

            // Do some job...

            return Gearman::serializeWorkload(['status' => 'success', 'foo' => 'bar']);
        });
    }
}

Update /app/Console/Kernel.php:
Add to protected $commands:

protected $commands = [
    // ...
    \App\Console\Commands\CropImage::class,
]

Change supervisor config set and restart supervisor:

Yii2:

php yii gearman

Yii1:

php yiic gearman

Laravel:

php artisan gearman

Examples

Yii2:

At any place:

// synchronous
$result = Yii::$app->gearman->doNormal('crop_image', ['image_path' => '/var/www/image.jpg']);
var_dump(Yii::$app->gearman->deserializeWorkload($result)); // ['status' => 'success', 'foo' => 'bar']
$result = Yii::$app->gearman->doNormal('crop_image');
var_dump(Yii::$app->gearman->deserializeWorkload($result)); // ['status' => 'error', 'message' => 'No image']

// asynchronous
$result = Yii::$app->gearman->doBackground('crop_image', ['image_path' => '/var/www/image.jpg']);
var_dump($result); // job handle file descriptior
$result = Yii::$app->gearman->doBackground('crop_image');
var_dump($result); // job handle file descriptior

// Variants:
// doLow(), doNormal(), doHigh(),
// doLowBackground(), doBackground(), doHighBackground(),
Yii1:

At any place:

// synchronous
$result = Yii::app()->gearman->doNormal('crop_image', ['image_path' => '/var/www/image.jpg']);
var_dump(Yii::app()->gearman->deserializeWorkload($result)); // ['status' => 'success', 'foo' => 'bar']
$result = Yii::app()->gearman->doNormal('crop_image');
var_dump(Yii::app()->gearman->deserializeWorkload($result)); // ['status' => 'error', 'message' => 'No image']

// asynchronous
$result = Yii::app()->gearman->doBackground('crop_image', ['image_path' => '/var/www/image.jpg']);
var_dump($result); // job handle file descriptior
$result = Yii::app()->gearman->doBackground('crop_image');
var_dump($result); // job handle file descriptior

// Variants:
// doLow(), doNormal(), doHigh(),
// doLowBackground(), doBackground(), doHighBackground(),
Laravel:

At any place:

use Gearman;

// synchronous
$result = Gearman::doNormal('crop_image', ['image_path' => '/var/www/image.jpg']);
var_dump(Gearman::deserializeWorkload($result)); // ['status' => 'success', 'foo' => 'bar']
$result = Gearman::doNormal('crop_image');
var_dump(Gearman::deserializeWorkload($result)); // ['status' => 'error', 'message' => 'No image']

// asynchronous
$result = Gearman::doBackground('crop_image', ['image_path' => '/var/www/image.jpg']);
var_dump($result); // job handle file descriptior
$result = Gearman::doBackground('crop_image');
var_dump($result); // job handle file descriptior

// Variants:
// doLow(), doNormal(), doHigh(),
// doLowBackground(), doBackground(), doHighBackground(),