mrpoundsign/pheanstalk-5.3
PHP 5.3+ fork of Pheanstalk, a pure-PHP client for the beanstalkd work queue. Supports beanstalkd up to 1.4 and implements the full beanstalkd 1.3 protocol commands/responses. Includes unit tests and sample producer/worker usage.
Installation
composer require mrpoundsign/pheanstalk-5.3
Ensure your composer.json has "minimum-stability": "dev" if using this fork.
Basic Connection
$pheanstalk = new \Pheanstalk\Pheanstalk('127.0.0.1', 11300);
First Use Case: Job Producer
$pheanstalk->useTube('critical-jobs')->put('Process user upload');
First Use Case: Job Worker
$job = $pheanstalk->watch('critical-jobs')->reserve(1); // Timeout in seconds
if ($job) {
$data = $job->getData();
$pheanstalk->delete($job);
}
tests/ directory for real-world usage patterns (run with ./tests/runtests.php).Producer Pattern
$pheanstalk->useTube('emails')->put('Send welcome email', 0, 60, 1024, ['priority' => 1000]);
useTube() to scope jobs (e.g., high-priority, low-priority).delay (seconds) for scheduled jobs.timeout (seconds) for job expiration.Worker Pattern
$pheanstalk->watch('emails')->ignore('default');
while ($job = $pheanstalk->reserve()) {
try {
process($job->getData());
$pheanstalk->delete($job);
} catch (\Exception $e) {
$pheanstalk->release($job); // Retry later
}
}
watch()/ignore().release() for retries or bury() for dead-letter queues.Batching Jobs
$batch = $pheanstalk->batch();
foreach ($users as $user) {
$batch->put("Process user {$user['id']}");
}
$batch->send();
Laravel Service Provider
$this->app->singleton(\Pheanstalk\Pheanstalk::class, function ($app) {
return new \Pheanstalk\Pheanstalk(config('queue.connection.beanstalk.host'));
});
Bind to Laravel’s container for dependency injection.
Job Serialization
Use json_encode() for complex payloads:
$pheanstalk->put(json_encode(['user_id' => 123, 'action' => 'export']));
Connection Pooling
Reuse $pheanstalk instances across requests (thread-safe for PHP-FPM).
Connection Timeouts
$pheanstalk->setReadTimeout(5); // Seconds
$pheanstalk->setWriteTimeout(5);
Job Bursting
reserve() loops without delays to prevent CPU overload:
$job = $pheanstalk->reserve(0.5); // 500ms timeout
Memory Leaks
ignore() unused tubes:
$pheanstalk->ignore('unwatched-tube');
PHP 5.3 Legacy
use \Pheanstalk\Pheanstalk; // Explicit namespace
$pheanstalk->setLogger(new \Monolog\Logger('beanstalk'));
$stats = $pheanstalk->stats();
$tubeStats = $pheanstalk->statsTube('testtube');
Custom Job Classes
Extend \Pheanstalk\Job for metadata:
class MyJob extends \Pheanstalk\Job {
public function getUserId() {
return json_decode($this->getData(), true)['user_id'];
}
}
Middleware Pipeline Intercept jobs before processing:
$job = $pheanstalk->reserve();
if ($this->middleware->handle($job)) {
$pheanstalk->delete($job);
}
Retry Logic Implement exponential backoff for failed jobs:
$attempts = $job->getData()['attempts'] ?? 0;
if ($attempts > 3) {
$pheanstalk->bury($job);
} else {
$pheanstalk->release($job, 2 ** $attempts); // Delay in seconds
}
default tube if no tube is set.0 (lowest) to 4294967295 (highest).base64_encode() for binary payloads (Beanstalkd is text-only).How can I help you explore Laravel packages today?