Gearman job server workers helper
composer require "demi/php-gearman" "~1.0"
apt-get install supervisor
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,
],
],
];
/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)
],
],
],
/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'],
],
],
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
Gearman workers - it is simple looped console commands
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']);
});
}
}
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']);
});
}
}
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,
]
Yii2:
php yii gearman
Yii1:
php yiic gearman
Laravel:
php artisan gearman
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(),
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(),
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(),