# Görev (Task)

Eklentinizde bazı durumlarda uzun süren işlemler yaptırmanız gerekebilir. Bu gibi durumlarda Task componentini kullanabilirsiniz.&#x20;

### Task Soyut Sınıfından Türeyen Bir Sınıf Oluşturmak

* OOP eklenti klasör yapısında öncelikle app klasöründe `Tasks` isminde bir klasör oluşturuyoruz ve `InstallPackage.php` dosyası oluşturuyoruz.
* Task soyut sınıfındaki değişkenleri yaptıracağımız işe göre override ediyoruz.

```php
<?php
namespace App\Tasks;

use Liman\Toolkit\Formatter;
use Liman\Toolkit\RemoteTask\Task;

class InstallPackage extends Task
{
    protected $command = "DEBIAN_FRONTEND=noninteractive apt install @{:package} -qqy";
    protected $sudoRequired = true;
    protected $control = "apt\|dpkg";

    public function __construct(array $attributes = []){
        if(!isset($attributes["package"])){
            throw new \Exception("Package name is required");
        }
        $this->attributes = $attributes;
        $this->logFile = Formatter::run(
            "/tmp/install-package-{:package}.txt", 
            ["package" => $attributes["package"]]
        );
        $this->checkCommand = Formatter::run(
            "apt list --installed | grep {:package}", 
            ["package" => $attributes["package"]]
        );
    }
}
```

| Değişken      | Tip     | Açıklama                                                                                                 |
| ------------- | ------- | -------------------------------------------------------------------------------------------------------- |
| $command      | string  | Çalıştırılacak komut metni.                                                                              |
| $logFile      | string  | Komutun çıktılarının saklanacağı konum.                                                                  |
| $sudoRequired | boolean | Sudo gerekli ise `true` olmalıdır. (opsiyonel)                                                           |
| $control      | string  | İşlemin bitip bitmediğini anlamak için gerekli grep kontrol deseni.                                      |
| $attributes   | array   | Komut metninde değişken kullandıysanız değişkenlerin değerlerinin tanımlanması gereken dizi. (opsiyonel) |
| $checkCommand | string  | İşlemin başarıyla bitip bitmediğinin kontrolünü sağlayan komut. (opsiyonel)                              |

### Görevleri Çalıştırmak

* Controller içerisinde bir fonksiyon tanımlıyoruz ve routes.php içerisinde gerekli tanımlamaları yapıyoruz.

```php
	public function installPackage()
	{
		validate([
			'package_name' => 'required|string',
		]);
		return respond(
			view('task', [
				'tasks' => [
					0 => [
						'name' => 'InstallPackage',
						'attributes' => [
							'package' => request('package')
						]
					]
				]
			]),
			200
		);
	}
```

* Bu fonksiyon geriye task componentini çalıştırmanız için gerekli HTML verisini döndürür.&#x20;
* Birden fazla task `tasks` dizisi içerisinde tanımlanabilir böyle bir durumda görevler kuyruklanır ve sırayla çalıştırılır.&#x20;

### Blade İçerisinde Task Componentinin Kullanımı

* Tercihe bağlı olarak Görev componenti bir Modal içerisinde gösterilebilir.

```php
@component('modal-component',[
    "id" => "taskModal",
    "title" => "Görev İşleniyor",
])
@endcomponent
```

* Bu şekilde bir modal tanımlanabilir.

```php
    function installPackage(package_name){
        showSwal('{{__("Yükleniyor...")}}', 'info');
        let formData = new FormData();
        formData.append("package_name", package_name);
        request("{{API('install_package')}}", formData, function(response){
            $('#taskModal').find('.modal-body').html(JSON.parse(response).message);
            $('#taskModal').modal("show"); 
            Swal.close();
        }, function(response){
            let error = JSON.parse(response);
            showSwal(error.message, 'error', 2000);
        });
    }
```

* Görevi başlatmak için de bu şekilde javascript ile ajax çağrısı yapılır ve Task modal'ının içerisine eklenir.&#x20;


---

# 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.liman.dev/eklenti-gelistirme/sandbox/goerev-task.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.
