PHPackages                             phelixjuma/php-enqueue - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Queues &amp; Workers](/categories/queues)
4. /
5. phelixjuma/php-enqueue

ActiveLibrary[Queues &amp; Workers](/categories/queues)

phelixjuma/php-enqueue
======================

This is a simple but robust implementation of redis-based job queues in PHP.

v4.2.10(1y ago)1216MITPHPPHP ^7.4 || ^8.0

Since Sep 1Pushed 1y ago2 watchersCompare

[ Source](https://github.com/phelixjuma/php-enqueue)[ Packagist](https://packagist.org/packages/phelixjuma/php-enqueue)[ RSS](/packages/phelixjuma-php-enqueue/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (9)Versions (108)Used By (0)

PHELIXJUMA 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
============

[](#requirements)

- PHP &gt;= 7.1
- vlucas/phpdotenv
- predis/predis
- symfony/console
- amphp/parallel

Installation
============

[](#installation)

```
composer require phelixjuma/php-enqueue

```

USAGE
=====

[](#usage)

1.Running the Worker
--------------------

[](#1running-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:

```
./bin/worker --queue=name_of_queue --threaded=1 --concurrency=1 --max_retries=3 --log_path=/path/to/log log_level=100

```

Note that the worker takes parameters such us:

1. 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
2. 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)
3. concurrency: Defines the number of concurrent jobs a single worker can handle at a given time
4. 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
5. 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
6. log\_level: As per the monolog log levels

NB:

1. 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.
2. 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
--------------

[](#2-manage-jobs)

You have options to manage tasks in the command line.

### 2.1 View list of jobs

[](#21-view-list-of-jobs)

```
./bin/manager enqueue:list --queue=queue_name

```

### 2.2 Add new Job

[](#22-add-new-job)

```
./bin/manager enqueue:add --queue=queue_name --class=job_class_name --parameters=job_args

```

### 2.3 Remove a job from queue

[](#23-remove-a-job-from-queue)

```
./bin/manager enqueue:remove --queue=queue_name --taskId=task_id

```

### 2.4 List failed jobs

[](#24-list-failed-jobs)

```
./bin/manager enqueue:failed:list --queue=queue_name

```

### 2.5 Requeue failed jobs

[](#25-requeue-failed-jobs)

```
./bin/manager enqueue:failed:requeue --queue=queue_name

```

### 2.6 Remove all failed jobs

[](#26-remove-all-failed-jobs)

```
./bin/manager enqueue:failed:purge --queue=queue_name

```

3. 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

[](#31-sample-job-class)

```
class EmailJob implements JobInterface
{

    /**
     * @var Logger
     */
    private $logger;

    public function setUp(Task $task)
    {
        $this->logger = new Logger("email_job");
        $this->logger->pushHandler(new StreamHandler('/path/to/log/email_job.log', Logger::DEBUG));
    }

    public function perform(Task $task)
    {
        // Actual logic to send an email goes here
        $this->logger->info("Performing email job. Args: ".json_encode($task->getArgs()));
    }

    public function tearDown(Task $task)
    {
    }
}
```

### 3.2 Script to schedule the job

[](#32-script-to-schedule-the-job)

```
use Phelixjuma\Enqueue\Jobs\EmailJob;
use Phelixjuma\Enqueue\RedisQueue;
use Phelixjuma\Enqueue\Task;
use Predis\Client;

// Some global section where redis and queue are defined
$redis = new Client('tcp://127.0.0.1:6379');
$queue = new RedisQueue($redis);

// Actual section to queue the email job task
$queue
->setName('test_queue')
->enqueue(new Task(new EmailJob(), ['some_arg' => 'some_value']));
```

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

```
