You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.8 KiB
PHP
66 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudInstance;
|
|
use App\Models\CloudVpc;
|
|
use App\Models\CloudVpcPort;
|
|
|
|
/**
|
|
* Canonical row-lock entry points for VPC mutations.
|
|
*
|
|
* When present, instance rows are acquired in ascending id order, followed by
|
|
* the VPC row, VPC ports, host projection rows, and finally coordination jobs.
|
|
* Reference reads may discover ids but must not use FOR UPDATE out of order.
|
|
*/
|
|
final class CloudVpcLockOrder
|
|
{
|
|
public const ORDER = [
|
|
'instances', 'vpc', 'ports', 'host_states', 'coordination',
|
|
];
|
|
|
|
public static function instances(array $instanceIds)
|
|
{
|
|
$ids = array_values(array_unique(array_map('intval', $instanceIds)));
|
|
sort($ids, SORT_NUMERIC);
|
|
if ($ids === [] || $ids[0] < 1) {
|
|
throw new \InvalidArgumentException('VPC instance locks need ids');
|
|
}
|
|
return CloudInstance::query()->whereIn('id', $ids)->orderBy('id')
|
|
->lockForUpdate()->get()->keyBy('id');
|
|
}
|
|
|
|
public static function instance(int $instanceId): ?CloudInstance
|
|
{
|
|
if ($instanceId < 1) {
|
|
return null;
|
|
}
|
|
return CloudInstance::query()->where('id', $instanceId)
|
|
->lockForUpdate()->first();
|
|
}
|
|
|
|
public static function vpcByKey(string $vpcKey): ?CloudVpc
|
|
{
|
|
return CloudVpc::query()->where('vpc_key', $vpcKey)
|
|
->lockForUpdate()->first();
|
|
}
|
|
|
|
public static function vpcById(int $vpcId): ?CloudVpc
|
|
{
|
|
if ($vpcId < 1) {
|
|
return null;
|
|
}
|
|
return CloudVpc::query()->where('id', $vpcId)
|
|
->lockForUpdate()->first();
|
|
}
|
|
|
|
public static function portById(int $portId): ?CloudVpcPort
|
|
{
|
|
if ($portId < 1) {
|
|
return null;
|
|
}
|
|
return CloudVpcPort::query()->where('id', $portId)
|
|
->lockForUpdate()->first();
|
|
}
|
|
}
|