composer require arko/queue-manager-bundle:dev-master
app/AppKernel.php:
new Arko\QueueManagerBundle\ArkoQueueManagerBundle(),
use Arko\QueueManagerBundle\QueueManager;
class MyController extends Controller
{
public function __construct(QueueManager $queueManager)
{
$this->queueManager = $queueManager;
}
public function enqueueTask()
{
$this->queueManager->add(function() {
// Your task logic (e.g., send email, process data)
\Log::info('Task executed!');
}, 'email_queue');
return 'Task queued!';
}
}
Enqueue Callables
Use add(callable, string $queueName) to defer execution:
$queueManager->add(fn() => $this->sendWelcomeEmail($user), 'email_queue');
Process Queues Trigger processing via CLI or HTTP endpoint:
// CLI Command Example
$queueManager->process('email_queue');
Named Queues for Isolation
Organize tasks by purpose (e.g., email_queue, report_queue).
Laravel Commands Create a custom Artisan command to process queues:
namespace App\Console\Commands;
use Arko\QueueManagerBundle\QueueManager;
use Illuminate\Console\Command;
class ProcessQueues extends Command
{
protected $signature = 'queues:process {queue?}';
protected $description = 'Process a specific queue or all queues';
public function handle(QueueManager $queueManager)
{
$queue = $this->argument('queue') ?? 'default';
$queueManager->process($queue);
$this->info("Processed queue: {$queue}");
}
}
Dependency Injection Prefer constructor injection for testability:
public function __construct(QueueManager $queueManager) {
$this->queueManager = $queueManager;
}
Contextual Queues Use dynamic queue names for scoped tasks:
$queueManager->add(fn() => $this->cleanup($userId), "cleanup_{$userId}");
No Persistence Queues are in-memory only (cleared after processing). Use a database or Redis for persistence:
composer require predis/predis
Workaround: Implement a custom storage adapter (see Extension Points).
Blocking Calls
process() executes callables synchronously. For async processing, wrap in a queue worker (e.g., Laravel Queues).
Singleton Scope
The QueueManager is a singleton. Avoid stateful operations across requests.
Queue Contents Inspect queues via reflection (not officially supported):
$reflection = new \ReflectionClass($queueManager);
$property = $reflection->getProperty('queues');
$property->setAccessible(true);
var_dump($property->getValue($queueManager));
Logging Add logging to callables for visibility:
$queueManager->add(function() {
\Log::debug('Processing task...');
// Task logic
}, 'debug_queue');
Custom Storage
Override the QueueManager service to use a persistent store:
# config/services.yaml
services:
Arko\QueueManagerBundle\QueueManager:
arguments:
$storage: '@your.custom.storage'
Priority Queues
Extend the bundle to support priority levels by modifying the add() method:
$queueManager->add($callable, 'queue_name', 1); // Priority 1
Event Dispatching Trigger events before/after processing:
$queueManager->process('queue_name', function() {
event(new QueueProcessed('queue_name'));
});
$queueManager->add(function() {
dispatch(new ProcessHeavyTask());
}, 'laravel_queue');
QueueManager in tests:
$mockQueue = $this->createMock(QueueManager::class);
$mockQueue->expects($this->once())->method('add');
How can I help you explore Laravel packages today?