<commit_before>**Dispatcher** allows you to schedule your artisan commands within your Laravel project, eliminating the need to touch the crontab when deploying.  It also allows commands to run per environment and keeps your scheduling logic where it should be, in your version control.

[![Latest Stable Version](https://poser.pugx.org/indatus/dispatcher/v/stable.png)](https://packagist.org/packages/indatus/dispatcher) [![Total Downloads](https://poser.pugx.org/indatus/dispatcher/downloads.png)](https://packagist.org/packages/indatus/dispatcher) [![Build Status](https://travis-ci.org/Indatus/dispatcher.png?branch=master)](https://travis-ci.org/Indatus/dispatcher) [![Coverage Status](https://coveralls.io/repos/Indatus/dispatcher/badge.png?branch=master)](https://coveralls.io/r/Indatus/dispatcher?branch=master)

<img align="left" height="300" src="https://s3-us-west-2.amazonaws.com/oss-avatars/dispatcher_round_readme.png">

```php
use Indatus\Dispatcher\ScheduledCommand;
use Indatus\Dispatcher\Schedulable;
use Indatus\Dispatcher\Drivers\Cron\Scheduler;

class MyCommand extends ScheduledCommand {

    //your command name, description etc.

	public function schedule(Schedulable $scheduler)
	{
        //every day at 4:17am
        return $scheduler
            ->daily()
            ->hours(4)
            ->minutes(17);
    }

}
```

## README Contents

* [Features](#features)
* [Installation](#installation)
* [Usage](#usage)
  * [Generating New Scheduled Commands](#new-commands)
  * [Scheduling Existing Commands](#scheduling-commands)
  * [Running Commands As Users](#commands-as-users)
  * [Environment-specific commands](#environment-commands)
* [Drivers](#drivers)
  * [Cron](#Cron)
* [Custom Drivers](#custom-drivers)
* [FAQ](#faq)

<a name="features" />
## Features

 * Schedule artisan commands to run automatically
 * Scheduling is maintained within your version control system
 * Run commands as other users
 * Run commands in certain environments

<a name="installation" />
## Installation

You can install the library via [Composer](http://getcomposer.org) by adding the following line to the **require** block of your *composer.json* file:

````
"indatus/dispatcher": "dev-master"
````

Next run `composer update`.

Add this line to the providers array in your `app/config/app.php` file :

```php
        'Indatus\Dispatcher\ServiceProvider',
```

To use with Cron, see the [Cron driver](#Cron) section.

<a name="usage" />
## Usage

```
scheduled
  scheduled:make              Create a new scheduled artisan command
  scheduled:run               Run scheduled commands
  scheduled:summary           View a summary of all scheduled artisan commands
```

If commands are not visible via `php artisan` then they cannot be scheduled.

<a name="new-commands" />
### Generating New Scheduled Commands

Use `php artisan scheduled:make` to generate a new scheduled command, the same way you would use artisan's `command:make`.

<a name="scheduling-commands" />
### Scheduling Existing Commands

You may either `implement \Indatus\Dispatcher\ScheduledCommandInterface` or follow the below steps.

1. `extend \Indatus\Dispatcher\ScheduledCommand`
2. Add use statements to your command.  If you're using a custom driver you will use a different `Scheduler` class.
```php
use Indatus\Dispatcher\ScheduledCommand;
use Indatus\Dispatcher\Schedulable;
use Indatus\Dispatcher\Drivers\Cron\Scheduler;
```
3. Implement schedule():
```php
	/**
	 * When a command should run
	 *
	 * @param Scheduler $scheduler
	 * @return \Indatus\Dispatcher\Schedulable
	 */
	public function schedule(Schedulable $scheduler)
	{
		return $scheduler;
    }
```

For details and examples on how to schedule, see [Cron](#Cron).

<a name="commands-as-users" />
### Running Commands As Users

You may override `user()` to run a given artisan command as a specific user.  Ensure your `scheduled:run` artisan command is running as root.

```php
    public function user()
    {
        return 'backup';
    }
```

> This feature may not be supported by all drivers.

<a name="environment-commands" />
### Environment-specific commands

You may override `environment()` to ensure your command is only scheduled in specific environments.  It should provide a single environment or an array of environments.

```php
    public function environment()
    {
        return ['development','staging'];
    }
```



<a name="drivers" />
## Drivers

While Cron is the default driver for Dispatcher, it can be used with any scheduling tool that can run artisan commands. See [building custom drivers](#custom-drivers).

<a name="Cron" />
### Cron (Default)

Add the following to your Crontab:

```php
* * * * * php /path/to/artisan scheduled:run 1>> /dev/null 2>&1
```

> If you'd like for scheduled commands to be able to run as different users, be sure to add this to the root Crontab.  Otherwise all commands run as the user whose Crontab you've added this to.

Examples of how to schedule:

```php
	public function schedule(Schedulable $scheduler)
	{
        //every day at 4:17am
        return $scheduler->daily()->hours(4)->minutes(17);
    }
```


```php
	public function schedule(Schedulable $scheduler)
	{
        //every Tuesday/Thursday at 5:03am
        return $scheduler->daysOfTheWeek([
                Scheduler::TUESDAY,
                Scheduler::THURSDAY
            ])->hours(5)->minutes(3);
    }
```

You may also schedule commands via raw Cron expressions

```php
	public function schedule(Schedulable $scheduler)
	{
        //every other day at 3:15am, 4:15am and 5:15am
        return $scheduler->setSchedule(15, [3,4,5], '*/2', '*', '*');
    }
```

<a name="custom-drivers" />
## Custom Drivers

You can build your own drivers or extend a driver that's included.  Create a packagepath such as `\MyApp\ScheduleDriver\` and create two classes:

 * `Scheduler` that `implements Indatus\Dispatcher\Schedulable`.  This class should provide a useful interface for programmers to schedule their commands.
 * `ScheduleService` that `extends \Indatus\Dispatcher\Services\ScheduleService`.  This class contains logic on how to determine if a command is due to run.

Publish the configs using `php artisan view:publish indatus/dispatcher`. Then update your driver configuration to reference the package in which these 2 classes are included (do not include a trailing slash):

```php<commit_msg>publish config not view<commit_after>**Dispatcher** allows you to schedule your artisan commands within your Laravel project, eliminating the need to touch the crontab when deploying.  It also allows commands to run per environment and keeps your scheduling logic where it should be, in your version control.

[![Latest Stable Version](https://poser.pugx.org/indatus/dispatcher/v/stable.png)](https://packagist.org/packages/indatus/dispatcher) [![Total Downloads](https://poser.pugx.org/indatus/dispatcher/downloads.png)](https://packagist.org/packages/indatus/dispatcher) [![Build Status](https://travis-ci.org/Indatus/dispatcher.png?branch=master)](https://travis-ci.org/Indatus/dispatcher) [![Coverage Status](https://coveralls.io/repos/Indatus/dispatcher/badge.png?branch=master)](https://coveralls.io/r/Indatus/dispatcher?branch=master)

<img align="left" height="300" src="https://s3-us-west-2.amazonaws.com/oss-avatars/dispatcher_round_readme.png">

```php
use Indatus\Dispatcher\ScheduledCommand;
use Indatus\Dispatcher\Schedulable;
use Indatus\Dispatcher\Drivers\Cron\Scheduler;

class MyCommand extends ScheduledCommand {

    //your command name, description etc.

	public function schedule(Schedulable $scheduler)
	{
        //every day at 4:17am
        return $scheduler
            ->daily()
            ->hours(4)
            ->minutes(17);
    }

}
```

## README Contents

* [Features](#features)
* [Installation](#installation)
* [Usage](#usage)
  * [Generating New Scheduled Commands](#new-commands)
  * [Scheduling Existing Commands](#scheduling-commands)
  * [Running Commands As Users](#commands-as-users)
  * [Environment-specific commands](#environment-commands)
* [Drivers](#drivers)
  * [Cron](#Cron)
* [Custom Drivers](#custom-drivers)
* [FAQ](#faq)

<a name="features" />
## Features

 * Schedule artisan commands to run automatically
 * Scheduling is maintained within your version control system
 * Run commands as other users
 * Run commands in certain environments

<a name="installation" />
## Installation

You can install the library via [Composer](http://getcomposer.org) by adding the following line to the **require** block of your *composer.json* file:

````
"indatus/dispatcher": "dev-master"
````

Next run `composer update`.

Add this line to the providers array in your `app/config/app.php` file :

```php
        'Indatus\Dispatcher\ServiceProvider',
```

To use with Cron, see the [Cron driver](#Cron) section.

<a name="usage" />
## Usage

```
scheduled
  scheduled:make              Create a new scheduled artisan command
  scheduled:run               Run scheduled commands
  scheduled:summary           View a summary of all scheduled artisan commands
```

If commands are not visible via `php artisan` then they cannot be scheduled.

<a name="new-commands" />
### Generating New Scheduled Commands

Use `php artisan scheduled:make` to generate a new scheduled command, the same way you would use artisan's `command:make`.

<a name="scheduling-commands" />
### Scheduling Existing Commands

You may either `implement \Indatus\Dispatcher\ScheduledCommandInterface` or follow the below steps.

1. `extend \Indatus\Dispatcher\ScheduledCommand`
2. Add use statements to your command.  If you're using a custom driver you will use a different `Scheduler` class.
```php
use Indatus\Dispatcher\ScheduledCommand;
use Indatus\Dispatcher\Schedulable;
use Indatus\Dispatcher\Drivers\Cron\Scheduler;
```
3. Implement schedule():
```php
	/**
	 * When a command should run
	 *
	 * @param Scheduler $scheduler
	 * @return \Indatus\Dispatcher\Schedulable
	 */
	public function schedule(Schedulable $scheduler)
	{
		return $scheduler;
    }
```

For details and examples on how to schedule, see [Cron](#Cron).

<a name="commands-as-users" />
### Running Commands As Users

You may override `user()` to run a given artisan command as a specific user.  Ensure your `scheduled:run` artisan command is running as root.

```php
    public function user()
    {
        return 'backup';
    }
```

> This feature may not be supported by all drivers.

<a name="environment-commands" />
### Environment-specific commands

You may override `environment()` to ensure your command is only scheduled in specific environments.  It should provide a single environment or an array of environments.

```php
    public function environment()
    {
        return ['development','staging'];
    }
```



<a name="drivers" />
## Drivers

While Cron is the default driver for Dispatcher, it can be used with any scheduling tool that can run artisan commands. See [building custom drivers](#custom-drivers).

<a name="Cron" />
### Cron (Default)

Add the following to your Crontab:

```php
* * * * * php /path/to/artisan scheduled:run 1>> /dev/null 2>&1
```

> If you'd like for scheduled commands to be able to run as different users, be sure to add this to the root Crontab.  Otherwise all commands run as the user whose Crontab you've added this to.

Examples of how to schedule:

```php
	public function schedule(Schedulable $scheduler)
	{
        //every day at 4:17am
        return $scheduler->daily()->hours(4)->minutes(17);
    }
```


```php
	public function schedule(Schedulable $scheduler)
	{
        //every Tuesday/Thursday at 5:03am
        return $scheduler->daysOfTheWeek([
                Scheduler::TUESDAY,
                Scheduler::THURSDAY
            ])->hours(5)->minutes(3);
    }
```

You may also schedule commands via raw Cron expressions

```php
	public function schedule(Schedulable $scheduler)
	{
        //every other day at 3:15am, 4:15am and 5:15am
        return $scheduler->setSchedule(15, [3,4,5], '*/2', '*', '*');
    }
```

<a name="custom-drivers" />
## Custom Drivers

You can build your own drivers or extend a driver that's included.  Create a packagepath such as `\MyApp\ScheduleDriver\` and create two classes:

 * `Scheduler` that `implements Indatus\Dispatcher\Schedulable`.  This class should provide a useful interface for programmers to schedule their commands.
 * `ScheduleService` that `extends \Indatus\Dispatcher\Services\ScheduleService`.  This class contains logic on how to determine if a command is due to run.

Publish the configs using `php artisan config:publish indatus/dispatcher`. Then update your driver configuration to reference the package in which these 2 classes are included (do not include a trailing slash):

```php
{'fraction_non_alphanumeric': 0.11585365853658537, 'fraction_numerical': 0.0068792995622263915, 'mean_word_length': 4.0366141732283465, 'pattern_counts': {'":': 2, '<': 25, '<?xml version=': 0, '>': 55, 'https://': 18, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4371809', 'n_tokens_mistral': 3900, 'n_tokens_neox': 3631, 'n_words': 1264}