Skip to content

Commit

Permalink
Merge pull request #74 from xiCO2k/feat/add-termwind
Browse files Browse the repository at this point in the history
Add Termwind to improve the Command Outputs.
  • Loading branch information
freekmurze authored May 5, 2022
2 parents 658cbbd + 0549fbb commit 0ac79e7
Show file tree
Hide file tree
Showing 22 changed files with 255 additions and 286 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"illuminate/bus": "^9.0",
"lorisleiva/cron-translator": "^0.3.0",
"nesbot/carbon": "^2.41.3",
"nunomaduro/termwind": "^1.9",
"spatie/laravel-package-tools": "^1.9"
},
"require-dev": {
Expand Down
Empty file removed resources/views/.gitkeep
Empty file.
3 changes: 3 additions & 0 deletions resources/views/alert.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="mt-1 mx-2 {{ $class ?? '' }}">
{!! $message !!}
</div>
16 changes: 16 additions & 0 deletions resources/views/components/duplicate-tasks.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@props(['tasks'])
<div class="space-y-1">
<x-schedule-monitor::title>Duplicate Tasks</x-schedule-monitor::title>

<div>These tasks could not be monitored because they have a duplicate name.</div>

<div>
@foreach ($tasks as $task)
<x-schedule-monitor::task :task="$task" />
@endforeach
</div>

<div>
To monitor these tasks you should add <span class="text-yellow font-bold">->monitorName()</span> in the schedule to manually specify a unique name.
</div>
</div>
63 changes: 63 additions & 0 deletions resources/views/components/monitored-tasks.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
@props(['tasks', 'dateFormat', 'usingOhDear'])
<div class="space-y-1">
<x-schedule-monitor::title>Monitored Tasks</x-schedule-monitor::title>

<div class="space-y-1">
@forelse ($tasks as $task)
<div>
<x-schedule-monitor::task :task="$task" />
<div class="ml-2">
<div>
<span>
<span class="w-14 text-gray-500">⇁ Started at:</span>
<span class="date-width">
{{ optional($task->lastRunStartedAt())->format($dateFormat) ?? '--' }}
</span>
</span>
<span class="ml-3">
<span class="w-15 text-gray-500">⇁ Finished at:</span>
<span class="date-width {{ $task->lastRunFinishedTooLate() && $task->lastRunFinishedAt() ? 'text-red' : '' }}">
{{ optional($task->lastRunFinishedAt())->format($dateFormat) ?? '--' }}
</span>
</span>
<br class="xl:hidden">
<span class="xl:ml-3">
<span class="w-14 text-gray-500">⇁ Failed at:</span>
<span class="date-width {{ $task->lastRunFailed() ? 'text-red' : '' }}">
{{ optional($task->lastRunFailedAt())->format($dateFormat) ?? '--' }}
</span>
</span>
<br class="hidden xl:block">
<span class="ml-3 xl:ml-0">
<span class="w-15 xl:w-14 text-gray-500">⇁ Next run:</span>
<span class="date-width">{{ $task->nextRunAt()->format($dateFormat) }}</span>
</span>
<br class="xl:hidden">
<span class="xl:ml-3">
<span class="w-14 xl:w-15 text-gray-500">⇁ Grace time:</span>
<span class="date-width">{{ $task->graceTimeInMinutes() }} minutes</span>
</span>
@if ($usingOhDear)
<span class="ml-3">
<span class="text-gray-500">⇁ Registered at Oh Dear:</span>
@if ($task->isBeingMonitoredAtOhDear())
<span class="ml-1 px-1 bg-green-700 text-white font-bold">Yes</span>
@else
<span class="ml-1 px-1 bg-red-700 text-white font-bold">No</span>
@endif
</span>
@endif
</div>
</div>
</div>
@empty
<div class="text-gray-500 italic">There currently are no tasks being monitored!</div>
@endforelse
</div>
@if ($usingOhDear)
<div>
Some tasks are not registered on <b class="text-white bg-red-700 px-1">oh dear</b>. You will not be notified when they do not run on time. <br>
Run <span class="text-yellow font-bold">php artisan schedule-monitor:sync</span> to register them and receive notifications.
</div>
@endif
</div>
12 changes: 12 additions & 0 deletions resources/views/components/ready-for-monitoring-tasks.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@props(['tasks'])
<div class="space-y-1">
<x-schedule-monitor::title>Run sync to start monitoring</x-schedule-monitor::title>

<div>These tasks will be monitored after running: <span class="text-yellow font-bold">php artisan schedule-monitor:sync</span></div>

<div>
@foreach ($tasks as $task)
<x-schedule-monitor::task :task="$task" />
@endforeach
</div>
</div>
15 changes: 15 additions & 0 deletions resources/views/components/task.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@props(['task'])
<div class="space-x-1">
@if ($task->name())
<span>{{ $task->name() }}</span>
<span class="text-gray-500 lowercase">({{ $task->type() }})</span>
@else
<span>{{ $task->type() }}</span>
@endif
<span class="text-gray-500">
{{ str_repeat('.', (new \Termwind\Terminal)->width() - (
strlen($task->name() . $task->type() . $task->humanReadableCron()) + ($task->name() && $task->type() ? 9 : 6)
)) }}
</span>
<span class="text-gray-500">{{ $task->humanReadableCron() }}</span>
</div>
3 changes: 3 additions & 0 deletions resources/views/components/title.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="bg-blue-600 text-white font-bold px-1">
{{ $slot }}
</div>
14 changes: 14 additions & 0 deletions resources/views/components/unnamed-tasks.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@props(['tasks'])
<div class="space-y-1">
<x-schedule-monitor::title>Unnamed Tasks</x-schedule-monitor::title>

<div>These tasks cannot be monitored because no name could be determined for them.</div>

<div>
@foreach ($tasks as $task)
<x-schedule-monitor::task :task="$task" />
@endforeach
</div>

<div>To monitor these tasks you should add <span class="text-yellow font-bold">->monitorName()</span> in the schedule to manually specify a name.</div>
</div>
22 changes: 22 additions & 0 deletions resources/views/list.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<div class="my-1 mx-2 space-y-1">
<x-schedule-monitor::monitored-tasks
:tasks="$monitoredTasks"
:dateFormat="$dateFormat"
:usingOhDear="$usingOhDear"
/>
@if (! $readyForMonitoringTasks->isEmpty())
<x-schedule-monitor::ready-for-monitoring-tasks
:tasks="$readyForMonitoringTasks"
/>
@endif
@if (! $unnamedTasks->isEmpty())
<x-schedule-monitor::unnamed-tasks
:tasks="$unnamedTasks"
/>
@endif
@if (! $duplicateTasks->isEmpty())
<x-schedule-monitor::duplicate-tasks
:tasks="$duplicateTasks"
/>
@endif
</div>
4 changes: 4 additions & 0 deletions resources/views/sync.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div class="mx-2 my-1 space-y-1">
<div>All done! Now monitoring {{ $monitoredScheduledTasksCount }} {{ str()->plural('scheduled task', $monitoredScheduledTasksCount) }}.</div>
<div>Run <span class="text-yellow font-bold">php artisan schedule-monitor:list</span> to see which jobs are now monitored.</div>
</div>
40 changes: 31 additions & 9 deletions src/Commands/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace Spatie\ScheduleMonitor\Commands;

use Illuminate\Console\Command;
use Spatie\ScheduleMonitor\Commands\Tables\DuplicateTasksTable;
use Spatie\ScheduleMonitor\Commands\Tables\MonitoredTasksTable;
use Spatie\ScheduleMonitor\Commands\Tables\ReadyForMonitoringTasksTable;
use Spatie\ScheduleMonitor\Commands\Tables\UnnamedTasksTable;
use Spatie\ScheduleMonitor\Support\ScheduledTasks\ScheduledTasks;
use Spatie\ScheduleMonitor\Support\ScheduledTasks\Tasks\Task;
use function Termwind\render;
use function Termwind\style;

class ListCommand extends Command
{
Expand All @@ -16,11 +16,33 @@ class ListCommand extends Command

public function handle()
{
(new MonitoredTasksTable($this))->render();
(new ReadyForMonitoringTasksTable($this))->render();
(new UnnamedTasksTable($this))->render();
(new DuplicateTasksTable($this))->render();
$dateFormat = config('schedule-monitor.date_format');
style('date-width')->apply('w-' . strlen(date($dateFormat)));

$this->line('');
render(view('schedule-monitor::list', [
'monitoredTasks' => ScheduledTasks::createForSchedule()->monitoredTasks(),
'readyForMonitoringTasks' => ScheduledTasks::createForSchedule()->readyForMonitoringTasks(),
'unnamedTasks' => ScheduledTasks::createForSchedule()->unnamedTasks(),
'duplicateTasks' => ScheduledTasks::createForSchedule()->duplicateTasks(),
'usingOhDear' => $this->usingOhDear(),
'dateFormat' => $dateFormat,
]));
}

protected function usingOhDear(): bool
{
if (! class_exists(OhDear::class)) {
return false;
}

if (empty(config('schedule-monitor.oh_dear.api_token'))) {
return false;
}

if (empty(config('schedule-monitor.oh_dear.site_id'))) {
return false;
}

return true;
}
}
41 changes: 32 additions & 9 deletions src/Commands/SyncCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Spatie\ScheduleMonitor\Support\Concerns\UsesScheduleMonitoringModels;
use Spatie\ScheduleMonitor\Support\ScheduledTasks\ScheduledTasks;
use Spatie\ScheduleMonitor\Support\ScheduledTasks\Tasks\Task;
use function Termwind\render;

class SyncCommand extends Command
{
Expand All @@ -21,22 +22,27 @@ class SyncCommand extends Command

public function handle()
{
$this->info('Start syncing schedule...' . PHP_EOL);
render(view('schedule-monitor::alert', [
'message' => 'Start syncing schedule...',
'class' => 'text-green'
]));

$this
->syncScheduledTasksWithDatabase()
->syncMonitoredScheduledTaskWithOhDear();

$monitoredScheduledTasksCount = $this->getMonitoredScheduleTaskModel()->count();

$this->info('');
$this->info('All done! Now monitoring ' . $monitoredScheduledTasksCount . ' ' . Str::plural('scheduled task', $monitoredScheduledTasksCount) . '.');
$this->info('');
$this->info('Run `php artisan schedule-monitor:list` to see which jobs are now monitored.');
render(view('schedule-monitor::sync', [
'monitoredScheduledTasksCount' => $monitoredScheduledTasksCount,
]));
}

protected function syncScheduledTasksWithDatabase(): self
{
$this->comment('Start syncing schedule with database...');
render(view('schedule-monitor::alert', [
'message' => 'Start syncing schedule with database...',
]));

$monitoredScheduledTasks = ScheduledTasks::createForSchedule()
->uniqueTasks()
Expand Down Expand Up @@ -68,12 +74,25 @@ protected function syncMonitoredScheduledTaskWithOhDear(): self
$siteId = config('schedule-monitor.oh_dear.site_id');

if (! $siteId) {
$this->warn('Not syncing schedule with Oh Dear because not `site_id` is not set in the `oh-dear` config file. Learn how to set this up at https://ohdear.app/docs/general/cron-job-monitoring/php#cron-monitoring-in-laravel-php');
render(view('schedule-monitor::alert', [
'message' => <<<HTML
<div>
Not syncing schedule with <b class="bg-red-700 text-white px-1">oh dear</b> because not <b class="bg-gray-500 px-1 text-white">site_id</b>
is not set in the <b class="bg-gray-500 px-1 text-white">oh-dear</b> config file.
</div>
<div>
Learn how to set this up at <a href="https://ohdear.app/docs/general/cron-job-monitoring/php#cron-monitoring-in-laravel-php">https://ohdear.app/docs/general/cron-job-monitoring/php#cron-monitoring-in-laravel-php</a>.
</div>
HTML,
'class' => 'text-yellow',
]));

return $this;
}

$this->comment('Start syncing schedule with Oh Dear...');
render(view('schedule-monitor::alert', [
'message' => 'Start syncing schedule with Oh Dear...',
]));

$monitoredScheduledTasks = $this->getMonitoredScheduleTaskModel()->get();

Expand All @@ -91,7 +110,11 @@ protected function syncMonitoredScheduledTaskWithOhDear(): self
->toArray();

$cronChecks = app(OhDear::class)->site($siteId)->syncCronChecks($cronChecks);
$this->comment('Successfully synced schedule with Oh Dear!');

render(view('schedule-monitor::alert', [
'message' => 'Successfully synced schedule with Oh Dear!',
'class' => 'text-green',
]));

collect($cronChecks)
->each(
Expand Down
38 changes: 0 additions & 38 deletions src/Commands/Tables/DuplicateTasksTable.php

This file was deleted.

Loading

0 comments on commit 0ac79e7

Please sign in to comment.