> For the complete documentation index, see [llms.txt](https://docs.opencart.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.opencart.com/developer-guide/tasks.md).

# Tasks

### Introduction

Tasks have been introduced in OpenCart v4.2, they are background processes for tasks that does not require immediate feedback, aiming to speed up the whole experience.

Tasks are run in a background process using PHP shell\_exec() function, which may overcome some server limitations while using normal PHP process.

### How to Create a task

* **Create a task class**: Write your code into the index() method.
* **Register the task**: Save your task using the task registry.
* **Ensure Accessibility**: Ensure the task is correctly working.

#### Example

**Step 1: Create the Task Class**

Create a PHP class for your extension. For example, place the following code in `extension/test_module/catalog/controller/task.php`:

```php
<?php
namespace Opencart\Catalog\Controller\Extension\TestModule;

class Task extends \Opencart\System\Engine\Controller {
  public function index(): void {
    // Process your customizations here
    $this->log->write('My task has successfully started!');
  }
  
}
```

**Step 2: Register the Task**

Tasks are typically registered in the `install()` method of your extension.

Add the following code to your module’s controller:

```php
<?php
namespace Opencart\Admin\Controller\Extension\TestModule\Module;

class TestModule extends \Opencart\System\Engine\Controller {
    public function install(): void {
    // Load the task model
    $this->load->model('setting/task');

    // Register the task
    $task_data = [
    	'code'   => 'my_task',
		'action' => 'task/catalog/test_module.task',
		'args'   => []
	];

	$this->model_setting_task->addTask($task_data);
  }

  public function uninstall(): void {
    // Remove the task on uninstall
    $this->load->model('setting/task');
    $this->model_setting_task->deleteTaskByCode('my_task');
  }
}
```

**Step 3: Test the Task**

* Install your module via the OpenCart admin panel (`Extensions > Installer`).
* Install your module in Extensions menu (`Extensions > Extensions`).
* Open `Extension > Tasks`, you should see your new entry
* Click on the button `▶️ Run Tasks`
* Check the log file (`System > Maintenance > Error logs`) to verify that your custom log message appears.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.opencart.com/developer-guide/tasks.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
