Installation:
composer require dmank/gearman-bundle
Add to config/app.php under providers:
DominikMank\GearmanBundle\GearmanBundle::class,
Configuration:
Edit config/gearman.php to define your Gearman server(s):
return [
'servers' => [
'default' => [
'host' => '127.0.0.1',
'port' => 4730,
],
],
];
First Use Case: Define a job class:
namespace App\Jobs;
use DominikMank\GearmanBundle\Job\JobInterface;
class ProcessData implements JobInterface
{
public function run()
{
// Your logic here
return 'Processed!';
}
}
Dispatch the job:
$client = app('gearman.client');
$client->addJob('process_data', new ProcessData(), function ($job) {
// Handle response
});
Job Dispatching:
// Basic dispatch
$client->addJob('task_name', new MyJob(), function ($job) {});
// With unique ID
$client->addJob('task_name', new MyJob(), function ($job) {}, 'unique_id');
// With priority
$client->addJob('task_name', new MyJob(), function ($job) {}, null, 1000);
Background Jobs:
Use addBackgroundJob() for fire-and-forget tasks:
$client->addBackgroundJob('task_name', new MyJob());
Integration with Laravel Queues: Create a custom queue driver:
// config/queue.php
'connections' => [
'gearman' => [
'driver' => 'gearman',
'server' => 'default',
],
],
Dispatch via Laravel’s queue system:
dispatch(new MyJob());
Worker Setup:
Register a worker in app/Console/Kernel.php:
protected function schedule(Schedule $schedule)
{
$schedule->command('gearman:work')->everyMinute();
}
Run workers:
php artisan gearman:work
Task Groups:
Use addTask() for grouped jobs:
$group = $client->addTask();
$group->addJob('task1', new Job1());
$group->addJob('task2', new Job2());
$group->run();
Connection Issues:
try {
$client->addJob(...);
} catch (\GearmanException $e) {
Log::error('Gearman error: ' . $e->getMessage());
}
ping() to check server connectivity:
$client->ping();
Job Timeouts:
config/gearman.php:
'timeout' => 60, // seconds
Worker Stuck on Tasks:
onException() in jobs:
public function onException(\Exception $e)
{
Log::error('Job failed: ' . $e->getMessage());
}
Serialization:
__serialize() and __unserialize() in jobs for complex data:
public function __serialize()
{
return ['data' => $this->data];
}
public function __unserialize(array $data)
{
$this->data = $data['data'];
}
Logging:
Enable debug logging in config/gearman.php:
'debug' => env('APP_DEBUG', false),
Log worker activity:
$client->setLogger(function ($message) {
Log::debug($message);
});
Testing:
Use GearmanMock for unit tests:
$mock = new \DominikMank\GearmanBundle\Testing\GearmanMock();
$client = new \GearmanClient();
$client->addServer($mock);
Load Balancing: Configure multiple servers for redundancy:
'servers' => [
'primary' => ['host' => '127.0.0.1', 'port' => 4730],
'backup' => ['host' => '192.168.1.100', 'port' => 4730],
],
Use setServers() dynamically:
$client->setServers(['primary', 'backup']);
Monitoring: Track job progress with callbacks:
$client->addJob('task', new MyJob(), function ($job) {
Log::info('Job progress: ' . $job->getProgress());
});
Custom Workers:
Extend \DominikMank\GearmanBundle\Worker\Worker for custom logic:
class MyWorker extends Worker
{
protected function getTasks()
{
return ['task1', 'task2'];
}
}
Register in services.yaml:
gearman.worker.class: App\Worker\MyWorker
How can I help you explore Laravel packages today?