Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

Start scheduled task job execution.

Stuart Rowlands9bbe9567c71b
diff --git a/app/Jobs/ScheduledTaskJob.php b/app/Jobs/ScheduledTaskJob.php
new file mode 100644
index 0000000000..f810e746a7
--- /dev/null
+++ b/app/Jobs/ScheduledTaskJob.php
@@ -0,0 +1,77 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Models\ScheduledTask;
+use App\Models\Server;
+use App\Models\Application;
+use App\Models\Service;
+use Carbon\Carbon;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\Middleware\WithoutOverlapping;
+use Illuminate\Queue\SerializesModels;
+use Illuminate\Support\Str;
+use Throwable;
+
+class ScheduledTaskJob implements ShouldQueue
+{
+ use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+ public ?Team $team = null;
+ public Server $server;
+ public ScheduledTask $task;
+ public Application|Service $resource;
+
+ public ?string $container_name = null;
+ public ?string $directory_name = null;
+ public ?ScheduledTaskExecution $backup_log = null;
+ public string $task_status = 'failed';
+ public int $size = 0;
+ public ?string $backup_output = null;
+ public ?S3Storage $s3 = null;
+
+ public function __construct($task)
+ {
+ $this->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 0000000000..de13fefb05
--- /dev/null
+++ b/app/Models/ScheduledTaskExecution.php
@@ -0,0 +1,15 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Relations\BelongsTo;
+
+class ScheduledTaskExecution extends BaseModel
+{
+ protected $guarded = [];
+
+ public function scheduledTask(): BelongsTo
+ {
+ return $this->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 0000000000..27ace08d4c
--- /dev/null
+++ b/database/migrations/2024_01_01_231053_create_scheduled_task_executions_table.php
@@ -0,0 +1,31 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+ /**
+ * Run the migrations.
+ */
+ public function up(): void
+ {
+ Schema::create('scheduled_task_executions', function (Blueprint $table) {
+ $table->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');
+ }
+};