85 lines
4.1 KiB
PHP
85 lines
4.1 KiB
PHP
<?php
|
|
|
|
use App\Services\RequisitionExpiry;
|
|
use App\Services\StandaloneRuntime;
|
|
use Illuminate\Contracts\Console\Kernel;
|
|
use Illuminate\Support\Facades\Artisan;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
require dirname(__DIR__).'/vendor/autoload.php';
|
|
$root = dirname(__DIR__);
|
|
$check = in_array('--check', $argv, true);
|
|
$once = in_array('--once', $argv, true);
|
|
if ($check === $once) {
|
|
throw new LogicException('Choose read-only check or explicitly authorised single passage.');
|
|
}
|
|
$planPath = $root.'/docs/proofs/approval-deadlines-task-proposal.json';
|
|
$plan = json_decode(file_get_contents($planPath), true, 32, JSON_THROW_ON_ERROR);
|
|
$source = json_decode(file_get_contents('C:/laragon/data/sgf-tempo/secrets/payables-demo-compras.json'), true, 32, JSON_THROW_ON_ERROR);
|
|
if ($source['APP_ENV'] !== 'payable-testing' || $source['DB_HOST'] !== '127.0.0.1'
|
|
|| realpath($source['HOST_ROOT']) !== realpath($root)
|
|
|| ! preg_match('/^compras_receipts_test_([a-f0-9]{12})$/D', $source['DB_DATABASE'], $match)
|
|
|| $source['DB_USERNAME'] !== 'payable_qa_'.$match[1]
|
|
|| ($source['COMPRAS_RUNTIME_ENABLED'] ?? null) !== 'true'
|
|
|| $source['COMPRAS_RUNTIME_INSTALLATION_ID'] !== $plan['installation_id']) {
|
|
throw new LogicException('Approval deadline evaluation target mismatch.');
|
|
}
|
|
if ($once) {
|
|
$approvalPath = $root.'/docs/proofs/approval-deadlines-task-authorization.json';
|
|
$approval = is_file($approvalPath) ? json_decode(file_get_contents($approvalPath), true, 32, JSON_THROW_ON_ERROR) : [];
|
|
if (($approval['state'] ?? null) !== 'approved' || ($approval['scope'] ?? null) !== 'existing_pilot_approval_deadline_task'
|
|
|| ! hash_equals($approval['proposal_sha256'], hash_file('sha256', $planPath))) {
|
|
throw new LogicException('APPROVAL_DEADLINE_TASK_NOT_AUTHORIZED');
|
|
}
|
|
}
|
|
foreach ([...$source, 'CACHE_STORE' => $check ? 'array' : ($source['CACHE_STORE'] ?? 'file'),
|
|
'PGOPTIONS' => $check ? '-c default_transaction_read_only=on' : '', 'DB_CONNECTION' => 'pgsql', 'DB_URL' => '',
|
|
'LOG_CHANNEL' => 'stderr'] as $key => $value) {
|
|
putenv($key.'='.$value);
|
|
$_ENV[$key] = $_SERVER[$key] = $value;
|
|
}
|
|
$app = require $root.'/bootstrap/app.php';
|
|
$app->make(Kernel::class)->bootstrap();
|
|
app(StandaloneRuntime::class)->assertLoadedPackages();
|
|
if (! app(RequisitionExpiry::class)->ready()) {
|
|
throw new LogicException('Approval deadline capability unavailable.');
|
|
}
|
|
if ($check) {
|
|
if (DB::selectOne('SHOW default_transaction_read_only')->default_transaction_read_only !== 'on') {
|
|
throw new LogicException('Deadline task preflight must be read-only.');
|
|
}
|
|
echo json_encode(['state' => 'ready', 'database_enforced_read_only' => true, 'installation_verified' => true,
|
|
'pending_requests_with_configured_cycle_policy' => DB::table('requisitions')->where('status', 'pending')->whereNotNull('approval_snapshot->approval_expiry')->count(),
|
|
'business_commands' => false, 'task_installed_or_started' => false], JSON_THROW_ON_ERROR).PHP_EOL;
|
|
exit;
|
|
}
|
|
$lock = fopen($root.'/storage/framework/approval-deadlines-evaluation.lock', 'c');
|
|
if ($lock === false) {
|
|
throw new RuntimeException('Deadline task lock unavailable.');
|
|
}
|
|
if (! flock($lock, LOCK_EX | LOCK_NB)) {
|
|
fclose($lock);
|
|
exit(0);
|
|
}
|
|
$status = ['started_at' => gmdate('c'), 'result' => 'running', 'scope' => 'Compras pilot 8005 approval deadlines'];
|
|
$statusPath = $root.'/storage/framework/approval-deadlines-status.json';
|
|
$save = static function (array $state) use ($statusPath): void {
|
|
$temporary = $statusPath.'.'.bin2hex(random_bytes(4)).'.tmp';
|
|
file_put_contents($temporary, json_encode($state, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
|
|
if (! rename($temporary, $statusPath)) {
|
|
throw new RuntimeException('Deadline task status unavailable.');
|
|
}
|
|
};
|
|
try {
|
|
$save($status);
|
|
$exitCode = Artisan::call('sgc:approval-deadlines-tick');
|
|
$status += ['finished_at' => gmdate('c'), 'exit_code' => $exitCode];
|
|
$status['result'] = $exitCode === 0 ? 'passed' : 'retry_pending';
|
|
$save($status);
|
|
} finally {
|
|
flock($lock, LOCK_UN);
|
|
fclose($lock);
|
|
}
|
|
echo json_encode($status, JSON_THROW_ON_ERROR).PHP_EOL;
|
|
exit($exitCode);
|