Skip to content

Commit

Permalink
Maintenance Page (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurpar06 committed Oct 8, 2023
1 parent 86e0947 commit eebb9de
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 0 deletions.
105 changes: 105 additions & 0 deletions app/Filament/Pages/Maintenance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace App\Filament\Pages;

use App\Repositories\KvpRepository;
use App\Services\VersionService;
use App\Support\Utils;
use Filament\Actions\Action;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Log;

class Maintenance extends Page
{
protected static ?string $navigationGroup = 'config';
protected static ?int $navigationSort = 9;

protected static ?string $navigationLabel = 'Maintenance';

protected static ?string $navigationIcon = 'heroicon-o-wrench-screwdriver';

protected static string $view = 'filament.pages.maintenance';

public function forceUpdateCheckAction(): Action
{
return Action::make('forceUpdateCheck')->label('Force Update Check')->icon('heroicon-o-arrow-path')->action(function () {
app(VersionService::class)->isNewVersionAvailable();

$kvpRepo = app(KvpRepository::class);

$new_version_avail = $kvpRepo->get('new_version_available', false);
$new_version_tag = $kvpRepo->get('latest_version_tag');

Log::info('Force check, available='.$new_version_avail.', tag='.$new_version_tag);

if (!$new_version_avail) {
Notification::make()
->title('No new version available')
->success()
->send();
} else {
Notification::make()
->title('New version available: '.$new_version_tag)
->success()
->send();
}
});
}

public function webCronEnable(): Action
{
return Action::make('webCronEnable')->label('Enable/Change ID')->action(function () {
$id = Utils::generateNewId(24);
setting_save('cron.random_id', $id);

Notification::make()
->title('Web cron refreshed!')
->success()
->send();
});
}

public function webCronDisable(): Action
{
return Action::make('webCronDisable')->label('Disable')->color('warning')->action(function () {
setting_save('cron.random_id', '');

Notification::make()
->title('Web cron disabled!')
->success()
->send();
});
}

public function clearCaches(): Action
{
return Action::make('clearCaches')->label('Clear Cache')->action(function (array $arguments) {
$calls = [];
$type = $arguments['type'];

// When clearing the application, clear the config and the app itself
if ($type === 'application' || $type === 'all') {
$calls[] = 'config:cache';
$calls[] = 'cache:clear';
$calls[] = 'route:cache';
$calls[] = 'clear-compiled';
}

// If we want to clear only the views but keep everything else
if ($type === 'views' || $type === 'all') {
$calls[] = 'view:clear';
}

foreach ($calls as $call) {
Artisan::call($call);
}

Notification::make()
->title('Cache cleared!')
->success()
->send();
});
}
}
62 changes: 62 additions & 0 deletions resources/views/filament/pages/maintenance.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<x-filament-panels::page>
<x-filament::section>
<x-slot name="heading">
Update
</x-slot>

{{ $this->forceUpdateCheckAction() }}
</x-filament::section>

<x-filament::section>
<x-slot name="heading">
CRON
</x-slot>

<div class="mb-4">
A cron must be created that runs every minute calling artisan. An example is below. <a href="{{ docs_link('cron') }}" target="_blank" class="text-primary-500">See the docs</a>
</div>

<x-filament::input.wrapper disabled class="mb-4">
<x-filament::input
type="text"
disabled
:value="app(\App\Services\CronService::class)->getCronExecString()"
/>
</x-filament::input.wrapper>

<div class="mb-4">If you don't have cron access on your server, you can use a web-cron service to access this URL every minute. Keep it disabled if you're not using it. It's a unique ID that can be reset/changed if needed for security.</div>

@php
$cron_id = setting('cron.random_id');
$cron_url = empty($cron_id) ? 'Not enabled' : url(route('api.maintenance.cron', $cron_id));
@endphp

<div class="flex items-center justify-between">
<x-filament::input.wrapper disabled class="w-full">
<x-filament::input
type="text"
disabled
:value="$cron_url"
/>
</x-filament::input.wrapper>

<div class="mx-3">
{{ $this->webCronEnable() }}
</div>
{{ $this->webCronDisable() }}
</div>

</x-filament::section>

<x-filament::section>
<x-slot name="heading">
Reset Caches
</x-slot>

<div class="flex items-center justify-between">
{{ $this->clearCaches()->label('Clear all caches')->arguments(['type' => 'all']) }}
{{ $this->clearCaches()->label('Clear application cache')->arguments(['type' => 'application']) }}
{{ $this->clearCaches()->label('Clear views cache')->arguments(['type' => 'views']) }}
</div>
</x-filament::section>
</x-filament-panels::page>

0 comments on commit eebb9de

Please sign in to comment.