From adecf328fc95661e61819ef98483158d0e94cd6c Mon Sep 17 00:00:00 2001
From: Stuart Rowlands
Date: Mon, 1 Jan 2024 10:33:16 -0800
Subject: [PATCH 1/5] WIP start of scheduled tasks.
---
.../Project/Shared/ScheduledTask/Add.php | 59 ++++++++
.../Project/Shared/ScheduledTask/All.php | 134 ++++++++++++++++++
.../Project/Shared/ScheduledTask/Show.php | 60 ++++++++
app/Models/Application.php | 5 +
app/Models/ScheduledTask.php | 88 ++++++++++++
...31_173041_create_scheduled_tasks_table.php | 36 +++++
.../application/configuration.blade.php | 6 +
.../shared/scheduled-task/add.blade.php | 15 ++
.../shared/scheduled-task/all.blade.php | 16 +++
.../shared/scheduled-task/show.blade.php | 21 +++
10 files changed, 440 insertions(+)
create mode 100644 app/Livewire/Project/Shared/ScheduledTask/Add.php
create mode 100644 app/Livewire/Project/Shared/ScheduledTask/All.php
create mode 100644 app/Livewire/Project/Shared/ScheduledTask/Show.php
create mode 100644 app/Models/ScheduledTask.php
create mode 100644 database/migrations/2023_12_31_173041_create_scheduled_tasks_table.php
create mode 100644 resources/views/livewire/project/shared/scheduled-task/add.blade.php
create mode 100644 resources/views/livewire/project/shared/scheduled-task/all.blade.php
create mode 100644 resources/views/livewire/project/shared/scheduled-task/show.blade.php
diff --git a/app/Livewire/Project/Shared/ScheduledTask/Add.php b/app/Livewire/Project/Shared/ScheduledTask/Add.php
new file mode 100644
index 000000000..84f790ad4
--- /dev/null
+++ b/app/Livewire/Project/Shared/ScheduledTask/Add.php
@@ -0,0 +1,59 @@
+ 'clear'];
+ protected $rules = [
+ 'name' => 'required|string',
+ 'command' => 'required|string',
+ 'frequency' => 'required|string',
+ 'container' => 'nullable|string',
+ ];
+ protected $validationAttributes = [
+ 'name' => 'name',
+ 'command' => 'command',
+ 'frequency' => 'frequency',
+ 'container' => 'container',
+ ];
+
+ public function mount()
+ {
+ $this->parameters = get_route_parameters();
+ }
+
+ public function submit()
+ {
+ error_log("*** IN SUBMIT");
+ $this->validate();
+ $isValid = validate_cron_expression($this->frequency);
+ if (!$isValid) {
+ $this->dispatch('error', 'Invalid Cron / Human expression.');
+ return;
+ }
+ $this->dispatch('saveScheduledTask', [
+ 'name' => $this->name,
+ 'command' => $this->command,
+ 'frequency' => $this->frequency,
+ 'container' => $this->container,
+ ]);
+ $this->clear();
+ }
+
+ public function clear()
+ {
+ $this->name = '';
+ $this->command = '';
+ $this->frequency = '';
+ $this->container = '';
+ }
+}
diff --git a/app/Livewire/Project/Shared/ScheduledTask/All.php b/app/Livewire/Project/Shared/ScheduledTask/All.php
new file mode 100644
index 000000000..1562a4cae
--- /dev/null
+++ b/app/Livewire/Project/Shared/ScheduledTask/All.php
@@ -0,0 +1,134 @@
+ 'submit'];
+
+ public function mount()
+ {
+ $resourceClass = get_class($this->resource);
+ $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->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()
+ {
+ $this->resource->refresh();
+ $this->getDevView();
+ }
+
+ public function submit($data)
+ {
+ error_log("** submitting the beast");
+ try {
+ $task = new ScheduledTask();
+ $task->name = $data['name'];
+ $task->command = $data['command'];
+ $task->frequency = $data['frequency'];
+ $task->container = $data['container'];
+
+ 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 'service':
+ $task->service_id = $this->resource->id;
+ break;
+ }
+ $task->save();
+ $this->refreshTasks();
+ $this->dispatch('success', 'Scheduled task added successfully.');
+ } catch (\Throwable $e) {
+ return handleError($e, $this);
+ }
+ }
+}
diff --git a/app/Livewire/Project/Shared/ScheduledTask/Show.php b/app/Livewire/Project/Shared/ScheduledTask/Show.php
new file mode 100644
index 000000000..b65c36531
--- /dev/null
+++ b/app/Livewire/Project/Shared/ScheduledTask/Show.php
@@ -0,0 +1,60 @@
+ 'required|string',
+ 'task.command' => 'required|string',
+ ];
+ protected $validationAttributes = [
+ 'name' => 'Name',
+ 'command' => 'Command',
+ ];
+
+ public function mount()
+ {
+ $this->modalId = new Cuid2(7);
+ $this->parameters = get_route_parameters();
+ }
+
+ public function lock()
+ {
+ $this->task->is_shown_once = true;
+ $this->task->save();
+ $this->dispatch('refreshTasks');
+ }
+ public function instantSave()
+ {
+ $this->submit();
+ }
+ public function submit()
+ {
+ $this->validate();
+ $this->task->save();
+ $this->dispatch('success', 'Environment variable updated successfully.');
+ $this->dispatch('refreshTasks');
+ }
+
+ public function delete()
+ {
+ try {
+ $this->task->delete();
+ $this->dispatch('refreshTasks');
+ } catch (\Exception $e) {
+ return handleError($e);
+ }
+ }
+}
diff --git a/app/Models/Application.php b/app/Models/Application.php
index 018d6ec91..fc1b7d500 100644
--- a/app/Models/Application.php
+++ b/app/Models/Application.php
@@ -315,6 +315,11 @@ public function nixpacks_environment_variables_preview(): HasMany
return $this->hasMany(EnvironmentVariable::class)->where('is_preview', true)->where('key', 'like', 'NIXPACKS_%');
}
+ public function scheduled_tasks(): HasMany
+ {
+ return $this->hasMany(ScheduledTask::class)->orderBy('name', 'asc');
+ }
+
public function private_key()
{
return $this->belongsTo(PrivateKey::class);
diff --git a/app/Models/ScheduledTask.php b/app/Models/ScheduledTask.php
new file mode 100644
index 000000000..79468dd2a
--- /dev/null
+++ b/app/Models/ScheduledTask.php
@@ -0,0 +1,88 @@
+ '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()
+ {
+ return $this->belongsTo(Service::class);
+ }
+ // protected function value(): Attribute
+ // {
+ // 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");
+ // // $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;
+ }
+
+ private function set_scheduled_tasks(?string $scheduled_task = null): string|null
+ {
+ error_log("** in set_scheduled_tasks");
+ // if (is_null($scheduled_task) && $scheduled_task == '') {
+ // return null;
+ // }
+ // $scheduled_task = trim($scheduled_task);
+ // return encrypt($scheduled_task);
+ }
+
+ // protected function key(): Attribute
+ // {
+ // error_log("** in key()");
+
+ // // return Attribute::make(
+ // // set: fn (string $value) => Str::of($value)->trim(),
+ // // );
+ // }
+}
diff --git a/database/migrations/2023_12_31_173041_create_scheduled_tasks_table.php b/database/migrations/2023_12_31_173041_create_scheduled_tasks_table.php
new file mode 100644
index 000000000..b108cecf8
--- /dev/null
+++ b/database/migrations/2023_12_31_173041_create_scheduled_tasks_table.php
@@ -0,0 +1,36 @@
+id();
+ $table->string('uuid')->unique();
+ $table->boolean('enabled')->default(true);
+ $table->string('name');
+ $table->string('command');
+ $table->string('frequency');
+ $table->string('container')->nullable();
+ $table->timestamps();
+
+ $table->foreignId('application_id')->nullable();
+ $table->foreignId('service_id')->nullable();
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ */
+ public function down(): void
+ {
+ Schema::dropIfExists('scheduled_tasks');
+ }
+};
diff --git a/resources/views/livewire/project/application/configuration.blade.php b/resources/views/livewire/project/application/configuration.blade.php
index 5d2936649..61fbc6c66 100644
--- a/resources/views/livewire/project/application/configuration.blade.php
+++ b/resources/views/livewire/project/application/configuration.blade.php
@@ -54,6 +54,9 @@
href="#">Resource Limits
@endif
+ Scheduled Tasks
+
Danger Zone
@@ -97,6 +100,9 @@
+
+
+
diff --git a/resources/views/livewire/project/shared/scheduled-task/add.blade.php b/resources/views/livewire/project/shared/scheduled-task/add.blade.php
new file mode 100644
index 000000000..133c8cead
--- /dev/null
+++ b/resources/views/livewire/project/shared/scheduled-task/add.blade.php
@@ -0,0 +1,15 @@
+
diff --git a/resources/views/livewire/project/shared/scheduled-task/all.blade.php b/resources/views/livewire/project/shared/scheduled-task/all.blade.php
new file mode 100644
index 000000000..1c42cdc00
--- /dev/null
+++ b/resources/views/livewire/project/shared/scheduled-task/all.blade.php
@@ -0,0 +1,16 @@
+
+
+
+
Scheduled Tasks
+ + Add
+
+
+
Scheduled Tasks for this resource.
+
+ @forelse ($resource->scheduled_tasks as $task)
+
+ @empty
+
No scheduled tasks found.
+ @endforelse
+
diff --git a/resources/views/livewire/project/shared/scheduled-task/show.blade.php b/resources/views/livewire/project/shared/scheduled-task/show.blade.php
new file mode 100644
index 000000000..d3a9bce47
--- /dev/null
+++ b/resources/views/livewire/project/shared/scheduled-task/show.blade.php
@@ -0,0 +1,21 @@
+
+
+
+ Are you sure you want to delete this scheduled task ({{ $task->name }})?
+
+
+
+
From 7913a639b54ec5a385f84f9b4ef67fd957158d36 Mon Sep 17 00:00:00 2001
From: Stuart Rowlands
Date: Mon, 1 Jan 2024 18:23:29 -0800
Subject: [PATCH 2/5] Complete add/edit/delete for scheduled tasks. Refactor
views.
---
app/Console/Kernel.php | 30 +++++++
.../Project/Shared/ScheduledTask/Add.php | 1 -
.../Project/Shared/ScheduledTask/All.php | 83 +------------------
.../Project/Shared/ScheduledTask/Show.php | 46 ++++++----
app/Models/ScheduledTask.php | 80 +++---------------
.../shared/scheduled-task/all.blade.php | 43 ++++++----
.../shared/scheduled-task/show.blade.php | 37 ++++++---
routes/web.php | 3 +
8 files changed, 129 insertions(+), 194 deletions(-)
diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php
index 2a8e857e2..267572b39 100644
--- a/app/Console/Kernel.php
+++ b/app/Console/Kernel.php
@@ -5,12 +5,14 @@
use App\Jobs\CheckLogDrainContainerJob;
use App\Jobs\CleanupInstanceStuffsJob;
use App\Jobs\DatabaseBackupJob;
+use App\Jobs\ScheduledTaskJob;
use App\Jobs\InstanceAutoUpdateJob;
use App\Jobs\ContainerStatusJob;
use App\Jobs\PullHelperImageJob;
use App\Jobs\ServerStatusJob;
use App\Models\InstanceSettings;
use App\Models\ScheduledDatabaseBackup;
+use App\Models\ScheduledTask;
use App\Models\Server;
use App\Models\Team;
use Illuminate\Console\Scheduling\Schedule;
@@ -30,6 +32,7 @@ protected function schedule(Schedule $schedule): void
$this->check_resources($schedule);
$this->check_scheduled_backups($schedule);
$this->pull_helper_image($schedule);
+ $this->check_scheduled_tasks($schedule);
} else {
// Instance Jobs
$schedule->command('horizon:snapshot')->everyFiveMinutes();
@@ -41,6 +44,7 @@ protected function schedule(Schedule $schedule): void
$this->check_scheduled_backups($schedule);
$this->check_resources($schedule);
$this->pull_helper_image($schedule);
+ $this->check_scheduled_tasks($schedule);
}
}
private function pull_helper_image($schedule)
@@ -107,6 +111,32 @@ private function check_scheduled_backups($schedule)
}
}
+ 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
{
$this->load(__DIR__ . '/Commands');
diff --git a/app/Livewire/Project/Shared/ScheduledTask/Add.php b/app/Livewire/Project/Shared/ScheduledTask/Add.php
index 84f790ad4..3cc5428b8 100644
--- a/app/Livewire/Project/Shared/ScheduledTask/Add.php
+++ b/app/Livewire/Project/Shared/ScheduledTask/Add.php
@@ -33,7 +33,6 @@ public function mount()
public function submit()
{
- error_log("*** IN SUBMIT");
$this->validate();
$isValid = validate_cron_expression($this->frequency);
if (!$isValid) {
diff --git a/app/Livewire/Project/Shared/ScheduledTask/All.php b/app/Livewire/Project/Shared/ScheduledTask/All.php
index 1562a4cae..7f61c30cc 100644
--- a/app/Livewire/Project/Shared/ScheduledTask/All.php
+++ b/app/Livewire/Project/Shared/ScheduledTask/All.php
@@ -10,102 +10,23 @@
class All extends Component
{
public $resource;
- public bool $showPreview = false;
public string|null $modalId = null;
public ?string $variables = null;
- public ?string $variablesPreview = null;
+ public array $parameters;
protected $listeners = ['refreshTasks', 'saveScheduledTask' => 'submit'];
public function mount()
{
- $resourceClass = get_class($this->resource);
- $resourceWithPreviews = ['App\Models\Application'];
- $simpleDockerfile = !is_null(data_get($this->resource, 'dockerfile'));
- if (Str::of($resourceClass)->contains($resourceWithPreviews) && !$simpleDockerfile) {
- $this->showPreview = true;
- }
+ $this->parameters = get_route_parameters();
$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()
{
$this->resource->refresh();
- $this->getDevView();
}
public function submit($data)
{
- error_log("** submitting the beast");
try {
$task = new ScheduledTask();
$task->name = $data['name'];
diff --git a/app/Livewire/Project/Shared/ScheduledTask/Show.php b/app/Livewire/Project/Shared/ScheduledTask/Show.php
index b65c36531..23cb0e41a 100644
--- a/app/Livewire/Project/Shared/ScheduledTask/Show.php
+++ b/app/Livewire/Project/Shared/ScheduledTask/Show.php
@@ -4,47 +4,52 @@
use App\Models\ScheduledTask as ModelsScheduledTask;
use Livewire\Component;
+use App\Models\Application;
+use App\Models\Service;
use Visus\Cuid2\Cuid2;
class Show extends Component
{
public $parameters;
+ public Application|Service $resource;
public ModelsScheduledTask $task;
public ?string $modalId = null;
- public bool $isDisabled = false;
- public bool $isLocked = false;
public string $type;
protected $rules = [
'task.name' => 'required|string',
'task.command' => 'required|string',
+ 'task.frequency' => 'required|string',
+ 'task.container' => 'nullable|string',
];
protected $validationAttributes = [
- 'name' => 'Name',
- 'command' => 'Command',
+ 'name' => 'name',
+ 'command' => 'command',
+ 'frequency' => 'frequency',
+ 'container' => 'container',
];
public function mount()
{
- $this->modalId = new Cuid2(7);
$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();
+ }
+
+ $this->modalId = new Cuid2(7);
+ $this->task = ModelsScheduledTask::where('uuid', request()->route('task_uuid'))->first();
}
- public function lock()
- {
- $this->task->is_shown_once = true;
- $this->task->save();
- $this->dispatch('refreshTasks');
- }
- public function instantSave()
- {
- $this->submit();
- }
public function submit()
{
$this->validate();
$this->task->save();
- $this->dispatch('success', 'Environment variable updated successfully.');
+ $this->dispatch('success', 'Scheduled task updated successfully.');
$this->dispatch('refreshTasks');
}
@@ -52,7 +57,14 @@ public function delete()
{
try {
$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) {
return handleError($e);
}
diff --git a/app/Models/ScheduledTask.php b/app/Models/ScheduledTask.php
index 79468dd2a..2ff391c59 100644
--- a/app/Models/ScheduledTask.php
+++ b/app/Models/ScheduledTask.php
@@ -2,87 +2,27 @@
namespace App\Models;
-use App\Models\ScheduledTask as ModelsScheduledTask;
-use Illuminate\Database\Eloquent\Casts\Attribute;
-use Illuminate\Database\Eloquent\Model;
-use Illuminate\Support\Str;
+use Illuminate\Database\Eloquent\Relations\HasMany;
+use Illuminate\Database\Eloquent\Relations\HasOne;
class ScheduledTask extends BaseModel
{
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()
{
return $this->belongsTo(Service::class);
}
- // protected function value(): Attribute
- // {
- // 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
+ public function application()
{
- error_log("** in get_scheduled_tasks");
- // // $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;
+ return $this->belongsTo(Application::class);
}
-
- private function set_scheduled_tasks(?string $scheduled_task = null): string|null
+ public function latest_log(): HasOne
{
- error_log("** in set_scheduled_tasks");
- // if (is_null($scheduled_task) && $scheduled_task == '') {
- // return null;
- // }
- // $scheduled_task = trim($scheduled_task);
- // return encrypt($scheduled_task);
+ return $this->hasOne(ScheduledTaskExecution::class)->latest();
+ }
+ public function executions(): HasMany
+ {
+ return $this->hasMany(ScheduledTaskExecution::class);
}
-
- // protected function key(): Attribute
- // {
- // error_log("** in key()");
-
- // // return Attribute::make(
- // // set: fn (string $value) => Str::of($value)->trim(),
- // // );
- // }
}
diff --git a/resources/views/livewire/project/shared/scheduled-task/all.blade.php b/resources/views/livewire/project/shared/scheduled-task/all.blade.php
index 1c42cdc00..7a1f359ef 100644
--- a/resources/views/livewire/project/shared/scheduled-task/all.blade.php
+++ b/resources/views/livewire/project/shared/scheduled-task/all.blade.php
@@ -1,16 +1,31 @@
-
-
-
-
Scheduled Tasks
- + Add
-
-
-
Scheduled Tasks for this resource.
+
+
+
Scheduled Tasks
+ + Add
+
- @forelse ($resource->scheduled_tasks as $task)
-
- @empty
-
No scheduled tasks found.
- @endforelse
+
+
+
+ {{-- @if ($type === 'service-database' && $selectedBackup)
+
+
+
Executions
+
+
+ @endif --}}
diff --git a/resources/views/livewire/project/shared/scheduled-task/show.blade.php b/resources/views/livewire/project/shared/scheduled-task/show.blade.php
index d3a9bce47..a53ed6fbb 100644
--- a/resources/views/livewire/project/shared/scheduled-task/show.blade.php
+++ b/resources/views/livewire/project/shared/scheduled-task/show.blade.php
@@ -5,17 +5,32 @@
class="font-bold text-warning">({{ $task->name }})?
-
+
+
+
diff --git a/resources/views/livewire/project/shared/scheduled-task/all.blade.php b/resources/views/livewire/project/shared/scheduled-task/all.blade.php
index 7a1f359ef..b860e00b1 100644
--- a/resources/views/livewire/project/shared/scheduled-task/all.blade.php
+++ b/resources/views/livewire/project/shared/scheduled-task/all.blade.php
@@ -8,24 +8,18 @@
-
- {{-- @if ($type === 'service-database' && $selectedBackup)
-
-
-
Executions
-
-
- @endif --}}
diff --git a/resources/views/livewire/project/shared/scheduled-task/executions.blade.php b/resources/views/livewire/project/shared/scheduled-task/executions.blade.php
new file mode 100644
index 000000000..9f0fd9208
--- /dev/null
+++ b/resources/views/livewire/project/shared/scheduled-task/executions.blade.php
@@ -0,0 +1,27 @@
+
+ @forelse($executions as $execution)
+
data_get($execution, 'status') === 'success',
+ 'border-red-500' => data_get($execution, 'status') === 'failed',
+ ])>
+ @if (data_get($execution, 'status') === 'running')
+
+
+
+ @endif
+ Status: {{ data_get($execution, 'status') }}
+ Started At: {{ data_get($execution, 'created_at') }}
+ @if (data_get($execution, 'id') == $selectedKey)
+ @if (data_get($execution, 'message'))
+ Output:
{{ data_get($execution, 'message') }}
+ @else
+ No output was recorded for this execution.
+ @endif
+ @endif
+
+
+ @empty
+
No executions found.
+ @endforelse
+
diff --git a/resources/views/livewire/project/shared/scheduled-task/show.blade.php b/resources/views/livewire/project/shared/scheduled-task/show.blade.php
index a53ed6fbb..e9ab765ee 100644
--- a/resources/views/livewire/project/shared/scheduled-task/show.blade.php
+++ b/resources/views/livewire/project/shared/scheduled-task/show.blade.php
@@ -7,7 +7,11 @@ class="font-bold text-warning">({{ $task->name }})?
Scheduled Backup
+ @if ($type === 'application')
+ @elseif ($type === 'service')
+
+ @endif