Rework Schedules (#843)

This commit is contained in:
MartinOscar 2024-12-28 22:03:21 +01:00 committed by GitHub
parent a8680c7aed
commit 1571e3cb24
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -22,6 +22,50 @@ class TasksRelationManager extends RelationManager
{ {
protected static string $relationship = 'tasks'; 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 public function table(Table $table): Table
{ {
/** @var Schedule $schedule */ /** @var Schedule $schedule */
@ -31,13 +75,17 @@ class TasksRelationManager extends RelationManager
->reorderable('sequence_id', true) ->reorderable('sequence_id', true)
->columns([ ->columns([
TextColumn::make('action') TextColumn::make('action')
->state(fn (Task $task) => match ($task->action) { ->state(fn (Task $task) => $this->getActionOptions()[$task->action] ?? $task->action),
Task::ACTION_POWER => 'Send power action', TextColumn::make('payload')
Task::ACTION_COMMAND => 'Send command', ->state(function (Task $task) {
Task::ACTION_BACKUP => 'Create backup', $payload = match ($task->payload) {
Task::ACTION_DELETE_FILES => 'Delete files', 'start', 'restart', 'stop', 'kill' => mb_ucfirst($task->payload),
default => $task->action default => $task->payload
}), };
return explode(PHP_EOL, $payload);
})
->badge(),
TextColumn::make('time_offset') TextColumn::make('time_offset')
->hidden(fn () => config('queue.default') === 'sync') ->hidden(fn () => config('queue.default') === 'sync')
->suffix(' Seconds'), ->suffix(' Seconds'),
@ -46,45 +94,7 @@ class TasksRelationManager extends RelationManager
]) ])
->actions([ ->actions([
EditAction::make() EditAction::make()
->form([ ->form($this->getTaskForm($schedule)),
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'),
]),
DeleteAction::make(), DeleteAction::make(),
]) ])
->headerActions([ ->headerActions([
@ -92,45 +102,7 @@ class TasksRelationManager extends RelationManager
->createAnother(false) ->createAnother(false)
->label(fn () => $schedule->tasks()->count() >= config('panel.client_features.schedules.per_schedule_task_limit', 10) ? 'Task Limit Reached' : 'Create Task') ->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)) ->disabled(fn () => $schedule->tasks()->count() >= config('panel.client_features.schedules.per_schedule_task_limit', 10))
->form([ ->form($this->getTaskForm($schedule))
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'),
])
->action(function ($data) use ($schedule) { ->action(function ($data) use ($schedule) {
$sequenceId = ($schedule->tasks()->orderByDesc('sequence_id')->first()->sequence_id ?? 0) + 1; $sequenceId = ($schedule->tasks()->orderByDesc('sequence_id')->first()->sequence_id ?? 0) + 1;