Installation:
vendor/:
composer require drymek/pheanstalk-bundle
AppKernel.php (only for dev/test environments):
$bundles[] = new drymek\PheanstalkBundle\drymekPheanstalkBundle();
app/autoload.php to register namespaces:
$loader->registerNamespaces([
'Pheanstalk' => __DIR__.'/../vendor/pheanstalk/classes',
'drymek' => __DIR__.'/../vendor/bundles',
]);
Basic Configuration:
config.yml (defaults: 127.0.0.1:11300, 3s timeout):
drymek_pheanstalk: ~
drymek_pheanstalk:
server: "your-beanstalkd-server"
port: 11301
timeout: 5
First Use Case:
pheanstalk service into a controller/service:
use Pheanstalk\Pheanstalk;
class JobController extends Controller
{
public function enqueueJob(Pheanstalk $pheanstalk)
{
$tube = $pheanstalk->useTube('default');
$tube->put('Hello, Beanstalk!');
return new Response('Job enqueued!');
}
}
Job Enqueuing:
Pheanstalk service to interact with Beanstalkd tubes:
$pheanstalk->useTube('high_priority')->put('job_payload', 0, 60); // Priority 0, TTR 60s
services:
app.job_enqueuer:
class: App\Service\JobEnqueuer
arguments: ['@pheanstalk']
Job Consumption:
$job = $pheanstalk->reserve(1); // Wait 1 second for a job
if ($job) {
$payload = $job->getData();
// Process payload...
$job->delete(); // Acknowledge completion
}
class ProcessJobsCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output)
{
$pheanstalk = $this->getContainer()->get('pheanstalk');
while ($job = $pheanstalk->reserve()) {
// Process job...
$job->delete();
}
}
}
Tube Management:
$pheanstalk->createTube('new_tube');
$pheanstalk->deleteTube('old_tube');
emails, reports).Delayed Jobs:
$pheanstalk->useTube('delayed')->put('job_data', 0, time() + 3600); // Delay 1 hour
Pheanstalk via constructor for testability.try {
$job = $pheanstalk->reserve();
} catch (\Pheanstalk\Exception\ConnectionException $e) {
$this->logger->error('Beanstalkd connection failed');
}
parameters.yml):
parameters:
beanstalk.server: "%env(BEANSTALK_SERVER)%"
drymek_pheanstalk:
server: "%beanstalk.server%"
Connection Timeouts:
drymek_pheanstalk:
timeout: 10
ConnectionException if jobs hang indefinitely.Tube Naming:
InvalidTubeNameException if invalid characters are used.Job Expiration:
time_to_run (TTR) exceeding the server’s max-job-timeout will fail.300 seconds).Development vs. Production:
dev/test environments by default. Ensure it’s enabled in prod if needed:
$bundles[] = new drymek\PheanstalkBundle\drymekPheanstalkBundle();
Dev Tools Overhead:
/_pheanstalk routes (for monitoring) are only available in dev/test. Avoid exposing them in production.$this->logger->debug('Job reserved', ['job_id' => $job->getJobId()]);
beanstalkd’s CLI tools (beanstalkd-console) to inspect tubes/jobs:
beanstalkd-console -h 127.0.0.1 -p 11300
beanstalkd -l 0.0.0.0 -p 11300).Custom Services:
pheanstalk:
services:
app.pheanstalk.decorated:
decorates: pheanstalk
class: App\Service\PheanstalkDecorator
Event Listeners:
$pheanstalk->addListener('job.reserved', function ($job) {
$this->dispatcher->dispatch(new JobReservedEvent($job));
});
Configuration Overrides:
$container->setParameter('drymek_pheanstalk.server', getenv('BEANSTALK_HOST'));
Testing:
Pheanstalk in tests:
$mock = $this->createMock(Pheanstalk::class);
$mock->method('reserve')->willReturn($job);
$this->container->set('pheanstalk', $mock);
pheanstalk-mock (e.g., php-pecl/pecl_http) for integration tests.How can I help you explore Laravel packages today?