Skip to content

Commit ed16de4

Browse files
committed
wip : show upcoming schedule for each scheduled task in cli
1 parent dbbc135 commit ed16de4

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

resources/views/tasks/form.blade.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,22 @@
3434
<p class="tcdanger ft14">{{$errors->first('command')}}</p>
3535
@endif
3636
</div>
37+
</div>
38+
<div class="frame mb2">
39+
<p class="blk2 ft15 lh2 basic-text tar">
40+
<label for="command">Timezone</label>
41+
</p>
42+
<div class="blk6">
43+
<select id="timezone" name="timezone" class="form-control" placeholder="Click here to select one of the available commands">
44+
@foreach ($timezones as $key => $timezone)
45+
<option value="{{$timezone}}" {{old('timezone', $task->timezone) == $timezone ? 'selected' : ''}}>{{$timezone}}</option>
46+
@endforeach
47+
</select>
3748

49+
@if($errors->has('command'))
50+
<p class="tcdanger ft14">{{$errors->first('command')}}</p>
51+
@endif
52+
</div>
3853
</div>
3954
<div class="frame mb2">
4055
<p class="blk2 ft15 lh2 basic-text tar"></p>

resources/views/tasks/view.blade.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
Description<br>
1919
Command<br>
2020
Cron Expression<br>
21+
Timezone<br>
2122
Created @<br>
2223
Updated @<br>
2324
Notify @<br>
@@ -27,6 +28,7 @@
2728
{{$task->description}}<br>
2829
{{$task->command}}<br>
2930
{{$task->cron ? $task->cron : 'Frequency'}}<br>
31+
{{$task->timezone}}<br>
3032
{{$task->created_at->toDateTimeString()}}<br>
3133
{{$task->updated_at->toDateTimeString()}}<br>
3234
{{$task->notification_email_address}}<br>

src/Console/Commands/ListSchedule.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Studio\Totem\Console\Commands;
44

5+
use Carbon\Carbon;
6+
use Cron\CronExpression;
57
use Illuminate\Console\Command;
68
use Illuminate\Console\Scheduling\Schedule;
79

@@ -49,17 +51,36 @@ public function handle()
4951
if (count($this->schedule->events()) > 0) {
5052
$events = collect($this->schedule->events())->map(function ($event) {
5153
return [
52-
'command' => ltrim(str_after($event->command, "'artisan'")),
53-
'schedule' => $event->expression,
54+
'command' => ltrim(str_after($event->command, "'artisan'")),
55+
'schedule' => $event->expression,
56+
'upcoming' => $this->upcoming($event),
57+
'description' => $event->description,
58+
'timezone' => $event->timezone
5459
];
5560
});
5661

5762
$this->table(
58-
['Command', 'Schedule'],
63+
['Command', 'Schedule', 'Upcoming', 'Description', 'Timezone'],
5964
$events
6065
);
6166
} else {
6267
$this->info('No Scheduled Commands Found');
6368
}
6469
}
70+
71+
/**
72+
* Get Upcoming schedule.
73+
*
74+
* @return bool
75+
*/
76+
protected function upcoming($event)
77+
{
78+
$date = Carbon::now();
79+
80+
if ($event->timezone) {
81+
$date->setTimezone($event->timezone);
82+
}
83+
84+
return (CronExpression::factory($event->expression)->getNextRunDate($date->toDateTimeString()))->format('Y-m-d H:i:s');
85+
}
6586
}

src/Console/Kernel.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public function __construct(Application $app, Dispatcher $events, TaskInterface
3939
protected function schedule(Schedule $schedule)
4040
{
4141
$this->prepareSchedule($schedule);
42+
// $schedule->command('inspire')
43+
// ->hourly()
44+
// ->timezone('America/Chicago');
4245

4346
parent::schedule($schedule);
4447
}
@@ -63,6 +66,8 @@ public function prepareSchedule($schedule)
6366
if ($command) {
6467
$event = $schedule->command($command->getName());
6568
$event->cron($task->cron)
69+
->name($command->getDescription())
70+
->timezone($task->timezone)
6671
->before(function () use ($task, $event) {
6772
$event->start = microtime(true);
6873
Executing::dispatch($task);
@@ -71,6 +76,7 @@ public function prepareSchedule($schedule)
7176
Executed::dispatch($task, $event);
7277
})
7378
->sendOutputTo(storage_path('logs/schedule-'.sha1($event->mutexName()).'.log'));
79+
7480
if ($task->notification_email_address) {
7581
$event->emailOutputTo($task->notification_email_address);
7682
}

src/Http/Controllers/TasksController.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function create()
5151
return view('totem::tasks.form', [
5252
'task' => new Task,
5353
'commands' => $this->kernel->getCommands(),
54+
'timezones' => timezone_identifiers_list(),
5455
]);
5556
}
5657

@@ -85,8 +86,9 @@ public function show($task)
8586
public function edit($task)
8687
{
8788
return view('totem::tasks.form', [
88-
'task' => $task,
89-
'commands' => $this->kernel->getCommands(),
89+
'task' => $task,
90+
'commands' => $this->kernel->getCommands(),
91+
'timezones' => timezone_identifiers_list(),
9092
]);
9193
}
9294

0 commit comments

Comments
 (0)