59 lines
3.1 KiB
PHP
59 lines
3.1 KiB
PHP
<?php
|
|
|
|
use App\Services\ReplenishmentQueue;
|
|
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__);
|
|
if (count($argv) !== 2 || ! in_array($argv[1], ['--check', '--once'], true)) {
|
|
throw new LogicException('Choose read-only preflight or authorized native passage.');
|
|
}
|
|
$check = $argv[1] === '--check';
|
|
$planPath = $root.'/docs/proofs/replenishment-task-proposal.json';
|
|
$plan = json_decode(file_get_contents($planPath), true, 32, JSON_THROW_ON_ERROR);
|
|
$settings = json_decode(file_get_contents('C:/laragon/data/sgf-tempo/secrets/payables-demo-compras.json'), true, 32, JSON_THROW_ON_ERROR);
|
|
if ($settings['APP_ENV'] !== 'payable-testing' || $settings['DB_HOST'] !== '127.0.0.1' || (string) $settings['DB_PORT'] !== '5432'
|
|
|| realpath($settings['HOST_ROOT']) !== realpath($root)
|
|
|| ! preg_match('/^compras_receipts_test_([a-f0-9]{12})$/D', $settings['DB_DATABASE'], $match)
|
|
|| $settings['DB_USERNAME'] !== 'payable_qa_'.$match[1]
|
|
|| ($settings['COMPRAS_RUNTIME_ENABLED'] ?? null) !== 'true'
|
|
|| $settings['COMPRAS_RUNTIME_INSTALLATION_ID'] !== $plan['installation_id']) {
|
|
throw new LogicException('Existing standalone pilot required.');
|
|
}
|
|
foreach ($plan['source_sha256'] as $file => $hash) {
|
|
if (! hash_equals($hash, hash_file('sha256', $root.'/'.$file))) {
|
|
throw new LogicException('Reviewed replenishment task source changed.');
|
|
}
|
|
}
|
|
if (! $check) {
|
|
$approval = json_decode(file_get_contents($root.'/docs/proofs/replenishment-task-authorization.json'), true, 32, JSON_THROW_ON_ERROR);
|
|
if (($approval['state'] ?? null) !== 'approved' || ($approval['scope'] ?? null) !== 'existing_pilot_replenishment_task'
|
|
|| ! hash_equals($approval['proposal_sha256'], hash_file('sha256', $planPath))) {
|
|
throw new LogicException('Replenishment passage not authorized.');
|
|
}
|
|
}
|
|
foreach ([...$settings, 'DB_CONNECTION' => 'pgsql', 'DB_URL' => '', 'PGOPTIONS' => $check ? '-c default_transaction_read_only=on' : '',
|
|
'CACHE_STORE' => $check ? 'array' : ($settings['CACHE_STORE'] ?? 'file'), '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(ReplenishmentQueue::class)->ready()) {
|
|
throw new LogicException('Native replenishment capability unavailable.');
|
|
}
|
|
if ($check) {
|
|
if (DB::selectOne('SHOW default_transaction_read_only')->default_transaction_read_only !== 'on') {
|
|
throw new LogicException('Read-only task preflight required.');
|
|
}
|
|
echo json_encode(['state' => 'ready', 'database_enforced_read_only' => true,
|
|
'health' => app(ReplenishmentQueue::class)->health(), 'task_started' => false], JSON_THROW_ON_ERROR).PHP_EOL;
|
|
exit;
|
|
}
|
|
// The native command owns process exclusion, durable recovery and bound heartbeat.
|
|
exit(Artisan::call('sgc:replenishment-tick'));
|