diff --git a/app/Jobs/ServerOverflowJob.php b/app/Jobs/ServerOverflowJob.php new file mode 100644 index 000000000..af7b7a927 --- /dev/null +++ b/app/Jobs/ServerOverflowJob.php @@ -0,0 +1,62 @@ +team->uuid))]; + } + + public function uniqueId(): int + { + return $this->team->uuid; + } + + public function handle() + { + try { + ray('ServerOverflowJob'); + $servers = $this->team->servers; + $servers_count = $servers->count(); + $limit = $this->team->limits['serverLimit']; + $number_of_servers_to_disable = $servers_count - $limit; + ray($number_of_servers_to_disable, $servers_count, $limit); + if ($number_of_servers_to_disable > 0) { + ray('Disabling servers'); + $servers = $servers->sortBy('created_at'); + $servers_to_disable = $servers->take($number_of_servers_to_disable); + $servers_to_disable->each(function ($server) { + $server->disableServerDueToOverflow(); + $this->team->notify(new DisabledDueToOverflow($server)); + }); + } + } catch (\Throwable $e) { + send_internal_notification('ServerOverflowJob failed with: ' . $e->getMessage()); + ray($e->getMessage()); + return handleError($e); + } + } + +} diff --git a/app/Models/Server.php b/app/Models/Server.php index 016efd5f7..4272761a5 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -151,6 +151,11 @@ class Server extends BaseModel } return false; } + public function disableServerDueToOverflow() { + $this->settings->update([ + 'disabled_by_overflow' => true, + ]); + } public function isServerReady(int $tries = 3) { if ($this->skipServer()) { diff --git a/app/Notifications/Server/DisabledDueToOverflow.php b/app/Notifications/Server/DisabledDueToOverflow.php new file mode 100644 index 000000000..957d9c866 --- /dev/null +++ b/app/Notifications/Server/DisabledDueToOverflow.php @@ -0,0 +1,63 @@ +subject("Coolify: Server ({$this->server->name}) disabled because it is not paid!"); + $mail->view('emails.server-disabled-due-to-overflow', [ + 'name' => $this->server->name, + ]); + return $mail; + } + + public function toDiscord(): string + { + $message = "Coolify: Server ({$this->server->name}) disabled because it is not paid!\n All automations and integrations are stopped.\nPlease update your subscription to enable the server again [here](https://app.coolify.io/subsciprtions)."; + return $message; + } + public function toTelegram(): array + { + return [ + "message" => "Coolify: Server ({$this->server->name}) disabled because it is not paid!\n All automations and integrations are stopped.\nPlease update your subscription to enable the server again [here](https://app.coolify.io/subsciprtions)." + ]; + } +} diff --git a/database/migrations/2024_02_25_222150_add_disabled_server_due_to_overflow.php b/database/migrations/2024_02_25_222150_add_disabled_server_due_to_overflow.php new file mode 100644 index 000000000..3d1b808ab --- /dev/null +++ b/database/migrations/2024_02_25_222150_add_disabled_server_due_to_overflow.php @@ -0,0 +1,28 @@ +boolean('disabled_by_overflow')->default(false); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('server_settings', function (Blueprint $table) { + $table->dropColumn('disabled_by_overflow'); + }); + } +}; diff --git a/resources/views/emails/server-disabled-due-to-overflow.blade.php b/resources/views/emails/server-disabled-due-to-overflow.blade.php new file mode 100644 index 000000000..7cc43c354 --- /dev/null +++ b/resources/views/emails/server-disabled-due-to-overflow.blade.php @@ -0,0 +1,5 @@ + +Your server ({{ $name }}) disabled because it is not paid! All automations and integrations are stopped. + +Please update your subscription to enable the server again [here](https://app.coolify.io/subsciprtions). + diff --git a/routes/webhooks.php b/routes/webhooks.php index 03bbfd999..e8fc64713 100644 --- a/routes/webhooks.php +++ b/routes/webhooks.php @@ -3,6 +3,7 @@ use App\Enums\ProcessStatus; use App\Jobs\ApplicationPullRequestUpdateJob; use App\Jobs\GithubAppPermissionJob; +use App\Jobs\ServerOverflowJob; use App\Jobs\SubscriptionInvoiceFailedJob; use App\Jobs\SubscriptionTrialEndedJob; use App\Jobs\SubscriptionTrialEndsSoonJob; @@ -882,6 +883,7 @@ Route::post('/payments/stripe/events', function () { $team->update([ 'custom_server_limit' => $quantity, ]); + ServerOverflowJob::dispatch($team); } $subscription->update([ 'stripe_feedback' => $feedback,