# Task Queue

The Task Queue file provides a queue management system to handle asynchronous jobs. It uses Redis to store jobs in the queue and supports retries when a job fails. Below is a detailed explanation of each function:

### `push()`

```php
public static function push($queueName, $eventName, $payload = null, $attempts = 0) {}
```

* **Functionality**: Pushes a job into the queue.
* **Parameters**:
  * `$queueName`: The name of the queue (e.g., `'email'`, `'notifications'`).
  * `$eventName`: The name of the event to be triggered when the job is processed (e.g., `'SendEmailEvent'`).
  * `$payload`: Data associated with the job (e.g., email information to be sent).
  * `$attempts`: The number of retry attempts (default is `0`).

Example:

```php
<?php
TaskQueue::push(
    'email', 
    'SendEmailEvent', 
    ['to' => 'user@example.com', 'subject' => 'Welcome'], 
    0
);

// Result: The job is added to the task_queue:email queue.
```

### `pop()`

```php
public static function pop($queueName, $timeout = 5) {}
```

* **Functionality**: Retrieves a job from the queue (uses blocking pop if available).
* **Parameters**:
  * `$queueName`: The name of the queue.
  * `$timeout`: Timeout duration (in seconds) if the queue is empty.

Example:

<pre class="language-php"><code class="lang-php">&#x3C;?php
$job = TaskQueue::pop('email');
print_r($job);

// Output
// [
<strong>//    'event' => 'SendEmailEvent',
</strong>//    'payload' => ['to' => 'user@example.com', 'subject' => 'Welcome'],
//    'timestamp' => 1681111111,
//    'attempts' => 0
// ]
</code></pre>

### `del()`

Completely clears all session data, effectively logging out the user and resetting the session

Exampl&#x65;**:**

```php
Session::del('user_id'); // Deletes the user_id session variable
```

### `processJob()`

```php
public static function processJob($queueName, $maxRetries = 3) {}
```

* **Functionality**: Processes a job from the queue.
* **Parameters**:
  * `$queueName`: The name of the queue.
  * `$maxRetries`: The maximum number of retry attempts (default is `3`).

Exampl&#x65;**:**

```php
<?php
TaskQueue::processJob('email');

// Result:
// If the job succeeds: The event is processed.
// If the job fails: The job is retried or moved to the failed queue.
```

### `pushFailed()`

```php
public static function pushFailed($queueName, $job) {}
```

* **Functionality**: Pushes a failed job into the failed queue.
* **Parameters**:
  * `$queueName`: The name of the original queue.
  * `$job`: The job as an array.

Example:

```php
<?php
$job = [
    'event' => 'SendEmailEvent',
    'payload' => ['to' => 'user@example.com', 'subject' => 'Welcome'],
    'timestamp' => 1681111111,
    'attempts' => 4
];
TaskQueue::pushFailed('email', $job);

// Result: The job is stored in the failed_task_queue:email queue
```

### &#x20;`runWorker()`

```php
public static function runWorker($queueName, $maxRetries = 3, $timeout = 5) {}
```

* **Functionality**: Runs a worker to continuously retrieve and process jobs from the queue.
* **Parameters**:
  * `$queueName`: The name of the queue.
  * `$maxRetries`: The maximum number of retry attempts.
  * `$timeout`: Timeout duration (in seconds) for blocking pop.

Exampl&#x65;**:**

```php
<?php
TaskQueue::runWorker('email');

// Result: The worker continuously processes jobs in the email queue
```

## Example

```php
<?php
// Register the event
Events::on('SendEmailEvent', function ($payload) {
    echo "Sending email to {$payload['to']} with subject '{$payload['subject']}'<br>";
});

// Push a job into the queue
TaskQueue::push('email', 'SendEmailEvent', ['to' => 'user@example.com', 'subject' => 'Welcome']);

// Run the worker to process the job
TaskQueue::runWorker('email');

// Output
// Sending email to user@example.com with subject 'Welcome'
```

{% hint style="success" %}
Summary

* **`push()`**: Pushes a job into the queue.
* **`pop()`**: Retrieves a job from the queue.
* **`processJob()`**: Processes a single job.
* **`pushFailed()`**: Pushes a failed job into the failed queue.
* **`runWorker()`**: Runs a worker to continuously process jobs.
  {% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cmsfullform.com/documents/libraries/task-queue.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
