tudorr89 / phpqdrv
A simple queue system supporting multiple backends for PHP
1.0
2024-10-31 14:09 UTC
Requires
- php: ^8.1
- ext-pdo: *
- pear/net_beanstalkd: ^0.5.1
- php-di/php-di: ^7.0
- predis/predis: ^2.0
Requires (Dev)
- mockery/mockery: ^1.4
- phpunit/phpunit: ^9.5
Suggests
- ext-pgsql: For using PostgreSQL queue driver
- ext-redis: For using Redis queue driver
- ext-sqlite3: For using SQLite queue driver
README
A flexible PHP queue management system supporting multiple backend drivers.
🚀 Features
- Multiple queue backend support
- Redis
- MariaDB
- PostgreSQL
- SQLite
- Beanstalkd
- Robust job processing
- Configurable retry mechanisms
- Extensible architecture
📦 Installation
Install the package using Composer:
composer require tudorr89/phpqdrv
🔧 Requirements
- PHP 8.1+
- Supported database extensions based on chosen driver
💡 Usage Examples
Redis Queue
<?php use Predis\Client; use Tudorr89\Phpqdrv\QueueFactory; use Tudorr89\Phpqdrv\Worker; // Create Redis connection $redis = new Client([ 'host' => '127.0.0.1', 'port' => 6379 ]); // Create queue instance $queue = QueueFactory::createRedisQueue($redis); // Enqueue a job $job = $queue->push('emails', [ 'to' => 'user@example.com', 'subject' => 'Welcome', 'body' => 'Hello World!' ]); // Create a worker $worker = new Worker($queue); // Process jobs $worker->work('emails', function($payload) { sendEmail( $payload['to'], $payload['subject'], $payload['body'] ); });
PostgreSQL Queue
<?php use PDO; use Tudorr89\Phpqdrv\QueueFactory; use Tudorr89\Phpqdrv\Worker; // Create PDO connection $pdo = new PDO( 'pgsql:host=localhost;dbname=mydb', 'username', 'password' ); // Create queue instance $queue = QueueFactory::createPostgreSQLQueue($pdo); // Similar job enqueuing and processing as Redis example
Beanstalkd Queue
<?php use Net_Beanstalkd; use Tudorr89\Phpqdrv\QueueFactory; use Tudorr89\Phpqdrv\Worker; // Create Beanstalkd connection $beanstalkd = new Net_Beanstalkd('localhost', 11300); // Create queue instance $queue = QueueFactory::createBeanstalkdQueue($beanstalkd); // Similar job enqueuing and processing as previous examples
🛠 Advanced Configuration
Worker Configuration
// Customize worker behavior $worker = new Worker( $queue, $maxAttempts = 3, // Maximum job retry attempts $sleepTime = 5 // Seconds to wait between job checks );
📊 Queue Methods
Each queue driver implements these core methods:
push(string $queue, array $payload)
: Add a new job to the queuepop(string $queue)
: Retrieve and remove a job from the queueack(JobInterface $job)
: Acknowledge successful job completionfail(JobInterface $job)
: Mark a job as failedcount(string $queue)
: Count pending jobs in a queue
🔒 Error Handling
- Configurable max retry attempts
- Automatic job failure after max attempts
- Supports logging and custom error handling
📋 Planned Features
- Improved logging
- More sophisticated retry strategies
- Advanced job scheduling
- Distributed queue support
🤝 Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
📜 License
Distributed under the MIT License. See LICENSE
for more information.
🛟 Support
If you encounter any issues or have questions, please file an issue on the GitHub repository.