enqueue/gearman
Gearman transport for Enqueue: send and consume queue messages via a Gearman broker using Enqueue’s queue specification. Part of the php-enqueue ecosystem with docs, support chat, and CI-tested releases.
Install Dependencies:
composer require enqueue/gearman enqueue/laravel enqueue/doctrine
enqueue/gearman: Gearman transport.enqueue/laravel: Laravel bridge for Enqueue.enqueue/doctrine (optional): For Doctrine integration if using ORM.Configure Laravel:
Add Gearman connection to config/queue.php:
'connections' => [
'gearman' => [
'driver' => 'enqueue',
'transport' => 'gearman',
'host' => env('GEARMAN_HOST', '127.0.0.1'),
'port' => env('GEARMAN_PORT', 4730),
'timeout' => 5.0, // Gearman connection timeout
],
],
Dispatch a Job:
use App\Jobs\ProcessPodcast;
use Illuminate\Support\Facades\Queue;
Queue::connection('gearman')->push(new ProcessPodcast());
Run Gearman Worker:
Start a Gearman worker process (e.g., via gearman-worker CLI or a PHP script):
gearman-worker --job-background --queue-max-jobs=1000 --queue-timeout=60
Or programmatically:
$context = new \Enqueue\Client\Context();
$consumer = new \Enqueue\Gearman\Consumer($context, ['host' => '127.0.0.1']);
$consumer->setJobHandler(new \App\Jobs\GearmanJobHandler());
$consumer->run();
Verify Job Handling: Ensure the job appears in Gearman’s queue and is processed by the worker.
Queue facade with the Gearman connection:
Queue::connection('gearman')->later(now()->addMinutes(5), new SendEmailJob($user));
Producer:
$producer = new \Enqueue\Gearman\Producer($context, ['host' => 'gearman']);
$producer->send(new \Enqueue\Message\Message('task_name', json_encode($data)));
artisan gearman:work):
$consumer = new \Enqueue\Gearman\Consumer($context, ['host' => 'gearman']);
$consumer->setJobHandler(new class implements \Enqueue\Client\JobHandler {
public function handle(\Enqueue\Client\Job $job) {
$data = json_decode($job->getData(), true);
// Process job...
return new \Enqueue\Client\Result(200, ['status' => 'done']);
}
});
$consumer->run();
Illuminate\Bus\Queueable and implement handle():
class ProcessPodcast implements ShouldQueue {
public function handle() {
// Job logic
}
}
ConnectionFactory to switch transports at runtime:
$factory = new \Enqueue\Client\ConnectionFactory();
$connection = $factory->createConnection(['transport' => 'gearman', 'host' => 'gearman']);
$producer = new \Enqueue\Gearman\Producer($connection);
App\Exceptions\Handler:
public function register()
{
$this->registerQueueShouldBeUnique();
$this->registerQueueAfterCommit();
$this->registerQueueFailed();
}
Enqueue\Transport\Exception\TimeoutException for connection issues:
try {
$producer->send($message);
} catch (\Enqueue\Transport\Exception\TimeoutException $e) {
Log::error('Gearman timeout: ' . $e->getMessage());
// Fallback to another queue
}
gearman-worker --queue-name=worker1 --job-background
gearman-worker --queue-name=worker2 --job-background
--queue-max-jobs and --queue-timeout flags to distribute load.Queue Middleware:
Use Laravel’s queue middleware (e.g., throttle, timeout) with Gearman:
Queue::connection('gearman')->push(new Job)->throttle(5);
Job Events:
Listen to job.processing, job.processed, and job.failed events:
event(new JobProcessed($job));
Queue Monitoring:
Use Laravel’s queue:failed and queue:work commands:
php artisan queue:work --queue=gearman --sleep=3 --tries=3
Worker Prioritization:
Use Gearman’s --priority flag to prioritize critical jobs:
gearman-worker --priority=high --queue-name=critical
Job Timeouts: Set timeouts in the producer:
$producer->send($message, ['timeout' => 30]); // 30-second timeout
3. **Background Jobs**:
Run workers in detached mode:
```bash
gearman-worker --job-background --detach
Batch Processing:
Use Gearman’s --queue-max-jobs to limit concurrent jobs:
gearman-worker --queue-max-jobs=50
Connection Pooling:
Reuse Producer and Consumer instances to avoid connection overhead:
$producer = new \Enqueue\Gearman\Producer($context, ['host' => 'gearman']);
// Reuse $producer for multiple sends
Async Dispatch: Dispatch jobs asynchronously to avoid blocking HTTP requests:
Queue::connection('gearman')->push(new LongRunningJob)->onConnection('gearman');
No Active Maintenance:
enqueue/enqueue compatibility).Gearman Protocol Limitations:
Queue::later() with a fallback to database queue if Gearman fails.retryAfter().Connection Issues:
'timeout' => 10.0, // Increase if jobs are slow
Serialization Quirks:
class MyJob implements ShouldQueue, JsonSerializable {
public function toArray() { return ['data' => $this->data]; }
}
Worker Crashes:
[program:gearman-worker]
command=gearman-worker --job-background
autorestart=true
Laravel Queue Events:
job.processing, etc.) by default. Use Enqueue’s event system:
$context->getEventDispatcher()->addListener(\Enqueue\Client\Event\JobProcessed::class, function ($event) {
event(new JobProcessed($event->getJob()));
});
Docker Networking:
How can I help you explore Laravel packages today?