Refactor application status update logic and add complex_status column

This commit is contained in:
Andras Bacsai 2024-02-06 15:42:31 +01:00
parent 3616fc8ca9
commit 78b194cb16
5 changed files with 40 additions and 12 deletions

View File

@ -126,7 +126,16 @@ public function handle()
$foundApplications[] = $application->id;
$statusFromDb = $application->status;
if ($statusFromDb !== $containerStatus) {
// if ($application->additional_networks->count() > 0) {
// }
// if (!str($containerStatus)->contains('running')) {
// $application->update(['status' => 'degraded']);
// } else {
// $application->update(['status' => $containerStatus]);
// }
// } else {
$application->update(['status' => $containerStatus]);
// }
}
} else {
//Notify user that this container should not be there.
@ -160,10 +169,9 @@ public function handle()
// Notify user that this container should not be there.
}
}
if (data_get($container,'Name') === '/coolify-db') {
if (data_get($container, 'Name') === '/coolify-db') {
$foundDatabases[] = 0;
}
}
$serviceLabelId = data_get($labels, 'coolify.serviceId');
if ($serviceLabelId) {

View File

@ -29,14 +29,19 @@ public function mount()
public function check_status($showNotification = false)
{
if ($this->application->destination->server->isFunctional()) {
dispatch(new ContainerStatusJob($this->application->destination->server));
$this->application->refresh();
$this->application->previews->each(function ($preview) {
$preview->refresh();
});
} else {
dispatch(new ServerStatusJob($this->application->destination->server));
$all_servers = collect([]);
$all_servers = $all_servers->push($this->application->destination->server);
$all_servers = $all_servers->merge($this->application->additional_servers);
foreach ($all_servers as $server) {
if ($server->isFunctional()) {
dispatch(new ContainerStatusJob($server));
$this->application->refresh();
$this->application->previews->each(function ($preview) {
$preview->refresh();
});
} else {
dispatch(new ServerStatusJob($this->application->destination->server));
}
}
if ($showNotification) $this->dispatch('success', "Application status updated.");
}

View File

@ -15,6 +15,9 @@ class Application extends BaseModel
{
use SoftDeletes;
protected $guarded = [];
// protected $casts = [
// 'complex_status' => 'json',
// ];
protected static function booted()
{
static::saving(function ($application) {

View File

@ -9,6 +9,7 @@
use App\Notifications\Server\Unreachable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Facades\DB;
use Spatie\SchemalessAttributes\Casts\SchemalessAttributes;
use Spatie\SchemalessAttributes\SchemalessAttributesTrait;
use Illuminate\Support\Str;
@ -248,9 +249,17 @@ public function databases()
}
public function applications()
{
return $this->destinations()->map(function ($standaloneDocker) {
$applications = $this->destinations()->map(function ($standaloneDocker) {
return $standaloneDocker->applications;
})->flatten();
$additionalApplicationIds = DB::table('additional_destinations')->where('server_id', $this->id)->get('application_id');
$additionalApplicationIds = collect($additionalApplicationIds)->map(function ($item) {
return $item->application_id;
});
Application::whereIn('id', $additionalApplicationIds)->get()->each(function ($application) use ($applications) {
$applications->push($application);
});
return $applications;
}
public function dockerComposeBasedApplications()
{
@ -300,7 +309,8 @@ public function destinations()
{
$standalone_docker = $this->hasMany(StandaloneDocker::class)->get();
$swarm_docker = $this->hasMany(SwarmDocker::class)->get();
return $standalone_docker->concat($swarm_docker);
$asd = $this->belongsToMany(StandaloneDocker::class, 'additional_destinations')->withPivot('server_id')->get();
return $standalone_docker->concat($swarm_docker)->concat($asd);
}
public function standaloneDockers()

View File

@ -20,6 +20,7 @@ public function up(): void
});
Schema::table('applications', function (Blueprint $table) {
$table->dropColumn('additional_destinations');
$table->text('complex_status')->nullable();
});
}
@ -31,6 +32,7 @@ public function down(): void
Schema::dropIfExists('additional_destinations');
Schema::table('applications', function (Blueprint $table) {
$table->string('additional_destinations')->nullable()->after('destination');
$table->dropColumn('complex_status');
});
}
};