2023-12-13 11:35:53 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
2025-09-09 16:46:24 +02:00
|
|
|
use App\Jobs\CheckHelperImageJob;
|
2023-12-13 11:35:53 +01:00
|
|
|
use App\Models\InstanceSettings;
|
2025-11-11 12:32:52 +01:00
|
|
|
use App\Models\ScheduledDatabaseBackupExecution;
|
|
|
|
|
use App\Models\ScheduledTaskExecution;
|
|
|
|
|
use Carbon\Carbon;
|
2023-12-13 11:35:53 +01:00
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
use Illuminate\Support\Facades\Artisan;
|
|
|
|
|
|
|
|
|
|
class Dev extends Command
|
|
|
|
|
{
|
2025-03-31 17:38:50 +02:00
|
|
|
protected $signature = 'dev {--init}';
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-07-06 14:33:59 +02:00
|
|
|
protected $description = 'Helper commands for development.';
|
2023-12-13 11:35:53 +01:00
|
|
|
|
|
|
|
|
public function handle()
|
2024-07-06 14:33:59 +02:00
|
|
|
{
|
|
|
|
|
if ($this->option('init')) {
|
|
|
|
|
$this->init();
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function init()
|
2023-12-13 11:35:53 +01:00
|
|
|
{
|
|
|
|
|
// Generate APP_KEY if not exists
|
2024-07-06 14:33:59 +02:00
|
|
|
|
2024-11-12 15:53:05 +01:00
|
|
|
if (empty(config('app.key'))) {
|
2026-03-28 12:07:34 +01:00
|
|
|
echo " INFO Generating APP_KEY.\n";
|
2023-12-13 11:35:53 +01:00
|
|
|
Artisan::call('key:generate');
|
|
|
|
|
}
|
2024-07-21 13:30:28 -03:00
|
|
|
|
|
|
|
|
// Generate STORAGE link if not exists
|
|
|
|
|
if (! file_exists(public_path('storage'))) {
|
2026-03-28 12:07:34 +01:00
|
|
|
echo " INFO Generating storage link.\n";
|
2024-07-21 13:30:28 -03:00
|
|
|
Artisan::call('storage:link');
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-13 11:35:53 +01:00
|
|
|
// Seed database if it's empty
|
2025-01-07 15:31:43 +01:00
|
|
|
$settings = InstanceSettings::find(0);
|
2024-06-10 20:43:34 +00:00
|
|
|
if (! $settings) {
|
2026-03-28 12:07:34 +01:00
|
|
|
echo " INFO Initializing instance, seeding database.\n";
|
2023-12-13 11:35:53 +01:00
|
|
|
Artisan::call('migrate --seed');
|
|
|
|
|
} else {
|
2026-03-28 12:07:34 +01:00
|
|
|
echo " INFO Instance already initialized.\n";
|
2023-12-13 11:35:53 +01:00
|
|
|
}
|
2025-11-10 11:11:18 +01:00
|
|
|
|
|
|
|
|
// Clean up stuck jobs and stale locks on development startup
|
|
|
|
|
try {
|
2026-03-28 12:07:34 +01:00
|
|
|
echo " INFO Cleaning up Redis (stuck jobs and stale locks)...\n";
|
2025-11-10 11:11:18 +01:00
|
|
|
Artisan::call('cleanup:redis', ['--restart' => true, '--clear-locks' => true]);
|
2026-03-28 12:07:34 +01:00
|
|
|
echo " INFO Redis cleanup completed.\n";
|
2025-11-10 11:11:18 +01:00
|
|
|
} catch (\Throwable $e) {
|
2026-03-28 12:07:34 +01:00
|
|
|
echo " ERROR Redis cleanup failed: {$e->getMessage()}\n";
|
2025-11-10 11:11:18 +01:00
|
|
|
}
|
|
|
|
|
|
2025-11-11 12:32:52 +01:00
|
|
|
try {
|
|
|
|
|
$updatedTaskCount = ScheduledTaskExecution::where('status', 'running')->update([
|
|
|
|
|
'status' => 'failed',
|
|
|
|
|
'message' => 'Marked as failed during Coolify startup - job was interrupted',
|
|
|
|
|
'finished_at' => Carbon::now(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if ($updatedTaskCount > 0) {
|
2026-03-28 12:07:34 +01:00
|
|
|
echo " INFO Marked {$updatedTaskCount} stuck scheduled task executions as failed.\n";
|
2025-11-11 12:32:52 +01:00
|
|
|
}
|
|
|
|
|
} catch (\Throwable $e) {
|
2026-03-28 12:07:34 +01:00
|
|
|
echo " ERROR Could not clean up stuck scheduled task executions: {$e->getMessage()}\n";
|
2025-11-11 12:32:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$updatedBackupCount = ScheduledDatabaseBackupExecution::where('status', 'running')->update([
|
|
|
|
|
'status' => 'failed',
|
|
|
|
|
'message' => 'Marked as failed during Coolify startup - job was interrupted',
|
|
|
|
|
'finished_at' => Carbon::now(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if ($updatedBackupCount > 0) {
|
2026-03-28 12:07:34 +01:00
|
|
|
echo " INFO Marked {$updatedBackupCount} stuck database backup executions as failed.\n";
|
2025-11-11 12:32:52 +01:00
|
|
|
}
|
|
|
|
|
} catch (\Throwable $e) {
|
2026-03-28 12:07:34 +01:00
|
|
|
echo " ERROR Could not clean up stuck database backup executions: {$e->getMessage()}\n";
|
2025-11-11 12:32:52 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-09 16:46:24 +02:00
|
|
|
CheckHelperImageJob::dispatch();
|
2023-12-13 11:35:53 +01:00
|
|
|
}
|
|
|
|
|
}
|