Queueing System
The WebFramework provides a flexible queueing system for handling background jobs. This system allows you to process tasks asynchronously, improving application responsiveness.
Core Components
QueueService
The QueueService is the central manager for all queues in the application. It provides methods to:
- Register queues
- Dispatch jobs
- Get queue job counts
- Register job handlers
- Retrieve job handlers for processing
Queue Interface
The Queue interface defines the contract that all queue implementations must follow:
Job Interface
Jobs are simple data containers that implement the Job interface. They should contain only the data needed to perform the task, not the logic.
All jobs must implement the following methods:
| PHP | |
|---|---|
The QueueService automatically sets the job ID when dispatching. The job name should uniquely identify the type of job.
JobHandler Interface
Job handlers implement the JobHandler interface and contain the actual logic for processing jobs:
The handle() method should throw an exception if the job fails to execute. If no exception is thrown, the job is considered successfully processed and will be removed from the queue.
Common exceptions to throw:
- InvalidJobException - when the job type doesn't match the handler's expected type
- JobDataException - when required job data is missing
- JobExecutionException - when the job execution fails (e.g., mail delivery failure)
Usage
Registering a Queue
| PHP | |
|---|---|
Creating a Job
You can either use public readonly properties or private properties with public getters.
Creating a Job Handler
Registering a Job Handler
Dispatching a Job
The dispatch() method signature is:
- dispatch(Job $job, string $queue = 'default', int $delay = 0, int $maxAttempts = 3)
Running a Queue Worker
Queue workers can be started using the console command:
| Bash | |
|---|---|
Options:
- --max-jobs: Maximum number of jobs to process before stopping
- --max-runtime: Maximum runtime in seconds before stopping
The worker will: - Process jobs from the specified queue - Automatically handle job failures and retries - Sleep for 1 second when no jobs are available - Stop when max-jobs or max-runtime is reached
Built-in Implementations
MemoryQueue
The MemoryQueue is a simple in-memory queue implementation suitable for development and testing. It is not persistent and will lose all data when the server restarts. It does not work across multiple processes or servers.
DatabaseQueue
The DatabaseQueue uses a database table to store jobs, providing persistence and the ability to share jobs across multiple processes and servers. Failed jobs are automatically moved to a dead-letter queue (named {queueName}-failed) after exceeding the maximum retry attempts. The queue implements exponential backoff for retries.
RedisQueue
The RedisQueue is implemented in the web-framework-redis module. Persistency depends on your configuration of Redis, but it's shareable between multiple instantiations and / or servers.
Best Practices
- Keep jobs small and focused on a single task
- Use job handlers for the actual processing logic
- Implement proper error handling in job handlers
- Use appropriate queue names for different types of jobs
- Consider using delays for non-urgent tasks
- Monitor queue sizes and processing times
- Implement proper logging in job handlers