Files
SGF/scripts/sgf-document-math-concurrency.php
T
2026-11-11 09:00:17 +02:00

8 lines
2.1 KiB
PHP

<?php
require __DIR__.'/sgf-document-math-proof-bootstrap.php';$dir=$s['root'].'/document-math-concurrency';if(!is_dir($dir))mkdir($dir,0700,true);$file=$dir.'/progress.json';$mode=$argv[1]??'';
if($mode==='prepare'){if(is_file($file))throw new LogicException('Reuse prior preparation');$p=$rulePreview(13,$documentInput('gross_included',[$line('CONCURRENT','100')]));file_put_contents($file,json_encode(['cut'=>$p['preview'],'before'=>$host->table('fr_calculations')->count()]));}
elseif($mode==='first'){$p=json_decode(file_get_contents($file),true);$selectRule(13);$host->beginTransaction();try{$out=$rules->confirm(13,$p['cut']);file_put_contents($dir.'/locked','Company lock held');$end=microtime(true)+25;while(!is_file($dir.'/release')&&microtime(true)<$end)usleep(50000);if(!is_file($dir.'/release'))throw new LogicException('Release missing');$host->commit();file_put_contents($dir.'/first.json',json_encode($out));}finally{while($host->transactionLevel()>0)$host->rollBack();}}
elseif($mode==='second'){$p=json_decode(file_get_contents($file),true);$selectRule(13);request()->request->replace(['preview'=>$p['cut']]);$start=microtime(true);$r=app(App\Modules\Services\Http\FiscalRulesController::class)->confirm(request(),$rules);file_put_contents($dir.'/second.json',json_encode(['status'=>$r->getStatusCode(),'retry_after'=>$r->headers->get('Retry-After'),'elapsed_seconds'=>microtime(true)-$start]));}
elseif($mode==='recover'){$p=json_decode(file_get_contents($file),true);$selectRule(13);$out=$rules->confirm(13,$p['cut']);$first=json_decode(file_get_contents($dir.'/first.json'),true);$ok($out['replayed']&&$out['id']===$first['id']&&$host->table('fr_calculations')->count()===$p['before']+1,'same key recovers one calculation after lock release');file_put_contents($dir.'/result.json',json_encode(['passed'=>true,'during_lock'=>json_decode(file_get_contents($dir.'/second.json'),true),'after_release'=>200,'replayed'=>true,'one_calculation'=>true,'regression_only'=>true],JSON_PRETTY_PRINT));}
else throw new LogicException('Unknown mode');echo json_encode(['passed'=>true,'phase'=>$mode,'proof'=>$dir.'/result.json']).PHP_EOL;