Download the PHP package tobento/service-queue without Composer
On this page you can find all versions of the php package tobento/service-queue. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package service-queue
Queue Service
A queue system for processing jobs in background.
Table of Contents
- Getting started
- Requirements
- Highlights
- Documentation
- Creating Jobs
- Job
- Using A Named Job
- Using A Job Handler
- Callable Job
- Job
- Job Parameters
- Data Parameter
- Delay Parameter
- Duration Parameter
- Encrypt Parameter
- Monitor Parameter
- Priority Parameter
- Pushing Parameter
- Queue Parameter
- Retry Parameter
- Unique Parameter
- Without Overlapping Parameter
- Dispatching Jobs
- Queue
- In Memory Queue
- Null Queue
- Storage Queue
- Sync Queue
- Queues
- Default Queues
- Lazy Queues
- Queue Factories
- Queue Factory
- Storage Queue Factory
- Job Processor
- Adding Job Handlers
- Failed Job Handler
- Worker
- Running Worker
- Running Worker Using Commands
- Console
- Work Command
- Clear Command
- Events
- Learn More
- Creating Custom Job Parameters
- Chunkable Job Example
- Creating Jobs
- Credits
Getting started
Add the latest version of the task queue project running this command.
Requirements
- PHP 8.0 or greater
Highlights
- Framework-agnostic, will work with any project
- Decoupled design
Documentation
Creating Jobs
Job
You may use the to create jobs.
Using A Named Job
Next, you will need to add add a Job Handler which handles the job:
Using A Job Handler
First, create the job handler:
Finally, create the job:
Callable Job
You may use the to create jobs.
Parameters of the class constructor must be optional if they cannot be resolved by the container!
Creating the job:
Job Parameters
You may use the available parameters providing basic features for jobs or create custom parameters to add new features or customizing existing to suit your needs.
Parameter helper methods
The Callable Job support the following helper methods:
If you are using a Callable Job you may specify default parameters using the method:
Delay Parameter
Use the delay parameter to set the seconds the job needs to be delayed.
Queues supporting delays:
- Storage Queue
Data Parameter
Use the data parameter to add additional job data.
Duration Parameter
Use the duration parameter to set the approximate duration the job needs to process.
The Failed Job Handler will requeue the job if the job could not be run to prevent timing out.
Encrypt Parameter
The encrypt parameter uses the Service Encryption to encrypt the job data.
It will encrypt the following data:
- job payload
- Data Parameter values
First, install the service:
Next, bind the encrypter to your container used by the Job Processor:
Example using the Service Container as container:
Check out the Crypto Implementation section to learn more.
Finally, add the parameter to your job:
You may create a custom encrypt parameter to use another encrypter or to customize the encryption.
Monitor Parameter
The monitor parameter is added by the Work Command to write its data to the console.
Priority Parameter
Use the priority parameter to specify the priority of the job. Higher prioritized jobs will be processed first.
Pushing Parameter
Use the pushing parameter to specify a handler executed before the job gets pushed to the queue.
Queue Parameter
Use the queue parameter to specify the queue to push the job to.
The parameter will automatically be added by the Job Processor when the job is pushed to the queue.
Retry Parameter
Use the retry parameter to specify the max number of retries.
The Failed Job Handler uses the parameter to handle the retries.
Unique Parameter
The unique parameter will prevent any new, duplicate jobs from entering the queue while another instance of that job is queued or processing.
The parameter requires a to be binded to your container passed to the JobProcessor:
Example using the Cache Service and Container Service:
Without Overlapping Parameter
If you add the without overlapping parameter, the job will only be processed once at a time to prevent overlapping.
The parameter requires a to be binded to your container passed to the JobProcessor:
Example using the Cache Service and Container Service:
Dispatching Jobs
You may consider binding one of the Queues to the container as the default implementation, otherwise you will need to use the queues in order to dispatch on a certain queue:
Queue
In Memory Queue
The does store jobs in memory.
Null Queue
The does not queue any job and therefore jobs will not be processed at all.
Storage Queue
The uses the Storage Service to store the jobs.
First, you will need to install the storage service:
Next, you may install the clock service or use another implementation:
Finally, create the queue:
The storage needs to have the following table columns:
Column | Type | Description |
---|---|---|
bigint(21) primary key | - | |
varchar(100) | Used to store the queue name | |
varchar(255) | Used to store the job id | |
varchar(255) | Used to store the job name | |
json | Used to store the job payload | |
json | Used to store the job parameters | |
int(11) | Used to store the job priority | |
timestamp | Used to handle the job delay |
Sync Queue
The does dispatch jobs immediately without queuing.
Queues
Default Queues
Lazy Queues
The creates the queues only on demand.
You may check out the Queue Factories to learn more about it.
Queue Factories
Queue Factory
Check out the Job Processor to learn more about it.
Create queue
Storage Queue Factory
Check out the Job Processor to learn more about it.
Create queue
Create queue
Create or queue
Job Processor
The is responsible for processing jobs.
Adding Job Handlers
You may add job handlers for Named Jobs:
Example of handler:
Failed Job Handler
The is responsible for handling failed jobs.
After a failed job has exceeded the number of attempts defined with the Retry Parameter, the job will be lost.
You may extend and handle the finally failed jobs by using the method for storing the jobs in a database or simply log them:
Worker
The processes the queued jobs.
Running Worker
Running Worker Using Commands
Check out the Work Command section if you want to run the worker using commands.
Console
You may using the following commands using the Console Service.
To get quickly started consider using the following two app bundles:
Otherwise, you need to install the Console Service and set up your console by yourself.
Work Command
Running jobs from all queues
Running jobs from specific queue only
Available Options
Option | Description |
---|---|
The name of the worker. | |
The name of the queue to work. | |
The memory limit in megabytes. | |
The number of seconds the worker can run. | |
The number of seconds to sleep when no job is available. | |
The number of jobs to process before stopping (0 unlimited). | |
Stops the worker when the queue is empty. |
Clear Command
Delete all of the jobs from the queues
Delete jobs from specific queues only
Events
Available Events
Event | Description |
---|---|
The event will dispatch before the job is processed | |
The event will dispatch after the job is processed | |
The event will dispatch when the job failed. | |
The event will dispatch after the worker started. | |
The event will dispatch just before the worker stops. | |
The event will dispatch when popping a job from a queue failed. |
Just make sure you pass an event dispatcher to your worker!
Learn More
Creating Custom Job Parameters
You may create a custom parameter by extending the :
Storable parameter
By implementing the interface your parameter will be stored and available when handling the job.
Failable interface
By implementing the interface your can handle failed jobs.
Check out the to see its implementation.
Poppable interface
By implementing the interface you can handle jobs after it is popped from the queue.
Check out the to see its implementation.
Processable interface
By implementing the interface you can handle jobs processing.
Check out the to see its implementation.
Pushable interface
By implementing the interface you can handle jobs before being pushed to the queue.
Check out the to see its implementation.
Chunkable Job Example
This example shows a possible way to create a chunkable job using the data parameter to store its process data.
Credits
- Tobias Strub
- All Contributors
All versions of service-queue with dependencies
psr/container Version ^2.0
psr/simple-cache Version 2 - 3
psr/clock Version ^1.0
tobento/service-autowire Version ^1.0.9