From 1571e3cb24f92a3cc8c6a9289a7001864fe3050b Mon Sep 17 00:00:00 2001 From: MartinOscar <40749467+RMartinOscar@users.noreply.github.com> Date: Sat, 28 Dec 2024 22:03:21 +0100 Subject: [PATCH] Rework Schedules (#843) --- .../RelationManagers/TasksRelationManager.php | 142 +++++++----------- 1 file changed, 57 insertions(+), 85 deletions(-) diff --git a/app/Filament/Server/Resources/ScheduleResource/RelationManagers/TasksRelationManager.php b/app/Filament/Server/Resources/ScheduleResource/RelationManagers/TasksRelationManager.php index 6e503b7ec..1ba81d195 100644 --- a/app/Filament/Server/Resources/ScheduleResource/RelationManagers/TasksRelationManager.php +++ b/app/Filament/Server/Resources/ScheduleResource/RelationManagers/TasksRelationManager.php @@ -22,6 +22,50 @@ class TasksRelationManager extends RelationManager { protected static string $relationship = 'tasks'; + private function getActionOptions(bool $full = true): array + { + return [ + Task::ACTION_POWER => $full ? 'Send power action' : 'Power action', + Task::ACTION_COMMAND => $full ? 'Send command' : 'Command', + Task::ACTION_BACKUP => $full ? 'Create backup' : 'Files to ignore', + Task::ACTION_DELETE_FILES => $full ? 'Delete files' : 'Files to delete', + ]; + } + + private function getTaskForm(Schedule $schedule): array + { + return [ + Select::make('action') + ->required() + ->live() + ->disableOptionWhen(fn (string $value): bool => $value === Task::ACTION_BACKUP && $schedule->server->backup_limit === 0) + ->options($this->getActionOptions()) + ->selectablePlaceholder(false), + Textarea::make('payload') + ->hidden(fn (Get $get) => $get('action') === Task::ACTION_POWER) + ->label(fn (Get $get) => $this->getActionOptions(false)[$get('action')] ?? 'Payload'), + Select::make('payload') + ->visible(fn (Get $get) => $get('action') === Task::ACTION_POWER) + ->label('Power Action') + ->required() + ->options([ + 'start' => 'Start', + 'restart' => 'Restart', + 'stop' => 'Stop', + 'kill' => 'Kill', + ]) + ->selectablePlaceholder(false), + TextInput::make('time_offset') + ->hidden(fn (Get $get) => config('queue.default') === 'sync' || $get('sequence_id') === 1) + ->default(0) + ->numeric() + ->minValue(0) + ->maxValue(900) + ->suffix('Seconds'), + Toggle::make('continue_on_failure'), + ]; + } + public function table(Table $table): Table { /** @var Schedule $schedule */ @@ -31,13 +75,17 @@ class TasksRelationManager extends RelationManager ->reorderable('sequence_id', true) ->columns([ TextColumn::make('action') - ->state(fn (Task $task) => match ($task->action) { - Task::ACTION_POWER => 'Send power action', - Task::ACTION_COMMAND => 'Send command', - Task::ACTION_BACKUP => 'Create backup', - Task::ACTION_DELETE_FILES => 'Delete files', - default => $task->action - }), + ->state(fn (Task $task) => $this->getActionOptions()[$task->action] ?? $task->action), + TextColumn::make('payload') + ->state(function (Task $task) { + $payload = match ($task->payload) { + 'start', 'restart', 'stop', 'kill' => mb_ucfirst($task->payload), + default => $task->payload + }; + + return explode(PHP_EOL, $payload); + }) + ->badge(), TextColumn::make('time_offset') ->hidden(fn () => config('queue.default') === 'sync') ->suffix(' Seconds'), @@ -46,45 +94,7 @@ class TasksRelationManager extends RelationManager ]) ->actions([ EditAction::make() - ->form([ - Select::make('action') - ->required() - ->live() - ->disableOptionWhen(fn (string $value): bool => $value === Task::ACTION_BACKUP && $schedule->server->backup_limit === 0) - ->options([ - Task::ACTION_POWER => 'Send power action', - Task::ACTION_COMMAND => 'Send command', - Task::ACTION_BACKUP => 'Create backup', - Task::ACTION_DELETE_FILES => 'Delete files', - ]), - Textarea::make('payload') - ->hidden(fn (Get $get) => $get('action') === Task::ACTION_POWER) - ->label(fn (Get $get) => match ($get('action')) { - Task::ACTION_POWER => 'Power action', - Task::ACTION_COMMAND => 'Command', - Task::ACTION_BACKUP => 'Files to ignore', - Task::ACTION_DELETE_FILES => 'Files to delete', - default => 'Payload' - }), - Select::make('payload') - ->visible(fn (Get $get) => $get('action') === Task::ACTION_POWER) - ->label('Power Action') - ->required() - ->options([ - 'start' => 'Start', - 'restart' => 'Restart', - 'stop' => 'Stop', - 'Kill' => 'Kill', - ]), - TextInput::make('time_offset') - ->hidden(fn (Get $get) => config('queue.default') === 'sync' || $get('sequence_id') === 1) - ->default(0) - ->numeric() - ->minValue(0) - ->maxValue(900) - ->suffix('Seconds'), - Toggle::make('continue_on_failure'), - ]), + ->form($this->getTaskForm($schedule)), DeleteAction::make(), ]) ->headerActions([ @@ -92,45 +102,7 @@ class TasksRelationManager extends RelationManager ->createAnother(false) ->label(fn () => $schedule->tasks()->count() >= config('panel.client_features.schedules.per_schedule_task_limit', 10) ? 'Task Limit Reached' : 'Create Task') ->disabled(fn () => $schedule->tasks()->count() >= config('panel.client_features.schedules.per_schedule_task_limit', 10)) - ->form([ - Select::make('action') - ->required() - ->live() - ->disableOptionWhen(fn (string $value): bool => $value === Task::ACTION_BACKUP && $schedule->server->backup_limit === 0) - ->options([ - Task::ACTION_POWER => 'Send power action', - Task::ACTION_COMMAND => 'Send command', - Task::ACTION_BACKUP => 'Create backup', - Task::ACTION_DELETE_FILES => 'Delete files', - ]), - Textarea::make('payload') - ->hidden(fn (Get $get) => $get('action') === Task::ACTION_POWER) - ->label(fn (Get $get) => match ($get('action')) { - Task::ACTION_POWER => 'Power action', - Task::ACTION_COMMAND => 'Command', - Task::ACTION_BACKUP => 'Files to ignore', - Task::ACTION_DELETE_FILES => 'Files to delete', - default => 'Payload' - }), - Select::make('payload') - ->visible(fn (Get $get) => $get('action') === Task::ACTION_POWER) - ->label('Power Action') - ->required() - ->options([ - 'start' => 'Start', - 'restart' => 'Restart', - 'stop' => 'Stop', - 'Kill' => 'Kill', - ]), - TextInput::make('time_offset') - ->hidden(fn (Get $get) => config('queue.default') === 'sync' || $get('sequence_id') === 1) - ->default(0) - ->numeric() - ->minValue(0) - ->maxValue(900) - ->suffix('Seconds'), - Toggle::make('continue_on_failure'), - ]) + ->form($this->getTaskForm($schedule)) ->action(function ($data) use ($schedule) { $sequenceId = ($schedule->tasks()->orderByDesc('sequence_id')->first()->sequence_id ?? 0) + 1;