Installation Add the bundle via Composer:
composer require eduardtrandafir/gearman-bundle
Register the bundle in config/app.php under providers:
EduardTrandafir\GmBundle\GmBundle::class,
Configuration Publish the default config:
php artisan vendor:publish --provider="EduardTrandafir\GmBundle\GmBundle" --tag=config
Update config/gm.php with your Gearman server details (host, port, etc.).
First Use Case Define a simple job task:
use EduardTrandafir\GmBundle\Task\TaskInterface;
class ExampleTask implements TaskInterface
{
public function run($workload)
{
return "Processed: " . $workload;
}
}
Register the task in config/gm.php:
'tasks' => [
'example_task' => [
'class' => \App\Jobs\ExampleTask::class,
'timeout' => 30,
],
],
Dispatch the job:
$client = app('gearman.client');
$client->addTask('example_task', 'Hello Gearman!');
Job Dispatching Use the Gearman client to offload CPU-intensive tasks:
$client = app('gearman.client');
$client->addTask('image_processing', $imageData, function($task) {
// Optional callback for task completion
return $task->data();
});
Worker Management
Register workers in config/gm.php:
'workers' => [
'image_processor' => [
'class' => \App\Workers\ImageProcessor::class,
'tasks' => ['image_processing'],
'options' => ['timeout' => 60],
],
],
Start workers via Artisan command:
php artisan gm:worker image_processor
Task Chaining Chain tasks for sequential processing:
$client->addTask('task_one', $data, function($task) {
return $task->data() . "_processed";
})->addTask('task_two', $task->data());
Background Processing Use Laravel’s queue system to trigger Gearman jobs:
Dispatch::onQueue('gearman')->dispatch(new ProcessDataJob($data));
TaskInterface::run() to return false or throw exceptions for failed tasks.json_encode()/json_decode()).GearmanWorker::addOptions(GEARMAN_WORKER_NICE)).Worker Registration
config/gm.php and started via php artisan gm:worker.app/Console/Kernel.php schedule or run manually.Task Timeouts
timeout in config/gm.php or worker options:
'options' => ['timeout' => 300], // 5 minutes
Data Corruption
json_encode() for payloads or implement __serialize()/__unserialize().Connection Issues
retry helper:
try {
$client->addTask('task', $data);
} catch (\Exception $e) {
retry()->times(3)->later(5)->try(fn() => $client->addTask('task', $data));
}
config/gm.php:
'log' => [
'level' => 'debug',
'file' => storage_path('logs/gearman.log'),
],
php artisan gm:status to check pending/running tasks.Custom Workers
Extend EduardTrandafir\GmBundle\Worker\AbstractWorker for reusable worker logic:
class CustomWorker extends AbstractWorker
{
protected function handleTask($task)
{
// Custom logic
}
}
Task Middleware
Add middleware to tasks via TaskInterface:
public function run($workload)
{
$this->middleware->preProcess($workload);
$result = $this->process($workload);
return $this->middleware->postProcess($result);
}
Event Listeners
Listen for task events (e.g., TaskStarted, TaskCompleted) via Laravel’s event system:
Event::listen('gearman.task.completed', function($event) {
Log::info("Task {$event->taskName} completed");
});
Monitoring Integrate with Laravel Horizon or custom dashboards by exposing Gearman stats via:
$client->stats(); // Returns server/worker statistics
How can I help you explore Laravel packages today?