diff --git a/config/filament-database-schedule.php b/config/filament-database-schedule.php index 7b9d13d..0380a5a 100644 --- a/config/filament-database-schedule.php +++ b/config/filament-database-schedule.php @@ -3,6 +3,7 @@ // config for HusamTariq/FilamentDatabaseSchedule use HusamTariq\FilamentDatabaseSchedule\Models\Schedule; +use HusamTariq\FilamentDatabaseSchedule\Filament\Resources\ScheduleResource; use Illuminate\Support\Str; return [ @@ -17,7 +18,10 @@ 'timezone' => env('FILAMENT_SCHEDULE_TIMEZONE', config('app.timezone')), - + 'resources' => + [ + ScheduleResource::class, + ], /** * Cache settings diff --git a/database/migrations/2023_12_20_173900_change_status_type_in_schedule_table.php b/database/migrations/2023_12_20_173900_change_status_type_in_schedule_table.php index f9ad1bf..1802860 100644 --- a/database/migrations/2023_12_20_173900_change_status_type_in_schedule_table.php +++ b/database/migrations/2023_12_20_173900_change_status_type_in_schedule_table.php @@ -19,7 +19,7 @@ public function up() DB::table(Config::get('filament-database-schedule.table.schedules', 'schedules'))->where('status', 0)->update(['new_status' => 'inactive']); DB::table(Config::get('filament-database-schedule.table.schedules', 'schedules'))->where('status', 1)->update(['new_status' => 'active']); - DB::table(Config::get('filament-database-schedule.table.schedules', 'schedules'))->where('status', 3)->update(['new_status' => 'trashed']); + // DB::table(Config::get('filament-database-schedule.table.schedules', 'schedules'))->where('status', 3)->update(['new_status' => 'trashed']); Schema::table(Config::get('filament-database-schedule.table.schedules', 'schedules'), function (Blueprint $table) { $table->dropColumn('status'); diff --git a/src/Console/Scheduling/Schedule.php b/src/Console/Scheduling/Schedule.php index a442cb6..dc0c070 100644 --- a/src/Console/Scheduling/Schedule.php +++ b/src/Console/Scheduling/Schedule.php @@ -3,6 +3,7 @@ namespace HusamTariq\FilamentDatabaseSchedule\Console\Scheduling; use HusamTariq\FilamentDatabaseSchedule\Http\Services\ScheduleService; +use HusamTariq\FilamentDatabaseSchedule\Models\ScheduleHistory; use \Illuminate\Console\Scheduling\Schedule as BaseSchedule; use Illuminate\Support\Facades\Log; @@ -15,6 +16,8 @@ class Schedule private $tasks; + private $history; + public function __construct(ScheduleService $scheduleService, BaseSchedule $schedule) { $this->tasks = $scheduleService->getActives(); @@ -89,20 +92,24 @@ private function dispatch($task) $event->onOneServer(); } + $event->before(function () use ($task, $command){ + $this->history = $this->createHistoryEntry($task, $command); + }); + $event->onSuccess( - function () use ($task, $event, $command) { + function () use ($task, $event) { $this->createLogFile($task, $event); if ($task->log_success) { - $this->createHistoryEntry($task, $event, $command); + $this->updateHistoryEntry($this->history, $event); } } ); $event->onFailure( - function () use ($task, $event, $command) { + function () use ($task, $event) { $this->createLogFile($task, $event, 'critical'); if ($task->log_error) { - $this->createHistoryEntry($task, $event, $command); + $this->updateHistoryEntry($this->history, $event); } } ); @@ -128,13 +135,24 @@ private function createLogFile($task, $event, $type = 'info') } } - private function createHistoryEntry($task, $event, $command) + private function createHistoryEntry($task, $command) : ScheduleHistory { $task->histories()->create( [ 'command' => $command, 'params' => $task->getArguments(), 'options' => $task->getOptions(), + 'output' => "Processing ..." + ] + ); + $history = $task->histories()->latest()->first(); + return $history; + } + + private function updateHistoryEntry(ScheduleHistory $history, $event) + { + $history->update( + [ 'output' => file_get_contents($event->output) ] ); diff --git a/src/Filament/Resources/ScheduleResource/Pages/ViewSchedule.php b/src/Filament/Resources/ScheduleResource/Pages/ViewSchedule.php index 5c8d75d..8845ab7 100644 --- a/src/Filament/Resources/ScheduleResource/Pages/ViewSchedule.php +++ b/src/Filament/Resources/ScheduleResource/Pages/ViewSchedule.php @@ -2,20 +2,21 @@ namespace HusamTariq\FilamentDatabaseSchedule\Filament\Resources\ScheduleResource\Pages; -use HusamTariq\FilamentDatabaseSchedule\Filament\Resources\ScheduleResource; -use Filament\Tables; use Filament\Forms; -use Filament\Resources\Pages\Page; -use Filament\Resources\Pages\Concerns\HasRelationManagers; -use Filament\Resources\Concerns\HasTabs; -use Filament\Resources\Pages\Concerns\InteractsWithRecord; +use Filament\Tables; use Livewire\Attributes\Url; - +use Filament\Resources\Pages\Page; +use Illuminate\Support\HtmlString; use Filament\Tables\Contracts\HasTable; +use Filament\Resources\Concerns\HasTabs; use Illuminate\Database\Eloquent\Builder; -use HusamTariq\FilamentDatabaseSchedule\Filament\Columns\ScheduleArguments; -use HusamTariq\FilamentDatabaseSchedule\Filament\Columns\ScheduleOptions; + +use Filament\Resources\Pages\Concerns\HasRelationManagers; +use Filament\Resources\Pages\Concerns\InteractsWithRecord; use HusamTariq\FilamentDatabaseSchedule\Models\ScheduleHistory; +use HusamTariq\FilamentDatabaseSchedule\Filament\Columns\ScheduleOptions; +use HusamTariq\FilamentDatabaseSchedule\Filament\Columns\ScheduleArguments; +use HusamTariq\FilamentDatabaseSchedule\Filament\Resources\ScheduleResource; class ViewSchedule extends Page implements HasTable { @@ -28,7 +29,7 @@ class ViewSchedule extends Page implements HasTable #[Url] public ?string $activeTab = null; - + use Forms\Concerns\InteractsWithForms; use Tables\Concerns\InteractsWithTable { makeTable as makeBaseTable; @@ -72,17 +73,32 @@ protected function getTableColumns(): array return [ Tables\Columns\Layout\Split::make([ Tables\Columns\TextColumn::make('command')->label(__('filament-database-schedule::schedule.fields.command')), - ScheduleArguments::make('params')->withValue(false)->label(__('filament-database-schedule::schedule.fields.arguments'))->separator(',')->badge(), - Tables\Columns\TextColumn::make('options')->label(__('filament-database-schedule::schedule.fields.options'))->separator(',')->badge(), - Tables\Columns\TextColumn::make('created_at')->label(__('filament-database-schedule::schedule.fields.expression')) + Tables\Columns\TextColumn::make('created_at') + ->label(__('filament-database-schedule::schedule.fields.expression')) ->dateTime(), + Tables\Columns\TextColumn::make('updated_at') + ->label(__('filament-database-schedule::schedule.fields.expression')) + ->formatStateUsing(function ($state, $record) { + if($state == $record->created_at){ + return "Processing..."; + }else{ + return $state->diffInSeconds($record->created_at) . " seconds"; + } + }), + Tables\Columns\TextColumn::make('output') + ->label("Output lines") + ->formatStateUsing(function ($state) { + return (count(explode("
", nl2br($state))) - 1) . " rows of output"; + }), ]), Tables\Columns\Layout\Panel::make([ - Tables\Columns\TextColumn::make('output')->extraAttributes(["class"=>"!max-w-max"],true), + Tables\Columns\TextColumn::make('output')->extraAttributes(["class" => "!max-w-max"], true) + ->formatStateUsing(function ($state) { + return new HtmlString(nl2br($state)); + }), ])->collapsible()->collapsed(config("filament-database-schedule.history_collapsed")), - ]; } } diff --git a/src/FilamentDatabaseSchedulePlugin.php b/src/FilamentDatabaseSchedulePlugin.php index 96ccfd3..5ad6d9a 100644 --- a/src/FilamentDatabaseSchedulePlugin.php +++ b/src/FilamentDatabaseSchedulePlugin.php @@ -4,7 +4,6 @@ use Filament\Contracts\Plugin; use Filament\Panel; -use HusamTariq\FilamentDatabaseSchedule\Filament\Resources\ScheduleResource; class FilamentDatabaseSchedulePlugin implements Plugin { @@ -25,9 +24,7 @@ public function getId(): string public function register(Panel $panel): void { $panel - ->resources([ - ScheduleResource::class, - ]) + ->resources(config('filament-database-schedule.resources')) ; }