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.
65 lines
2.7 KiB
PHP
65 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
/** Canonical SQL predicates for administrator recovery inventories. */
|
|
final class CloudJobRecoverySql
|
|
{
|
|
public static function pendingSql(string $alias): string
|
|
{
|
|
return '(' . self::incidentSql($alias) . ' OR '
|
|
. CloudJobService::terminalRecoveryStateConflictSql($alias) . ')';
|
|
}
|
|
|
|
public static function resourceLockedSql(string $alias): string
|
|
{
|
|
return '((' . $alias . ".phase = 'terminal' AND "
|
|
. $alias . ".outcome = 'failed' AND "
|
|
. $alias . ".effect_resolution = 'unknown' AND "
|
|
. $alias . ".kind <> 'host_storage_configure') OR "
|
|
. self::pendingSql($alias) . ')';
|
|
}
|
|
|
|
private static function incidentSql(string $alias): string
|
|
{
|
|
$supported = self::sqlList(
|
|
CloudOperationRecoveryPolicy::ledgerFinalizerKinds()
|
|
);
|
|
$repair = self::sqlList(CloudDispatchLedger::repairIncidentCodes());
|
|
$aggregateKinds = self::sqlList(CloudJobService::aggregateKinds());
|
|
$agent = '(' . $alias . ".execution_mode = 'agent' AND ("
|
|
. '(' . $alias . '.dispatch_attempts IS NULL OR '
|
|
. $alias . '.dispatch_attempts NOT IN (0, 1)) OR ('
|
|
. $alias . ".phase = 'terminal' AND "
|
|
. $alias . '.finalization_incident = 1 AND ('
|
|
. $alias . '.finalization_error_code IN (' . $repair . ') OR ('
|
|
. $alias . ".outcome = 'failed' AND "
|
|
. $alias . ".status = 'failed' AND "
|
|
. $alias . '.dispatch_attempts = 0 AND '
|
|
. $alias . '.kind IN (' . $supported . ')) OR ('
|
|
. $alias . '.dispatch_attempts IN (0, 1) AND '
|
|
. $alias . '.effect_resolution IN (\'applied\', \'not_applied\') '
|
|
. 'AND ' . $alias . '.kind IN (' . $supported . '))))))';
|
|
$aggregateIncident = '(' . $alias . ".phase = 'terminal' AND "
|
|
. $alias . '.parent_job_id IS NULL AND '
|
|
. $alias . '.finalization_incident = 1 AND '
|
|
. '(' . $alias . ".execution_mode = 'aggregate' OR "
|
|
. $alias . '.kind IN (' . $aggregateKinds . ') OR '
|
|
. $alias . '.finalization_error_code = '
|
|
. "'recovery_aggregate_state_conflict'))";
|
|
return '(' . $agent . ' OR '
|
|
. CloudJobService::aggregateLocalStateConflictSql($alias)
|
|
. ' OR ' . $aggregateIncident . ')';
|
|
}
|
|
|
|
/** @param list<string> $values */
|
|
private static function sqlList(array $values): string
|
|
{
|
|
return implode(', ', array_map(
|
|
static fn (string $value): string => "'"
|
|
. str_replace("'", "''", $value) . "'",
|
|
$values
|
|
));
|
|
}
|
|
}
|