make(\Illuminate\Contracts\Console\Kernel::class)->bootstrap(); \App\Modules\Services\ServiceReceivables::guard(); $in=json_decode(stream_get_contents(STDIN),true,32,JSON_THROW_ON_ERROR); app(\App\Shared\Context\CompanyContext::class)->selectFor($in['actor'],$in['tenant'],$in['company'],$in['unit']); if($in['pause']??false) { $paused=false; \Illuminate\Support\Facades\DB::listen(function($q)use(&$paused){ if(!$paused && str_contains($q->sql,'"companies"') && str_contains($q->sql,'for update')) { $paused=true; echo "LOCK_HELD\n"; flush(); usleep(1200000); } }); } try { $s=app(\App\Modules\Services\ServiceReceivables::class); if($in['method']==='sql_allocate') { $result=\Illuminate\Support\Facades\DB::transaction(function()use($in) { if($in['pause']??false) { \Illuminate\Support\Facades\DB::table('companies')->where('id',$in['company'])->lockForUpdate()->firstOrFail(); } $id=\Illuminate\Support\Facades\DB::table('sr_effects')->insertGetId($in['sql']); return ['id'=>$id]; }); } else $result=$s->{$in['method']}($in['id'],$in['actor'],$in['input'],$in['key']); echo json_encode(['status'=>200,'result'=>$result],JSON_THROW_ON_ERROR).PHP_EOL; } catch(\Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $e) { echo json_encode(['status'=>$e->getStatusCode()],JSON_THROW_ON_ERROR).PHP_EOL; } catch(\Illuminate\Database\QueryException $e) { if(!str_contains($e->getMessage(),'SGF_SR_')) throw $e; echo json_encode(['status'=>409,'database_guard'=>true],JSON_THROW_ON_ERROR).PHP_EOL; }