Skip to content

Commit

Permalink
[ADD] Schedule.
Browse files Browse the repository at this point in the history
  • Loading branch information
Seiger committed Nov 20, 2022
1 parent 2625c8d commit c4ce08e
Show file tree
Hide file tree
Showing 19 changed files with 2,429 additions and 1 deletion.
3 changes: 2 additions & 1 deletion core/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
"james-heinrich/phpthumb": "1.*",
"evolutioncms-services/user-manager": "1.*",
"evolutioncms-services/document-manager": "1.*",
"evolution-cms/salo": "1.*"
"evolution-cms/salo": "1.*",
"dragonmantank/cron-expression": "^3.3"
},
"require-dev": {
"roave/security-advisories": "dev-master"
Expand Down
55 changes: 55 additions & 0 deletions core/src/Console/Scheduling/ScheduleListCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php namespace EvolutionCMS\Console\Scheduling;

use Cron\CronExpression;
use DateTimeZone;
use Illuminate\Console\Command;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Carbon;

/** @see: https://github.com/laravel/framework/blob/8.x/src/Illuminate/Console/Scheduling/ScheduleListCommand.php */
class ScheduleListCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $signature = 'schedule:list {--timezone= : The timezone that times should be displayed in}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'List the scheduled commands';

/**
* Execute the console command.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*
* @throws \Exception
*/
public function handle(Schedule $schedule)
{
foreach ($schedule->events() as $event) {
$rows[] = [
$event->command,
$event->expression,
$event->description,
(new CronExpression($event->expression))
->getNextRunDate(Carbon::now()->setTimezone($event->timezone))
->setTimezone(new DateTimeZone($event->timezone))
->format('Y-m-d H:i:s P'),
];
}

$this->table([
'Command',
'Interval',
'Description',
'Next Due',
], $rows ?? []);
}
}
157 changes: 157 additions & 0 deletions core/src/Console/Scheduling/ScheduleRunCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php namespace EvolutionCMS\Console\Scheduling;

use Illuminate\Console\Command;
use Illuminate\Console\Events\ScheduledTaskFailed;
use Illuminate\Console\Events\ScheduledTaskFinished;
use Illuminate\Console\Events\ScheduledTaskSkipped;
use Illuminate\Console\Events\ScheduledTaskStarting;
use EvolutionCMS\ExceptionHandler;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Facades\Date;
use Throwable;

/** @see: https://github.com/laravel/framework/blob/8.x/src/Illuminate/Console/Scheduling/ScheduleRunCommand.php */
class ScheduleRunCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'schedule:run';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Run the scheduled commands';

/**
* The schedule instance.
*
* @var \Illuminate\Console\Scheduling\Schedule
*/
protected $schedule;

/**
* The 24 hour timestamp this scheduler command started running.
*
* @var \Illuminate\Support\Carbon
*/
protected $startedAt;

/**
* Check if any events ran.
*
* @var bool
*/
protected $eventsRan = false;

/**
* The event dispatcher.
*
* @var \Illuminate\Contracts\Events\Dispatcher
*/
protected $dispatcher;

/**
* The exception handler.
*
* @var \Illuminate\Contracts\Debug\ExceptionHandler
*/
protected $handler;

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
$this->startedAt = Date::now();

parent::__construct();
}

/**
* Execute the console command.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
* @param \Illuminate\Contracts\Debug\ExceptionHandler $handler
* @return void
*/
public function handle(Schedule $schedule, Dispatcher $dispatcher, ExceptionHandler $handler)
{
$this->schedule = $schedule;
$this->dispatcher = $dispatcher;
$this->handler = $handler;

foreach ($this->schedule->dueEvents($this->laravel) as $event) {
if (! $event->filtersPass($this->laravel)) {
$this->dispatcher->dispatch(new ScheduledTaskSkipped($event));

continue;
}

if ($event->onOneServer) {
$this->runSingleServerEvent($event);
} else {
$this->runEvent($event);
}

$this->eventsRan = true;
}

if (! $this->eventsRan) {
$this->info('No scheduled commands are ready to run.');
}
}

/**
* Run the given single server event.
*
* @param \Illuminate\Console\Scheduling\Event $event
* @return void
*/
protected function runSingleServerEvent($event)
{
if ($this->schedule->serverShouldRun($event, $this->startedAt)) {
$this->runEvent($event);
} else {
$this->line('<info>Skipping command (has already run on another server):</info> '.$event->getSummaryForDisplay());
}
}

/**
* Run the given event.
*
* @param \Illuminate\Console\Scheduling\Event $event
* @return void
*/
protected function runEvent($event)
{
$this->line('<info>['.date('c').'] Running scheduled command:</info> '.$event->getSummaryForDisplay());

$this->dispatcher->dispatch(new ScheduledTaskStarting($event));

$start = microtime(true);

try {
$event->run($this->laravel);

$this->dispatcher->dispatch(new ScheduledTaskFinished(
$event,
round(microtime(true) - $start, 2)
));

$this->eventsRan = true;
} catch (Throwable $e) {
$this->dispatcher->dispatch(new ScheduledTaskFailed($event, $e));

$this->handler->report($e);
}
}
}
72 changes: 72 additions & 0 deletions core/src/Providers/ArtisanServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<?php namespace EvolutionCMS\Providers;

use EvolutionCMS\Console;
use EvolutionCMS\Console\Scheduling\ScheduleListCommand;
use EvolutionCMS\Console\Scheduling\ScheduleRunCommand;
use Illuminate\Console\Scheduling\ScheduleClearCacheCommand;
use Illuminate\Console\Scheduling\ScheduleFinishCommand;
use Illuminate\Console\Scheduling\ScheduleTestCommand;
use Illuminate\Console\Scheduling\ScheduleWorkCommand;
use Illuminate\Support\ServiceProvider;
use Illuminate\Database\Console\Seeds\SeedCommand;
use Illuminate\Database\Console\Migrations\MigrateCommand;
Expand Down Expand Up @@ -48,6 +54,12 @@ class ArtisanServiceProvider extends ServiceProvider
'MigrateRollback' => 'command.migrate.rollback',
'MigrateStatus' => 'command.migrate.status',
'Seed' => 'command.seed',
'ScheduleFinish' => ScheduleFinishCommand::class,
'ScheduleList' => ScheduleListCommand::class,
'ScheduleRun' => ScheduleRunCommand::class,
'ScheduleClearCache' => ScheduleClearCacheCommand::class,
'ScheduleTest' => ScheduleTestCommand::class,
'ScheduleWork' => ScheduleWorkCommand::class,
'ViewClear' => 'command.view.clear',
'ListsDoc' => 'command.lists.doc',
'ListsTv' => 'command.lists.tv',
Expand Down Expand Up @@ -235,6 +247,66 @@ protected function registerSeedCommand()
});
}

/**
* Register the command.
*
* @return void
*/
protected function registerScheduleClearCacheCommand()
{
$this->app->singleton(ScheduleClearCacheCommand::class);
}

/**
* Register the command.
*
* @return void
*/
protected function registerScheduleFinishCommand()
{
$this->app->singleton(ScheduleFinishCommand::class);
}

/**
* Register the command.
*
* @return void
*/
protected function registerScheduleListCommand()
{
$this->app->singleton(ScheduleListCommand::class);
}

/**
* Register the command.
*
* @return void
*/
protected function registerScheduleRunCommand()
{
$this->app->singleton(ScheduleRunCommand::class);
}

/**
* Register the command.
*
* @return void
*/
protected function registerScheduleTestCommand()
{
$this->app->singleton(ScheduleTestCommand::class);
}

/**
* Register the command.
*
* @return void
*/
protected function registerScheduleWorkCommand()
{
$this->app->singleton(ScheduleWorkCommand::class);
}

/**
* Register the command.
*
Expand Down
Loading

0 comments on commit c4ce08e

Please sign in to comment.