Skip to content

Commit

Permalink
Scheduled commands may now implement ScheduledCommandInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Kuhl committed Mar 24, 2014
1 parent d5290df commit 3add68a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Use `php artisan scheduled:make` to generate a new scheduled command, the same w
<a name="scheduling-commands" />
### Scheduling Existing Commands

Simply `extend \Indatus\Dispatcher\ScheduledCommand` and implement the `schedule()` method within your command. This method is injected with a `Schedulable` which provides a simple interface for scheduling your commands.
Simply `extend \Indatus\Dispatcher\ScheduledCommand` and implement the `schedule()` method within your command or `implement \Indatus\Dispatcher\ScheduledCommandInterface`. This method is injected with a `Schedulable` which provides a simple interface for scheduling your commands.

```php
public function schedule(Schedulable $scheduler)
Expand Down
14 changes: 5 additions & 9 deletions src/Indatus/Dispatcher/Drivers/Cron/ScheduleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@

use App;
use Cron\CronExpression;
use Indatus\Dispatcher\ScheduledCommand;
use Indatus\Dispatcher\ScheduledCommandInterface;

class ScheduleService extends \Indatus\Dispatcher\Services\ScheduleService {

/**
* Determine if a command is due to be run
* @param ScheduledCommand $command
* @param ScheduledCommandInterface $command
* @return bool
*/
public function isDue(ScheduledCommand $command)
public function isDue(ScheduledCommandInterface $command)
{
$scheduler = App::make('Indatus\Dispatcher\Schedulable');
$cron = CronExpression::factory($command->schedule($scheduler)->getSchedule());
Expand All @@ -34,11 +34,9 @@ public function isDue(ScheduledCommand $command)
public function printSummary()
{
$this->table->setHeaders(array('Environment(s)', 'Name', 'Minute', 'Hour', 'Day of Month', 'Month', 'Day of Week', 'Run as'));
/** @var $command \Indatus\Dispatcher\ScheduledCommand */
$commands = 0;
$activeCommands = 0;
/** @var $command \Indatus\Dispatcher\ScheduledCommandInterface */
foreach ($this->getScheduledCommands() as $command) {
/** @var $command \Indatus\Dispatcher\ScheduledCommand */
/** @var $command \Indatus\Dispatcher\ScheduledCommandInterface */
$scheduler = $command->schedule(App::make('Indatus\Dispatcher\Schedulable'));

$this->table->addRow(array(
Expand All @@ -51,8 +49,6 @@ public function printSummary()
$scheduler->getScheduleDayOfWeek(),
$command->user()
));
$commands++;
$activeCommands++;
}

//sort by first column
Expand Down
10 changes: 1 addition & 9 deletions src/Indatus/Dispatcher/ScheduledCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use App;
use Illuminate\Console\Command;

abstract class ScheduledCommand extends Command
abstract class ScheduledCommand extends Command implements ScheduledCommandInterface
{


Expand All @@ -24,14 +24,6 @@ abstract class ScheduledCommand extends Command
*/
protected $name = 'scheduledCommand';

/**
* When a command should run
*
* @param Schedulable $scheduler
* @return \Indatus\Dispatcher\Schedulable
*/
abstract public function schedule(Schedulable $scheduler);

/**
* User to run the command as
*
Expand Down
36 changes: 36 additions & 0 deletions src/Indatus/Dispatcher/ScheduledCommandInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* @author Ben Kuhl <[email protected]>
*/
namespace Indatus\Dispatcher;

interface ScheduledCommandInterface
{
/**
* User to run the command as
* @return string Defaults to false to run as default user
*/
public function user();

/**
* When a command should run
* @param Schedulable $scheduler
* @return \Indatus\Dispatcher\Schedulable
*/
public function schedule(Schedulable $scheduler);

/**
* Environment(s) under which the given command should run
* Defaults to '*' for all environments
* @return string
*/
public function environment();

/**
* Checks whether the command is enabled or not in the current environment
* Override this to check for x or y and return false if the command can not
* run properly under the current conditions.
* @return Boolean
*/
public function isEnabled();
}
8 changes: 4 additions & 4 deletions src/Indatus/Dispatcher/Services/ScheduleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use App;
use Artisan;
use Indatus\Dispatcher\ScheduledCommand;
use Indatus\Dispatcher\ScheduledCommandInterface;
use Indatus\Dispatcher\Table;

abstract class ScheduleService
Expand All @@ -28,10 +28,10 @@ public function __construct(Table $table)

/**
* Determine if a command is due to be run
* @param ScheduledCommand $command
* @param ScheduledCommandInterface $command
* @return bool
*/
abstract public function isDue(ScheduledCommand $command);
abstract public function isDue(ScheduledCommandInterface $command);

/**
* Get all commands that are scheduled
Expand All @@ -42,7 +42,7 @@ public function getScheduledCommands()
{
$scheduledCommands = array();
foreach (Artisan::all() as $command) {
if ($command instanceOf ScheduledCommand) {
if ($command instanceOf ScheduledCommandInterface) {
$scheduledCommands[] = $command;
}
}
Expand Down

0 comments on commit 3add68a

Please sign in to comment.