coolify/app/Console/Commands/Dev.php

92 lines
2.9 KiB
PHP
Raw Permalink Normal View History

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