forked from modxcms/evolution
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
2,429 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ?? []); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.