From 3add68ae525def99d5b210960bee15dfbce4d91e Mon Sep 17 00:00:00 2001 From: Ben Kuhl Date: Sun, 23 Mar 2014 22:30:10 -0400 Subject: [PATCH] Scheduled commands may now implement ScheduledCommandInterface --- README.md | 2 +- .../Drivers/Cron/ScheduleService.php | 14 +++----- src/Indatus/Dispatcher/ScheduledCommand.php | 10 +----- .../Dispatcher/ScheduledCommandInterface.php | 36 +++++++++++++++++++ .../Dispatcher/Services/ScheduleService.php | 8 ++--- 5 files changed, 47 insertions(+), 23 deletions(-) create mode 100644 src/Indatus/Dispatcher/ScheduledCommandInterface.php diff --git a/README.md b/README.md index 6c4a49d..a82f1df 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ Use `php artisan scheduled:make` to generate a new scheduled command, the same w ### 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) diff --git a/src/Indatus/Dispatcher/Drivers/Cron/ScheduleService.php b/src/Indatus/Dispatcher/Drivers/Cron/ScheduleService.php index f06000b..dc8aefe 100644 --- a/src/Indatus/Dispatcher/Drivers/Cron/ScheduleService.php +++ b/src/Indatus/Dispatcher/Drivers/Cron/ScheduleService.php @@ -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()); @@ -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( @@ -51,8 +49,6 @@ public function printSummary() $scheduler->getScheduleDayOfWeek(), $command->user() )); - $commands++; - $activeCommands++; } //sort by first column diff --git a/src/Indatus/Dispatcher/ScheduledCommand.php b/src/Indatus/Dispatcher/ScheduledCommand.php index f57c107..dcb8ad2 100644 --- a/src/Indatus/Dispatcher/ScheduledCommand.php +++ b/src/Indatus/Dispatcher/ScheduledCommand.php @@ -13,7 +13,7 @@ use App; use Illuminate\Console\Command; -abstract class ScheduledCommand extends Command +abstract class ScheduledCommand extends Command implements ScheduledCommandInterface { @@ -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 * diff --git a/src/Indatus/Dispatcher/ScheduledCommandInterface.php b/src/Indatus/Dispatcher/ScheduledCommandInterface.php new file mode 100644 index 0000000..d0acc16 --- /dev/null +++ b/src/Indatus/Dispatcher/ScheduledCommandInterface.php @@ -0,0 +1,36 @@ + + */ +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(); +} \ No newline at end of file diff --git a/src/Indatus/Dispatcher/Services/ScheduleService.php b/src/Indatus/Dispatcher/Services/ScheduleService.php index 302f275..5d6ea09 100644 --- a/src/Indatus/Dispatcher/Services/ScheduleService.php +++ b/src/Indatus/Dispatcher/Services/ScheduleService.php @@ -12,7 +12,7 @@ use App; use Artisan; -use Indatus\Dispatcher\ScheduledCommand; +use Indatus\Dispatcher\ScheduledCommandInterface; use Indatus\Dispatcher\Table; abstract class ScheduleService @@ -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 @@ -42,7 +42,7 @@ public function getScheduledCommands() { $scheduledCommands = array(); foreach (Artisan::all() as $command) { - if ($command instanceOf ScheduledCommand) { + if ($command instanceOf ScheduledCommandInterface) { $scheduledCommands[] = $command; } }