Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge v2 with v2.0.2 #28

Merged
merged 6 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion config/filament-database-schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// config for HusamTariq/FilamentDatabaseSchedule

use HusamTariq\FilamentDatabaseSchedule\Models\Schedule;
use HusamTariq\FilamentDatabaseSchedule\Filament\Resources\ScheduleResource;
use Illuminate\Support\Str;

return [
Expand All @@ -17,7 +18,10 @@

'timezone' => env('FILAMENT_SCHEDULE_TIMEZONE', config('app.timezone')),


'resources' =>
[
ScheduleResource::class,
],

/**
* Cache settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
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")),

];
}
}
5 changes: 1 addition & 4 deletions src/FilamentDatabaseSchedulePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Filament\Contracts\Plugin;
use Filament\Panel;
use HusamTariq\FilamentDatabaseSchedule\Filament\Resources\ScheduleResource;

class FilamentDatabaseSchedulePlugin implements Plugin
{
Expand All @@ -25,9 +24,7 @@ public function getId(): string
public function register(Panel $panel): void
{
$panel
->resources([
ScheduleResource::class,
])
->resources(config('filament-database-schedule.resources'))
;
}

Expand Down