2024-02-25 22:34:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
|
|
use App\Models\Team;
|
2024-02-26 09:25:21 +00:00
|
|
|
use App\Notifications\Server\ForceDisabled;
|
|
|
|
use App\Notifications\Server\ForceEnabled;
|
2024-02-25 22:34:01 +00:00
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\Middleware\WithoutOverlapping;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
2024-06-10 20:43:34 +00:00
|
|
|
class ServerLimitCheckJob implements ShouldBeEncrypted, ShouldQueue
|
2024-02-25 22:34:01 +00:00
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
|
|
public $tries = 4;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-25 22:34:01 +00:00
|
|
|
public function backoff(): int
|
|
|
|
{
|
|
|
|
return isDev() ? 1 : 3;
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-25 22:34:01 +00:00
|
|
|
public function __construct(public Team $team)
|
|
|
|
{
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-25 22:34:01 +00:00
|
|
|
public function middleware(): array
|
|
|
|
{
|
|
|
|
return [(new WithoutOverlapping($this->team->uuid))];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function uniqueId(): int
|
|
|
|
{
|
|
|
|
return $this->team->uuid;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$servers = $this->team->servers;
|
|
|
|
$servers_count = $servers->count();
|
2024-05-15 09:44:42 +00:00
|
|
|
$limit = data_get($this->team->limits, 'serverLimit', 2);
|
2024-02-25 22:34:01 +00:00
|
|
|
$number_of_servers_to_disable = $servers_count - $limit;
|
2024-02-26 09:25:21 +00:00
|
|
|
ray('ServerLimitCheckJob', $this->team->uuid, $servers_count, $limit, $number_of_servers_to_disable);
|
2024-02-25 22:34:01 +00:00
|
|
|
if ($number_of_servers_to_disable > 0) {
|
|
|
|
ray('Disabling servers');
|
2024-02-26 09:25:21 +00:00
|
|
|
$servers = $servers->sortbyDesc('created_at');
|
2024-02-25 22:34:01 +00:00
|
|
|
$servers_to_disable = $servers->take($number_of_servers_to_disable);
|
|
|
|
$servers_to_disable->each(function ($server) {
|
2024-02-26 09:25:21 +00:00
|
|
|
$server->forceDisableServer();
|
|
|
|
$this->team->notify(new ForceDisabled($server));
|
|
|
|
});
|
2024-06-10 20:43:34 +00:00
|
|
|
} elseif ($number_of_servers_to_disable === 0) {
|
2024-02-26 09:25:21 +00:00
|
|
|
$servers->each(function ($server) {
|
|
|
|
if ($server->isForceDisabled()) {
|
|
|
|
$server->forceEnableServer();
|
|
|
|
$this->team->notify(new ForceEnabled($server));
|
|
|
|
}
|
2024-02-25 22:34:01 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (\Throwable $e) {
|
2024-06-10 20:43:34 +00:00
|
|
|
send_internal_notification('ServerLimitCheckJob failed with: '.$e->getMessage());
|
2024-02-25 22:34:01 +00:00
|
|
|
ray($e->getMessage());
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-25 22:34:01 +00:00
|
|
|
return handleError($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|