# Create a scheduled task

{% hint style="info" %}
hostware uses the default task scheduler of the **Laravel Framework**. Take a look at the [official documentation](https://laravel.com/docs/10.x/scheduling)!
{% endhint %}

{% hint style="info" %}
It is recommended to only schedule console commands because this makes it easier for debugging in some cases. If you want to execute a function of a Helper class, please create a command and then schedule the command.
{% endhint %}

Using the scheduler, it is not needed to create a separate cronjob for each command. Learn how to register your command to the task scheduler below

## Add the scheduled

To add a task to the scheduler of your module, please open the ServiceProvider class of your module in the directory /Providers/ (In this case /Providers/DashservVpsServiceProvider.php)

To prepare for task scheduling, add the following code inside of the boot() function of your ServiceProvider:

```php
$this->callAfterResolving(Schedule::class, function (Schedule $schedule) {

});
```

Now, you can schedule anything just like written inside of the official laravel documentation. For example, if you want to schedule a custom cli command, the function would look like this:

```php
$this->callAfterResolving(Schedule::class, function (Schedule $schedule) {
	// As you can see, we execute the command every minute. Please use 
	// the auto completion of your IDE or view the official documentation 
	// for all possible intervals.
	
	$schedule->command('dashservvps:checkInstallingProducts')->everyMinute();
});
```

{% code title="/Providers/DashservVpsServiceProvider.php" %}

```php
<?php

namespace custom\modules\DashservVps\Providers;

use App\Framework\Module\HwModuleServiceProvider;
use custom\modules\DashservVps\Console\CheckInstallingProducts;
use Illuminate\Console\Scheduling\Schedule;

class DashservVpsServiceProvider extends HwModuleServiceProvider {
	public function boot() {
		parent::boot();

		$this->commands([
			CheckInstallingProducts::class // <-- Add the class of your command here!
		]);
	}
}
```

{% endcode %}

## Viewing your scheduled task

To view all the scheduled task and to check if your task was registered correctly, you can execute the following command to view all tasks:

```bash
php artisan schedule:list
```

## Running the scheduler on a local environment

To run the scheduler on a local environment without cronjob, just execue the following:

```bash
php artisan schedule:work
```

## Your scheduled task does not appear?

Please check that you have installed and activated your module using the hostware administration. If this still not works, please check that your module name is listed in the moduels\_statuses.json file in the hostware root directory. If noot, add your module and clear the cache using php `artisan cache:clear`
