Skip to content

Commit c4ce08e

Browse files
committed
[ADD] Schedule.
1 parent 2625c8d commit c4ce08e

19 files changed

+2429
-1
lines changed

core/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@
6767
"james-heinrich/phpthumb": "1.*",
6868
"evolutioncms-services/user-manager": "1.*",
6969
"evolutioncms-services/document-manager": "1.*",
70-
"evolution-cms/salo": "1.*"
70+
"evolution-cms/salo": "1.*",
71+
"dragonmantank/cron-expression": "^3.3"
7172
},
7273
"require-dev": {
7374
"roave/security-advisories": "dev-master"
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php namespace EvolutionCMS\Console\Scheduling;
2+
3+
use Cron\CronExpression;
4+
use DateTimeZone;
5+
use Illuminate\Console\Command;
6+
use Illuminate\Console\Scheduling\Schedule;
7+
use Illuminate\Support\Carbon;
8+
9+
/** @see: https://github.com/laravel/framework/blob/8.x/src/Illuminate/Console/Scheduling/ScheduleListCommand.php */
10+
class ScheduleListCommand extends Command
11+
{
12+
/**
13+
* The console command name.
14+
*
15+
* @var string
16+
*/
17+
protected $signature = 'schedule:list {--timezone= : The timezone that times should be displayed in}';
18+
19+
/**
20+
* The console command description.
21+
*
22+
* @var string
23+
*/
24+
protected $description = 'List the scheduled commands';
25+
26+
/**
27+
* Execute the console command.
28+
*
29+
* @param \Illuminate\Console\Scheduling\Schedule $schedule
30+
* @return void
31+
*
32+
* @throws \Exception
33+
*/
34+
public function handle(Schedule $schedule)
35+
{
36+
foreach ($schedule->events() as $event) {
37+
$rows[] = [
38+
$event->command,
39+
$event->expression,
40+
$event->description,
41+
(new CronExpression($event->expression))
42+
->getNextRunDate(Carbon::now()->setTimezone($event->timezone))
43+
->setTimezone(new DateTimeZone($event->timezone))
44+
->format('Y-m-d H:i:s P'),
45+
];
46+
}
47+
48+
$this->table([
49+
'Command',
50+
'Interval',
51+
'Description',
52+
'Next Due',
53+
], $rows ?? []);
54+
}
55+
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php namespace EvolutionCMS\Console\Scheduling;
2+
3+
use Illuminate\Console\Command;
4+
use Illuminate\Console\Events\ScheduledTaskFailed;
5+
use Illuminate\Console\Events\ScheduledTaskFinished;
6+
use Illuminate\Console\Events\ScheduledTaskSkipped;
7+
use Illuminate\Console\Events\ScheduledTaskStarting;
8+
use EvolutionCMS\ExceptionHandler;
9+
use Illuminate\Console\Scheduling\Schedule;
10+
use Illuminate\Contracts\Events\Dispatcher;
11+
use Illuminate\Support\Facades\Date;
12+
use Throwable;
13+
14+
/** @see: https://github.com/laravel/framework/blob/8.x/src/Illuminate/Console/Scheduling/ScheduleRunCommand.php */
15+
class ScheduleRunCommand extends Command
16+
{
17+
/**
18+
* The console command name.
19+
*
20+
* @var string
21+
*/
22+
protected $name = 'schedule:run';
23+
24+
/**
25+
* The console command description.
26+
*
27+
* @var string
28+
*/
29+
protected $description = 'Run the scheduled commands';
30+
31+
/**
32+
* The schedule instance.
33+
*
34+
* @var \Illuminate\Console\Scheduling\Schedule
35+
*/
36+
protected $schedule;
37+
38+
/**
39+
* The 24 hour timestamp this scheduler command started running.
40+
*
41+
* @var \Illuminate\Support\Carbon
42+
*/
43+
protected $startedAt;
44+
45+
/**
46+
* Check if any events ran.
47+
*
48+
* @var bool
49+
*/
50+
protected $eventsRan = false;
51+
52+
/**
53+
* The event dispatcher.
54+
*
55+
* @var \Illuminate\Contracts\Events\Dispatcher
56+
*/
57+
protected $dispatcher;
58+
59+
/**
60+
* The exception handler.
61+
*
62+
* @var \Illuminate\Contracts\Debug\ExceptionHandler
63+
*/
64+
protected $handler;
65+
66+
/**
67+
* Create a new command instance.
68+
*
69+
* @return void
70+
*/
71+
public function __construct()
72+
{
73+
$this->startedAt = Date::now();
74+
75+
parent::__construct();
76+
}
77+
78+
/**
79+
* Execute the console command.
80+
*
81+
* @param \Illuminate\Console\Scheduling\Schedule $schedule
82+
* @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
83+
* @param \Illuminate\Contracts\Debug\ExceptionHandler $handler
84+
* @return void
85+
*/
86+
public function handle(Schedule $schedule, Dispatcher $dispatcher, ExceptionHandler $handler)
87+
{
88+
$this->schedule = $schedule;
89+
$this->dispatcher = $dispatcher;
90+
$this->handler = $handler;
91+
92+
foreach ($this->schedule->dueEvents($this->laravel) as $event) {
93+
if (! $event->filtersPass($this->laravel)) {
94+
$this->dispatcher->dispatch(new ScheduledTaskSkipped($event));
95+
96+
continue;
97+
}
98+
99+
if ($event->onOneServer) {
100+
$this->runSingleServerEvent($event);
101+
} else {
102+
$this->runEvent($event);
103+
}
104+
105+
$this->eventsRan = true;
106+
}
107+
108+
if (! $this->eventsRan) {
109+
$this->info('No scheduled commands are ready to run.');
110+
}
111+
}
112+
113+
/**
114+
* Run the given single server event.
115+
*
116+
* @param \Illuminate\Console\Scheduling\Event $event
117+
* @return void
118+
*/
119+
protected function runSingleServerEvent($event)
120+
{
121+
if ($this->schedule->serverShouldRun($event, $this->startedAt)) {
122+
$this->runEvent($event);
123+
} else {
124+
$this->line('<info>Skipping command (has already run on another server):</info> '.$event->getSummaryForDisplay());
125+
}
126+
}
127+
128+
/**
129+
* Run the given event.
130+
*
131+
* @param \Illuminate\Console\Scheduling\Event $event
132+
* @return void
133+
*/
134+
protected function runEvent($event)
135+
{
136+
$this->line('<info>['.date('c').'] Running scheduled command:</info> '.$event->getSummaryForDisplay());
137+
138+
$this->dispatcher->dispatch(new ScheduledTaskStarting($event));
139+
140+
$start = microtime(true);
141+
142+
try {
143+
$event->run($this->laravel);
144+
145+
$this->dispatcher->dispatch(new ScheduledTaskFinished(
146+
$event,
147+
round(microtime(true) - $start, 2)
148+
));
149+
150+
$this->eventsRan = true;
151+
} catch (Throwable $e) {
152+
$this->dispatcher->dispatch(new ScheduledTaskFailed($event, $e));
153+
154+
$this->handler->report($e);
155+
}
156+
}
157+
}

core/src/Providers/ArtisanServiceProvider.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<?php namespace EvolutionCMS\Providers;
22

33
use EvolutionCMS\Console;
4+
use EvolutionCMS\Console\Scheduling\ScheduleListCommand;
5+
use EvolutionCMS\Console\Scheduling\ScheduleRunCommand;
6+
use Illuminate\Console\Scheduling\ScheduleClearCacheCommand;
7+
use Illuminate\Console\Scheduling\ScheduleFinishCommand;
8+
use Illuminate\Console\Scheduling\ScheduleTestCommand;
9+
use Illuminate\Console\Scheduling\ScheduleWorkCommand;
410
use Illuminate\Support\ServiceProvider;
511
use Illuminate\Database\Console\Seeds\SeedCommand;
612
use Illuminate\Database\Console\Migrations\MigrateCommand;
@@ -48,6 +54,12 @@ class ArtisanServiceProvider extends ServiceProvider
4854
'MigrateRollback' => 'command.migrate.rollback',
4955
'MigrateStatus' => 'command.migrate.status',
5056
'Seed' => 'command.seed',
57+
'ScheduleFinish' => ScheduleFinishCommand::class,
58+
'ScheduleList' => ScheduleListCommand::class,
59+
'ScheduleRun' => ScheduleRunCommand::class,
60+
'ScheduleClearCache' => ScheduleClearCacheCommand::class,
61+
'ScheduleTest' => ScheduleTestCommand::class,
62+
'ScheduleWork' => ScheduleWorkCommand::class,
5163
'ViewClear' => 'command.view.clear',
5264
'ListsDoc' => 'command.lists.doc',
5365
'ListsTv' => 'command.lists.tv',
@@ -235,6 +247,66 @@ protected function registerSeedCommand()
235247
});
236248
}
237249

250+
/**
251+
* Register the command.
252+
*
253+
* @return void
254+
*/
255+
protected function registerScheduleClearCacheCommand()
256+
{
257+
$this->app->singleton(ScheduleClearCacheCommand::class);
258+
}
259+
260+
/**
261+
* Register the command.
262+
*
263+
* @return void
264+
*/
265+
protected function registerScheduleFinishCommand()
266+
{
267+
$this->app->singleton(ScheduleFinishCommand::class);
268+
}
269+
270+
/**
271+
* Register the command.
272+
*
273+
* @return void
274+
*/
275+
protected function registerScheduleListCommand()
276+
{
277+
$this->app->singleton(ScheduleListCommand::class);
278+
}
279+
280+
/**
281+
* Register the command.
282+
*
283+
* @return void
284+
*/
285+
protected function registerScheduleRunCommand()
286+
{
287+
$this->app->singleton(ScheduleRunCommand::class);
288+
}
289+
290+
/**
291+
* Register the command.
292+
*
293+
* @return void
294+
*/
295+
protected function registerScheduleTestCommand()
296+
{
297+
$this->app->singleton(ScheduleTestCommand::class);
298+
}
299+
300+
/**
301+
* Register the command.
302+
*
303+
* @return void
304+
*/
305+
protected function registerScheduleWorkCommand()
306+
{
307+
$this->app->singleton(ScheduleWorkCommand::class);
308+
}
309+
238310
/**
239311
* Register the command.
240312
*

0 commit comments

Comments
 (0)