Complete add/edit/delete for scheduled tasks.

Refactor views.
This commit is contained in:
Stuart Rowlands
2024-01-01 18:23:29 -08:00
parent adecf328fc
commit 7913a639b5
8 changed files with 129 additions and 194 deletions

View File

@@ -5,12 +5,14 @@ namespace App\Console;
use App\Jobs\CheckLogDrainContainerJob; use App\Jobs\CheckLogDrainContainerJob;
use App\Jobs\CleanupInstanceStuffsJob; use App\Jobs\CleanupInstanceStuffsJob;
use App\Jobs\DatabaseBackupJob; use App\Jobs\DatabaseBackupJob;
use App\Jobs\ScheduledTaskJob;
use App\Jobs\InstanceAutoUpdateJob; use App\Jobs\InstanceAutoUpdateJob;
use App\Jobs\ContainerStatusJob; use App\Jobs\ContainerStatusJob;
use App\Jobs\PullHelperImageJob; use App\Jobs\PullHelperImageJob;
use App\Jobs\ServerStatusJob; use App\Jobs\ServerStatusJob;
use App\Models\InstanceSettings; use App\Models\InstanceSettings;
use App\Models\ScheduledDatabaseBackup; use App\Models\ScheduledDatabaseBackup;
use App\Models\ScheduledTask;
use App\Models\Server; use App\Models\Server;
use App\Models\Team; use App\Models\Team;
use Illuminate\Console\Scheduling\Schedule; use Illuminate\Console\Scheduling\Schedule;
@@ -30,6 +32,7 @@ class Kernel extends ConsoleKernel
$this->check_resources($schedule); $this->check_resources($schedule);
$this->check_scheduled_backups($schedule); $this->check_scheduled_backups($schedule);
$this->pull_helper_image($schedule); $this->pull_helper_image($schedule);
$this->check_scheduled_tasks($schedule);
} else { } else {
// Instance Jobs // Instance Jobs
$schedule->command('horizon:snapshot')->everyFiveMinutes(); $schedule->command('horizon:snapshot')->everyFiveMinutes();
@@ -41,6 +44,7 @@ class Kernel extends ConsoleKernel
$this->check_scheduled_backups($schedule); $this->check_scheduled_backups($schedule);
$this->check_resources($schedule); $this->check_resources($schedule);
$this->pull_helper_image($schedule); $this->pull_helper_image($schedule);
$this->check_scheduled_tasks($schedule);
} }
} }
private function pull_helper_image($schedule) private function pull_helper_image($schedule)
@@ -107,6 +111,32 @@ class Kernel extends ConsoleKernel
} }
} }
private function check_scheduled_tasks($schedule) {
$scheduled_tasks = ScheduledTask::all();
if ($scheduled_tasks->isEmpty()) {
ray('no scheduled tasks');
return;
}
foreach ($scheduled_tasks as $scheduled_task) {
$service = $scheduled_task->service()->get();
$application = $scheduled_task->application()->get();
if (!$application && !$service) {
ray('application/service attached to scheduled task does not exist');
$scheduled_task->delete();
continue;
}
if (isset(VALID_CRON_STRINGS[$scheduled_task->frequency])) {
$scheduled_task->frequency = VALID_CRON_STRINGS[$scheduled_task->frequency];
}
$schedule->job(new ScheduledTaskJob(
task: $scheduled_task
))->cron($scheduled_task->frequency)->onOneServer();
}
}
protected function commands(): void protected function commands(): void
{ {
$this->load(__DIR__ . '/Commands'); $this->load(__DIR__ . '/Commands');

View File

@@ -33,7 +33,6 @@ class Add extends Component
public function submit() public function submit()
{ {
error_log("*** IN SUBMIT");
$this->validate(); $this->validate();
$isValid = validate_cron_expression($this->frequency); $isValid = validate_cron_expression($this->frequency);
if (!$isValid) { if (!$isValid) {

View File

@@ -10,102 +10,23 @@ use Illuminate\Support\Str;
class All extends Component class All extends Component
{ {
public $resource; public $resource;
public bool $showPreview = false;
public string|null $modalId = null; public string|null $modalId = null;
public ?string $variables = null; public ?string $variables = null;
public ?string $variablesPreview = null; public array $parameters;
protected $listeners = ['refreshTasks', 'saveScheduledTask' => 'submit']; protected $listeners = ['refreshTasks', 'saveScheduledTask' => 'submit'];
public function mount() public function mount()
{ {
$resourceClass = get_class($this->resource); $this->parameters = get_route_parameters();
$resourceWithPreviews = ['App\Models\Application'];
$simpleDockerfile = !is_null(data_get($this->resource, 'dockerfile'));
if (Str::of($resourceClass)->contains($resourceWithPreviews) && !$simpleDockerfile) {
$this->showPreview = true;
}
$this->modalId = new Cuid2(7); $this->modalId = new Cuid2(7);
$this->getDevView();
}
public function getDevView()
{
$this->variables = $this->resource->scheduled_tasks->map(function ($item) {
error_log("** got one");
return "$item->name=$item->command";
})->sort();
error_log(print_r($this->variables,1));
}
public function saveVariables($isPreview)
{
if ($isPreview) {
$variables = parseEnvFormatToArray($this->variablesPreview);
$this->resource->environment_variables_preview()->whereNotIn('key', array_keys($variables))->delete();
} else {
$variables = parseEnvFormatToArray($this->variables);
$this->resource->environment_variables()->whereNotIn('key', array_keys($variables))->delete();
}
foreach ($variables as $key => $variable) {
if ($isPreview) {
$found = $this->resource->environment_variables_preview()->where('key', $key)->first();
} else {
$found = $this->resource->environment_variables()->where('key', $key)->first();
}
if ($found) {
if ($found->is_shown_once) {
continue;
}
$found->value = $variable;
$found->save();
continue;
} else {
$task = new ScheduledTask();
$task->key = $key;
$task->value = $variable;
$task->is_build_time = false;
$task->is_preview = $isPreview ? true : false;
switch ($this->resource->type()) {
case 'application':
$task->application_id = $this->resource->id;
break;
case 'standalone-postgresql':
$task->standalone_postgresql_id = $this->resource->id;
break;
case 'standalone-redis':
$task->standalone_redis_id = $this->resource->id;
break;
case 'standalone-mongodb':
$task->standalone_mongodb_id = $this->resource->id;
break;
case 'standalone-mysql':
$task->standalone_mysql_id = $this->resource->id;
break;
case 'standalone-mariadb':
$task->standalone_mariadb_id = $this->resource->id;
break;
case 'service':
$task->service_id = $this->resource->id;
break;
}
$task->save();
}
}
if ($isPreview) {
$this->dispatch('success', 'Preview environment variables updated successfully.');
} else {
$this->dispatch('success', 'Environment variables updated successfully.');
}
$this->refreshTasks();
} }
public function refreshTasks() public function refreshTasks()
{ {
$this->resource->refresh(); $this->resource->refresh();
$this->getDevView();
} }
public function submit($data) public function submit($data)
{ {
error_log("** submitting the beast");
try { try {
$task = new ScheduledTask(); $task = new ScheduledTask();
$task->name = $data['name']; $task->name = $data['name'];

View File

@@ -4,47 +4,52 @@ namespace App\Livewire\Project\Shared\ScheduledTask;
use App\Models\ScheduledTask as ModelsScheduledTask; use App\Models\ScheduledTask as ModelsScheduledTask;
use Livewire\Component; use Livewire\Component;
use App\Models\Application;
use App\Models\Service;
use Visus\Cuid2\Cuid2; use Visus\Cuid2\Cuid2;
class Show extends Component class Show extends Component
{ {
public $parameters; public $parameters;
public Application|Service $resource;
public ModelsScheduledTask $task; public ModelsScheduledTask $task;
public ?string $modalId = null; public ?string $modalId = null;
public bool $isDisabled = false;
public bool $isLocked = false;
public string $type; public string $type;
protected $rules = [ protected $rules = [
'task.name' => 'required|string', 'task.name' => 'required|string',
'task.command' => 'required|string', 'task.command' => 'required|string',
'task.frequency' => 'required|string',
'task.container' => 'nullable|string',
]; ];
protected $validationAttributes = [ protected $validationAttributes = [
'name' => 'Name', 'name' => 'name',
'command' => 'Command', 'command' => 'command',
'frequency' => 'frequency',
'container' => 'container',
]; ];
public function mount() public function mount()
{ {
$this->modalId = new Cuid2(7);
$this->parameters = get_route_parameters(); $this->parameters = get_route_parameters();
if (data_get($this->parameters, 'application_uuid')) {
$this->type = 'application';
$this->resource = Application::where('uuid', $this->parameters['application_uuid'])->firstOrFail();
} else if (data_get($this->parameters, 'service_uuid')) {
$this->type = 'service';
$this->resource = Service::where('uuid', $this->parameters['service_uuid'])->firstOrFail();
} }
public function lock() $this->modalId = new Cuid2(7);
{ $this->task = ModelsScheduledTask::where('uuid', request()->route('task_uuid'))->first();
$this->task->is_shown_once = true;
$this->task->save();
$this->dispatch('refreshTasks');
}
public function instantSave()
{
$this->submit();
} }
public function submit() public function submit()
{ {
$this->validate(); $this->validate();
$this->task->save(); $this->task->save();
$this->dispatch('success', 'Environment variable updated successfully.'); $this->dispatch('success', 'Scheduled task updated successfully.');
$this->dispatch('refreshTasks'); $this->dispatch('refreshTasks');
} }
@@ -52,7 +57,14 @@ class Show extends Component
{ {
try { try {
$this->task->delete(); $this->task->delete();
$this->dispatch('refreshTasks');
if ($this->type == 'application') {
return redirect()->route('project.application.configuration', $this->parameters);
}
else {
return redirect()->route('project.service.configuration', $this->parameters);
}
} catch (\Exception $e) { } catch (\Exception $e) {
return handleError($e); return handleError($e);
} }

View File

@@ -2,87 +2,27 @@
namespace App\Models; namespace App\Models;
use App\Models\ScheduledTask as ModelsScheduledTask; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class ScheduledTask extends BaseModel class ScheduledTask extends BaseModel
{ {
protected $guarded = []; protected $guarded = [];
protected $casts = [
'name' => 'string',
'command' => 'string',
'frequency' => 'string',
'container' => 'string',
];
// protected static function booted()
// {
// static::created(function ($scheduled_task) {
// error_log("*** IN CREATED");
// if ($scheduled_task->application_id) {
// $found = ModelsScheduledTask::where('id', $scheduled_task->id)->where('application_id', $scheduled_task->application_id)->first();
// $application = Application::find($scheduled_task->application_id);
// if (!$found) {
// ModelsScheduledTask::create([
// 'name' => $scheduled_task->name,
// 'command' => $scheduled_task->command,
// 'frequency' => $scheduled_task->frequency,
// 'container' => $scheduled_task->container,
// 'application_id' => $scheduled_task->application_id,
// ]);
// }
// }
// });
// }
public function service() public function service()
{ {
return $this->belongsTo(Service::class); return $this->belongsTo(Service::class);
} }
// protected function value(): Attribute public function application()
// {
// return Attribute::make(
// get: fn (?string $value = null) => $this->get_scheduled_tasks($value),
// set: fn (?string $value = null) => $this->set_scheduled_tasks($value),
// );
// }
private function get_scheduled_tasks(?string $scheduled_task = null): string|null
{ {
error_log("** in get_scheduled_tasks"); return $this->belongsTo(Application::class);
// // $team_id = currentTeam()->id;
// if (!$scheduled_task) {
// return null;
// }
// $scheduled_task = trim(decrypt($scheduled_task));
// if (Str::startsWith($scheduled_task, '{{') && Str::endsWith($scheduled_task, '}}') && Str::contains($scheduled_task, 'global.')) {
// $variable = Str::after($scheduled_task, 'global.');
// $variable = Str::before($variable, '}}');
// $variable = Str::of($variable)->trim()->value;
// // $scheduled_task = GlobalScheduledTask::where('name', $scheduled_task)->where('team_id', $team_id)->first()?->value;
// ray('global env variable');
// return $scheduled_task;
// }
// return $scheduled_task;
} }
public function latest_log(): HasOne
private function set_scheduled_tasks(?string $scheduled_task = null): string|null
{ {
error_log("** in set_scheduled_tasks"); return $this->hasOne(ScheduledTaskExecution::class)->latest();
// if (is_null($scheduled_task) && $scheduled_task == '') { }
// return null; public function executions(): HasMany
// } {
// $scheduled_task = trim($scheduled_task); return $this->hasMany(ScheduledTaskExecution::class);
// return encrypt($scheduled_task);
} }
// protected function key(): Attribute
// {
// error_log("** in key()");
// // return Attribute::make(
// // set: fn (string $value) => Str::of($value)->trim(),
// // );
// }
} }

View File

@@ -1,16 +1,31 @@
<div class="flex flex-col gap-2"> <div>
<div> <div class="flex gap-2">
<div class="flex items-center gap-2"> <h2 class="pb-4">Scheduled Tasks</h2>
<h2>Scheduled Tasks</h2>
<x-forms.button class="btn" onclick="newTask.showModal()">+ Add</x-forms.button> <x-forms.button class="btn" onclick="newTask.showModal()">+ Add</x-forms.button>
<livewire:project.shared.scheduled-task.add /> <livewire:project.shared.scheduled-task.add />
</div> </div>
<div>Scheduled Tasks for this resource.</div>
</div> <div class="flex flex-wrap gap-2">
@forelse ($resource->scheduled_tasks as $task) @forelse($resource->scheduled_tasks as $task)
<livewire:project.shared.scheduled-task.show wire:key="scheduled-task-{{ $task->id }}" <a class="flex flex-col box"
:task="$task" :type="$resource->type()" /> href="{{ route('project.application.scheduled-tasks', [...$parameters, 'task_uuid' => $task->uuid]) }}">
<div><span class="font-bold text-warning">{{ $task->name }}<span></div>
<div>Frequency: {{ $task->frequency }}</div>
<div>Last run: {{ data_get($task->latest_log, 'status', 'No runs yet') }}</div>
<div>Next run: @todo</div>
</a>
@empty @empty
<div class="text-neutral-500">No scheduled tasks found.</div> <div>No scheduled tasks configured.</div>
@endforelse @endforelse
</div>
{{-- @if ($type === 'service-database' && $selectedBackup)
<div class="pt-10">
<livewire:project.database.backup-edit key="{{ $selectedBackup->id }}" :backup="$selectedBackup" :s3s="$s3s"
:status="data_get($database, 'status')" />
<h3 class="py-4">Executions</h3>
<livewire:project.database.backup-executions key="{{ $selectedBackup->id }}" :backup="$selectedBackup"
:executions="$selectedBackup->executions" />
</div>
@endif --}}
</div> </div>

View File

@@ -5,17 +5,32 @@
class="font-bold text-warning">({{ $task->name }})</span>?</p> class="font-bold text-warning">({{ $task->name }})</span>?</p>
</x-slot:modalBody> </x-slot:modalBody>
</x-modal> </x-modal>
<form wire:submit='submit'
class="flex flex-col gap-2 p-4 m-2 border lg:items-center border-coolgray-300 lg:m-0 lg:p-0 lg:border-0 lg:flex-row"> <h1>Scheduled Backup</h1>
<x-forms.input id="task.name" /> <livewire:project.application.heading :application="$resource" />
<x-forms.input id="task.command" />
<div class="flex gap-2"> <form wire:submit="submit">
<div class="flex flex-col gap-2 pb-10">
<div class="flex items-end gap-2 pt-4">
<h2>Scheduled Task</h2>
<x-forms.button type="submit"> <x-forms.button type="submit">
Update Save
</x-forms.button> </x-forms.button>
{{-- @if (Str::of($status)->startsWith('running'))
<livewire:project.database.backup-now :backup="$backup" />
@endif --}}
<x-forms.button isError isModal modalId="{{ $modalId }}"> <x-forms.button isError isModal modalId="{{ $modalId }}">
Delete Delete
</x-forms.button> </x-forms.button>
</div> </div>
</div>
<x-forms.input placeholder="Run cron" id="task.name" label="Name" required />
<x-forms.input placeholder="php artisan schedule:run" id="task.command" label="Command" required />
<x-forms.input placeholder="0 0 * * * or daily" id="task.frequency" label="Frequency" required />
<x-forms.input placeholder="php" id="task.container" label="Container name" />
</form> </form>
</div> </div>

View File

@@ -16,6 +16,7 @@ use App\Livewire\Project\CloneProject;
use App\Livewire\Project\EnvironmentEdit; use App\Livewire\Project\EnvironmentEdit;
use App\Livewire\Project\Shared\ExecuteContainerCommand; use App\Livewire\Project\Shared\ExecuteContainerCommand;
use App\Livewire\Project\Shared\Logs; use App\Livewire\Project\Shared\Logs;
use App\Livewire\Project\Shared\ScheduledTask\Show as ScheduledTaskShow;
use App\Livewire\Security\ApiTokens; use App\Livewire\Security\ApiTokens;
use App\Livewire\Server\All; use App\Livewire\Server\All;
use App\Livewire\Server\Create; use App\Livewire\Server\Create;
@@ -127,6 +128,8 @@ Route::middleware(['auth', 'verified'])->group(function () {
Route::get('/project/{project_uuid}/{environment_name}/application/{application_uuid}/logs', Logs::class)->name('project.application.logs'); Route::get('/project/{project_uuid}/{environment_name}/application/{application_uuid}/logs', Logs::class)->name('project.application.logs');
Route::get('/project/{project_uuid}/{environment_name}/application/{application_uuid}/command', ExecuteContainerCommand::class)->name('project.application.command'); Route::get('/project/{project_uuid}/{environment_name}/application/{application_uuid}/command', ExecuteContainerCommand::class)->name('project.application.command');
Route::get('/project/{project_uuid}/{environment_name}/application/{application_uuid}/tasks/{task_uuid}', ScheduledTaskShow::class)->name('project.application.scheduled-tasks');
// Databases // Databases
Route::get('/project/{project_uuid}/{environment_name}/database/{database_uuid}', [DatabaseController::class, 'configuration'])->name('project.database.configuration'); Route::get('/project/{project_uuid}/{environment_name}/database/{database_uuid}', [DatabaseController::class, 'configuration'])->name('project.database.configuration');