PHP code example of contrictor / php-gearman

1. Go to this page and download the library: Download contrictor/php-gearman library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

contrictor / php-gearman example snippets


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,
        ],
    ],
];

'components' => [
    'gearman' => [
        'class' => '\contrictor\gearman\yii2\Component',
        'host' => '127.0.0.1',
        'port' => 4730,
        'supervisorConfig' => 

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

'components' => [
    'gearman' => [
        'class' => '\contrictor\gearman\yii1\GearmanComponent',
        'host' => '127.0.0.1',
        'port' => 4730,
        'supervisorConfig' => 

$mainConfig = 'commandMap' => [
        'gearman' => [
            'class' => '\contrictor\gearman\yii1\SupervisorCommand',
            'gearmanComponentName' => 'gearman', // name of component: Yii::app()->gearman (from previous config listing)
        ],
    ],
    'components' => [
        'gearman' => $mainConfig['components']['gearman'],
    ],
],

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



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']);
        });
    }
}



/**
 * 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']);
        });
    }
}



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']);
        });
    }
}

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

// 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(),

// 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(),
code
composer 
bash
php artisan vendor:publish --provider="contrictor\gearman\laravel5\GearmanServiceProvider" --tag=config
bash
php yii gearman
bash
php yiic gearman
bash
php artisan gearman