Skip to content

Commit

Permalink
Merge pull request #19 from Indatus/develop
Browse files Browse the repository at this point in the history
Advanced Scheduling: Arguments, Options and more
  • Loading branch information
bkuhl committed Apr 9, 2014
2 parents 16e4caf + 9b785c6 commit 3ba6872
Show file tree
Hide file tree
Showing 38 changed files with 995 additions and 377 deletions.
73 changes: 62 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<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\Scheduling\ScheduledCommand;
use Indatus\Dispatcher\Scheduling\Schedulable;
use Indatus\Dispatcher\Drivers\Cron\Scheduler;

class MyCommand extends ScheduledCommand {
Expand All @@ -33,7 +33,8 @@ class MyCommand extends ScheduledCommand {
* [Generating New Scheduled Commands](#new-commands)
* [Scheduling Existing Commands](#scheduling-commands)
* [Running Commands As Users](#commands-as-users)
* [Environment-specific commands](#environment-commands)
* [Environment-Specific Commands](#environment-commands)
* [Advanced Scheduling](#advanced-scheduling)
* [Drivers](#drivers)
* [Cron](#Cron)
* [Custom Drivers](#custom-drivers)
Expand Down Expand Up @@ -86,13 +87,13 @@ Use `php artisan scheduled:make` to generate a new scheduled command, the same w
<a name="scheduling-commands" />
### Scheduling Existing Commands

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

1. `extend \Indatus\Dispatcher\ScheduledCommand`
1. `extend \Indatus\Dispatcher\Scheduling\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\Scheduling\ScheduledCommand;
use Indatus\Dispatcher\Scheduling\Schedulable;
use Indatus\Dispatcher\Drivers\Cron\Scheduler;
```
3. Implement schedule():
Expand All @@ -101,7 +102,7 @@ use Indatus\Dispatcher\Drivers\Cron\Scheduler;
* When a command should run
*
* @param Scheduler $scheduler
* @return \Indatus\Dispatcher\Schedulable
* @return \Indatus\Dispatcher\Scheduling\Schedulable
*/
public function schedule(Schedulable $scheduler)
{
Expand All @@ -126,7 +127,7 @@ You may override `user()` to run a given artisan command as a specific user. En
> This feature may not be supported by all drivers.
<a name="environment-commands" />
### Environment-specific 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.

Expand All @@ -137,7 +138,57 @@ You may override `environment()` to ensure your command is only scheduled in spe
}
```

<a name="advanced-scheduling" />
### Advanced scheduling

> These examples utilize the [cron](#Cron) driver.
You may schedule a given command to to run at multiple times by `schedule()` returning multiple `Schedulable` instances.

```php
public function schedule(Schedulable $scheduler)
{
return [
// 5am Mon-Fri
$scheduler->everyWeekday()->hours(5),

// 2am every Saturday
App::make(get_class($scheduler))
->daysOfTheWeek(Scheduler::SATURDAY)
->hours(2)
];
}
```

You may also schedule a command to run with arguments and options.

```php

public function schedule(Schedulable $scheduler)
{
return [
// equivalent to: php /path/to/artisan command:name /path/to/file
$scheduler->args(['/path/to/file'])
->everyWeekday()
->hours(5),

// equivalent to: php /path/to/artisan command:name /path/to/file --force --toDelete="expired" --exclude="admins" --exclude="developers"
$scheduler->args(['/path/to/file'])
->opts([
'force',
'toDelete' => 'expired',
'exclude' => [
'admins',
'developers'
]
])
->daysOfTheMonth([1, 15])
->hours(2)
];
}
```

> Both `args()` and `opts()`, whichever is called first, will internally create a new `Schedulable` instance for you so you don't need to `App::make()`.
<a name="drivers" />
## Drivers
Expand Down Expand Up @@ -192,7 +243,7 @@ You may also schedule commands via raw Cron expressions

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.
* `Scheduler` that `implements Indatus\Dispatcher\Scheduling\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):
Expand All @@ -206,7 +257,7 @@ Publish the configs using `php artisan config:publish indatus/dispatcher`. Then

**I need to deploy to multiple servers representing a single environment. How can I be sure my command is only run by a single server and not run on each server?**

Schedule `scheduled:run` to use [rcron](https://code.google.com/p/rcron/):
Schedule `scheduled:run` to run every minute with [rcron](https://code.google.com/p/rcron/):

```php
* * * * * /usr/bin/rcron php /path/to/artisan scheduled:run 1>> /dev/null 2>&1
Expand Down
20 changes: 13 additions & 7 deletions src/Indatus/Dispatcher/BackgroundProcessRunner.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php namespace Indatus\Dispatcher;

/**
* This file is part of Dispatcher
Expand All @@ -8,10 +8,11 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Indatus\Dispatcher;

use App;
use Indatus\Dispatcher\Scheduling\ScheduledCommand;
use Indatus\Dispatcher\Services\CommandService;
use Symfony\Component\Process\Process;

class BackgroundProcessRunner
{
Expand All @@ -29,13 +30,18 @@ public function __construct(CommandService $commandService)
/**
* Run a scheduled command
*
* @param ScheduledCommand $scheduledCommand
* @param ScheduledCommand $scheduledCommand
* @param array $arguments
* @param array $options
* @return bool
*/
public function run(ScheduledCommand $scheduledCommand)
{
exec($this->commandService->getRunCommand($scheduledCommand));
public function run(
ScheduledCommand $scheduledCommand,
array $arguments = array(),
array $options = array()
) {
exec($this->commandService->getRunCommand($scheduledCommand, $arguments, $options));

return true;
}
}
}
8 changes: 3 additions & 5 deletions src/Indatus/Dispatcher/Commands/Make.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php namespace Indatus\Dispatcher\Commands;

/**
* This file is part of Dispatcher
Expand All @@ -8,7 +8,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Indatus\Dispatcher\Commands;

use Config;
use Illuminate\Console\Command;
Expand All @@ -17,7 +16,6 @@
/**
* View a summary for all scheduled artisan commands
* @author Ben Kuhl <[email protected]>
* @package Indatus\Dispatcher\Commands
*/
class Make extends CommandMakeCommand
{
Expand Down Expand Up @@ -53,8 +51,8 @@ protected function writeCommand($file, $stub) {
protected function extendStub($stub)
{
$replacements = array(
'use Illuminate\Console\Command' => "use Indatus\\Dispatcher\\ScheduledCommand;\n".
"use Indatus\\Dispatcher\\Schedulable;\n".
'use Illuminate\Console\Command' => "use Indatus\\Dispatcher\\Scheduling\\ScheduledCommand;\n".
"use Indatus\\Dispatcher\\Scheduling\\Schedulable;\n".
"use Indatus\\Dispatcher\\Drivers\\".ucwords(Config::get('dispatcher::driver'))."\\Scheduler",
'extends Command {' => 'extends ScheduledCommand {',
'parent::__construct();' => $this->getStub()
Expand Down
5 changes: 2 additions & 3 deletions src/Indatus/Dispatcher/Commands/Run.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php namespace Indatus\Dispatcher\Commands;

/**
* This file is part of Dispatcher
Expand All @@ -8,15 +8,13 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Indatus\Dispatcher\Commands;

use Illuminate\Console\Command;
use Indatus\Dispatcher\Services\CommandService;

/**
* Run any commands that should be run
* @author Ben Kuhl <[email protected]>
* @package Indatus\Dispatcher\Commands
*/
class Run extends Command
{
Expand Down Expand Up @@ -52,6 +50,7 @@ public function __construct(CommandService $commandService)
*/
public function fire()
{

$this->commandService->runDue();
}
}
3 changes: 1 addition & 2 deletions src/Indatus/Dispatcher/Commands/ScheduleSummary.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php namespace Indatus\Dispatcher\Commands;

/**
* This file is part of Dispatcher
Expand All @@ -8,7 +8,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Indatus\Dispatcher\Commands;

use Illuminate\Console\Command;
use Indatus\Dispatcher\Services\ScheduleService;
Expand Down
2 changes: 1 addition & 1 deletion src/Indatus/Dispatcher/Commands/stubs/command.stub
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ parent::__construct();
* When a command should run
*
* @param Scheduler $scheduler
* @return \Indatus\Dispatcher\Schedulable
* @return \Indatus\Dispatcher\Scheduling\Schedulable
*/
public function schedule(Schedulable $scheduler)
{
Expand Down
49 changes: 42 additions & 7 deletions src/Indatus/Dispatcher/ConfigResolver.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php namespace Indatus\Dispatcher;

/**
* This file is part of Dispatcher
Expand All @@ -8,22 +8,57 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Indatus\Dispatcher;

use App;
use Config;

class ConfigResolver
{

public function resolveDriverClass($className)
/**
* Resolve a class based on the driver configuration
*
* @return \Indatus\Dispatcher\Scheduling\Schedulable
*/
public function resolveSchedulerClass()
{
try {
return App::make(Config::get('dispatcher::driver').'\\'.$className);
return App::make(
Config::get('dispatcher::driver').'\\Scheduler', array(
$this
)
);
} catch (\ReflectionException $e) {
$driver = ucwords(strtolower(Config::get('dispatcher::driver')));
return App::make('Indatus\Dispatcher\Drivers\\'.$driver.'\\'.$className);
return App::make(
'Indatus\Dispatcher\Drivers\\'.$this->getDriver().'\\Scheduler', array(
$this
)
);
}
}

}
/**
* Resolve a class based on the driver configuration
*
* @return \Indatus\Dispatcher\Scheduling\ScheduleService
*/
public function resolveServiceClass()
{
try {
return App::make(Config::get('dispatcher::driver').'\\ScheduleService');
} catch (\ReflectionException $e) {
return App::make('Indatus\Dispatcher\Drivers\\'.$this->getDriver().'\\ScheduleService');
}
}

/**
* Get the dispatcher driver class
*
* @return string
*/
public function getDriver()
{
return ucwords(strtolower(Config::get('dispatcher::driver')));
}

}
Loading

0 comments on commit 3ba6872

Please sign in to comment.