Download the PHP package phelixjuma/php-enqueue without Composer
On this page you can find all versions of the php package phelixjuma/php-enqueue. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download phelixjuma/php-enqueue
More information about phelixjuma/php-enqueue
Files in phelixjuma/php-enqueue
Package php-enqueue
Short Description This is a simple but robust implementation of redis-based job queues in PHP.
License MIT
Informations about the package php-enqueue
PHELIXJUMA PHP-ENQUEUE
This is a simple but robust implementation of redis-based job queues in PHP.
Why another job queue package? Well, I tried all the top ones I could find but none just fit fine: some of the top suggested options haven't been maintained in over 5 years and their dependencies caused a lot of conflicts with my other packages, so I built a new package, for me.
The backend for this package is redis.
Requirements
- PHP >= 7.1
- vlucas/phpdotenv
- predis/predis
- symfony/console
- amphp/parallel
Installation
USAGE
1.Running the Worker
php-enqueue is event driven. Jobs are dispatched and get scheduled in redis. A worker has to be set up to run "forever". This worker listens for any incoming job and executes it. Job execution is done concurrently using amphp/parallel package which allows for several jobs to be executed concurrently
To set up a worker, run the command below:
Note that the worker takes parameters such us:
- queue: The name of the queue the worker listens to. Each worker can only listen to a single queue. This allows you to have multiple workers handling different queues which introduces a level of parallelization in your job execution
- threaded: Value of 1 means the jobs will be run in multi-threaded manner (non blocking). 0 means the jobs are run in a blocking manner. Use multi-threaded if your jobs do not depend on any global variables, otherwise, set it to 0 (blocking execution)
- concurrency: Defines the number of concurrent jobs a single worker can handle at a given time
- max_retries: If a job fails, typically by throwing an exception, it will be retried to a max number of times defined here. By default, no retry is done
- log_path: The path to the directory where logs should be put. Specify a directory path not a log file and ensure php has permissions to write to that directory
- log_level: As per the monolog log levels
NB:
- You can use a service like supervisord to run the workers and to watch them so that, if the worker itself fails, it can be automatically restarted.
- When you update your codebase on any part the workers rely on, it is good to note that the updates will not reflect unless the worker is restarted
2. Manage Jobs
You have options to manage tasks in the command line.
2.1 View list of jobs
2.2 Add new Job
2.3 Remove a job from queue
2.4 List failed jobs
2.5 Requeue failed jobs
2.6 Remove all failed jobs
3. Jobs
Every job class implement JobInterface. The classes to define are:
- setUp() - for any setup needed before the job is executed,
- perform() - for actual logic to run for the job and
- tearDown() - for any post-processing tasks to do.
3.1 Sample Job Class
3.2 Script to schedule the job
NB: A better approach would be to have an enqueue service that wraps php-enqueue. In this service, you define the redis and queue so they are reusabe. An example of such a service is as shown below
4. A note On Jobs
- This package uses Redis to enqueue jobs.
- This means that the job alongside its arguments are serialized before queueing and unserialized when fetched
- Because of how PHP handles serialization/unserialization, your Jobs and args should not have instances that are not serializable
- A good example of something that's unserializable is pdo. So if your job class injects a database class with an instance of pdo, then the job queueing will fail
- A good practice is to not inject any classes in the Job class and to avoid using class instances as arguments
- Instead, use the setUp() method alongside your DI container to instantiate any other classes you need.
Credits
- Phelix Juma ([email protected])
All versions of php-enqueue with dependencies
ext-json Version *
ext-pcntl Version *
vlucas/phpdotenv Version ^5.3
predis/predis Version ^1.1
psr/log Version ^2.0
monolog/monolog Version ^3.4
doctrine/annotations Version ^2.0
pda/pheanstalk Version ^5.0
symfony/console Version ^6.0
webmozart/assert Version ^1.11