## Getting Started
### Minimal Setup
1. **Installation**:
```bash
composer require ajtis/job-queue-bundle
Enable the bundle in config/bundles.php:
return [
// ...
Ajtis\JobQueueBundle\AjtisJobQueueBundle::class => ['all' => true],
];
Configure the Bundle:
Add the bundle configuration to config/packages/ajtis_job_queue.yaml:
ajtis_job_queue:
queue_name: 'default' # Default queue name
workers_count: 4 # Number of concurrent workers
log_file: '%kernel.logs_dir%/job_queue.log' # Worker log file
command_prefix: 'app/console' # Prefix for console commands
First Use Case: Create a console command to test:
php bin/console make:command TestJob
Modify the generated command (src/Command/TestJobCommand.php) to accept arguments:
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln('Running test job with argument: ' . $input->getArgument('arg'));
return Command::SUCCESS;
}
Enqueue the job via a controller or another command:
use Ajtis\JobQueueBundle\JobQueue\JobQueueInterface;
public function __construct(private JobQueueInterface $jobQueue) {}
public function enqueueTestJob()
{
$this->jobQueue->addJob('test_job', ['arg' => 'test_value']);
}
Run Workers: Start workers in a separate terminal:
php bin/console ajtis:job-queue:worker
Job Enqueuing:
JobQueueInterface to add jobs dynamically:
$this->jobQueue->addJob('command_name', ['arg1' => 'value1', '--option' => 'value']);
Scheduling Jobs:
cron or symfony/scheduler) to trigger job enqueuing at specific times.crontab:
0 3 * * * php bin/console app:enqueue-daily-job
Worker Management:
php bin/console ajtis:job-queue:worker --env=dev
SIGTERM/SIGINT gracefully. Use --timeout to limit job execution time:
php bin/console ajtis:job-queue:worker --timeout=3600
Job Prioritization:
high, default, low) by configuring ajtis_job_queue per queue:
ajtis_job_queue:
queues:
high:
workers_count: 2
default:
workers_count: 4
$this->jobQueue->addJob('command_name', [], 'high');
Job Retries:
config/packages/ajtis_job_queue.yaml:
ajtis_job_queue:
retry:
enabled: true
max_attempts: 3
delay: 60 # seconds between retries
Monitoring:
log_file (configurable). Use monolog handlers to stream logs to external services (e.g., ELK, Datadog).use Ajtis\JobQueueBundle\JobQueue\JobQueueInterface;
public function listJobs(JobQueueInterface $jobQueue)
{
$jobs = $jobQueue->getPendingJobs();
// Render output...
}
Dependency Injection:
JobQueueInterface into services/controllers to enqueue jobs:
use Ajtis\JobQueueBundle\JobQueue\JobQueueInterface;
public function __construct(private JobQueueInterface $jobQueue) {}
Event-Driven Workflows:
KernelEvents::TERMINATE):
use Symfony\Component\HttpKernel\Event\TerminateEvent;
use Symfony\Component\HttpKernel\KernelEvents;
public function onTerminate(TerminateEvent $event)
{
$this->jobQueue->addJob('cleanup_temp_files');
}
Database-Backed Queues:
Job entity:
// src/Entity/Job.php
use Ajtis\JobQueueBundle\Model\Job as BaseJob;
class Job extends BaseJob
{
// Add custom fields (e.g., priority, tenant_id)
}
Testing:
JobQueueInterface in unit tests:
$jobQueue = $this->createMock(JobQueueInterface::class);
$jobQueue->expects($this->once())->method('addJob')->with('test_command', ['arg' => 'value']);
Ajtis\JobQueueBundle\Tests\JobQueueTestCase for integration tests.Docker/Kubernetes:
# docker-compose.yml
services:
job_worker:
image: your-app
command: php bin/console ajtis:job-queue:worker --env=prod
deploy:
replicas: 3
Worker Stuck in "Busy" State:
--timeout in worker command or handle exceptions in commands:
try {
// Command logic
} catch (\Exception $e) {
throw new \RuntimeException('Job failed: ' . $e->getMessage());
}
Jobs Disappearing Without Execution:
log_file) and verify workers are connected to the correct queue name.Argument/Option Parsing Issues:
--option and arg).JobQueueInterface methods consistently:
// Correct: Separate arguments and options
$this->jobQueue->addJob('command_name', ['arg1', 'arg2'], [], ['--option' => 'value']);
Database Locking:
doctrine connection pooling or use pessimistic_write locks in custom Job entities.Worker Process Leaks:
finally blocks:
$file = fopen('temp.txt', 'w');
try {
// Work with file
} finally {
fclose($file);
}
Enable Verbose Logging:
php bin/console ajtis:job-queue:worker --verbose
Or configure monolog in config/packages/monolog.yaml:
handlers:
job_queue:
type: stream
path: '%kernel.logs_dir%/job_queue_verbose.log'
level: debug
Inspect Queue State:
public function listJobs(JobQueueInterface $jobQueue)
{
$jobs = $jobQueue->getPendingJobs();
foreach ($jobs as $job) {
var_dump($job->getCommand(), $job->getArguments(), $job->getOptions());
}
}
Simulate Failures:
throw new \RuntimeException('Simulated failure');
queue_name in ajtis_job_queue.yaml is the default. OverHow can I help you explore Laravel packages today?