Skip to content

Commit

Permalink
Enable record and display schedule runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
edwink75 committed Feb 1, 2024
1 parent 38f0d75 commit fbf2757
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 20 deletions.
28 changes: 23 additions & 5 deletions src/Console/Scheduling/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -15,6 +16,8 @@ class Schedule

private $tasks;

private $history;

public function __construct(ScheduleService $scheduleService, BaseSchedule $schedule)
{
$this->tasks = $scheduleService->getActives();
Expand Down Expand Up @@ -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);
}
}
);
Expand All @@ -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)
]
);
Expand Down
46 changes: 31 additions & 15 deletions src/Filament/Resources/ScheduleResource/Pages/ViewSchedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
Expand Down Expand Up @@ -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("<br />", 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")),

];
}
}

0 comments on commit fbf2757

Please sign in to comment.