From 9bbe9567c71b9a6baed9408357d47a3816590a5b Mon Sep 17 00:00:00 2001 From: Stuart Rowlands Date: Mon, 1 Jan 2024 18:23:58 -0800 Subject: [PATCH] Start scheduled task job execution. --- app/Jobs/ScheduledTaskJob.php | 77 +++++++++++++++++++ app/Models/ScheduledTaskExecution.php | 15 ++++ ...create_scheduled_task_executions_table.php | 31 ++++++++ 3 files changed, 123 insertions(+) create mode 100644 app/Jobs/ScheduledTaskJob.php create mode 100644 app/Models/ScheduledTaskExecution.php create mode 100644 database/migrations/2024_01_01_231053_create_scheduled_task_executions_table.php diff --git a/app/Jobs/ScheduledTaskJob.php b/app/Jobs/ScheduledTaskJob.php new file mode 100644 index 000000000..f810e746a --- /dev/null +++ b/app/Jobs/ScheduledTaskJob.php @@ -0,0 +1,77 @@ +task = $task; + if ($service = $task->service()->first()) { + $this->resource = $service; + } else if ($application = $task->application()->first()) { + $this->resource = $application; + } + } + + public function middleware(): array + { + return [new WithoutOverlapping($this->task->id)]; + } + + public function uniqueId(): int + { + return $this->task->id; + } + + public function handle(): void + { + file_put_contents('/tmp/scheduled-job-run', 'ran in handle'); + try { + echo($this->resource->type()); + file_put_contents('/tmp/scheduled-job-run-'.$this->task->id, $this->task->name); + } catch (\Throwable $e) { + send_internal_notification('ScheduledTaskJob failed with: ' . $e->getMessage()); + throw $e; + } finally { + // BackupCreated::dispatch($this->team->id); + } + } + private function add_to_backup_output($output): void + { + if ($this->backup_output) { + $this->backup_output = $this->backup_output . "\n" . $output; + } else { + $this->backup_output = $output; + } + } +} diff --git a/app/Models/ScheduledTaskExecution.php b/app/Models/ScheduledTaskExecution.php new file mode 100644 index 000000000..de13fefb0 --- /dev/null +++ b/app/Models/ScheduledTaskExecution.php @@ -0,0 +1,15 @@ +belongsTo(ScheduledTask::class); + } +} diff --git a/database/migrations/2024_01_01_231053_create_scheduled_task_executions_table.php b/database/migrations/2024_01_01_231053_create_scheduled_task_executions_table.php new file mode 100644 index 000000000..27ace08d4 --- /dev/null +++ b/database/migrations/2024_01_01_231053_create_scheduled_task_executions_table.php @@ -0,0 +1,31 @@ +id(); + $table->string('uuid')->unique(); + $table->enum('status', ['success', 'failed', 'running'])->default('running'); + $table->longText('message')->nullable(); + $table->foreignId('scheduled_task_id'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('scheduled_task_executions'); + } +};