sharing_mysql_worker.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. // No application bootstrap, .env, provider or pre-existing database is loaded by this worker.
  3. error_reporting(E_ALL & ~E_DEPRECATED);
  4. require __DIR__ . '/sharing_mysql_support.php';
  5. use Tests\Fixtures\SharingMysqlSupport;
  6. $params = json_decode($argv[1], true, 512, JSON_THROW_ON_ERROR);
  7. try {
  8. SharingMysqlSupport::guardedPdo($params['socket'], $params['database']);
  9. if ($params['framework'] === 'tp') {
  10. $tpRoot = $params['tp_root'];
  11. require $tpRoot . '/vendor/autoload.php';
  12. foreach (['ShareSupport', 'ShareService'] as $class) require $tpRoot . '/app/common/service/sharing/' . $class . '.php';
  13. $app = new think\App($tpRoot . '/');
  14. think\Container::setInstance($app);
  15. $db = new think\DbManager();
  16. $db->setConfig(['default' => 'isolated', 'connections' => ['isolated' => ['type' => 'mysql', 'socket' => $params['socket'],
  17. 'database' => $params['database'], 'username' => 'root', 'password' => '', 'prefix' => '', 'charset' => 'utf8mb4',
  18. 'fields_strict' => true, 'debug' => false]]]);
  19. $app->instance('think\DbManager', $db);
  20. $app->instance('log', new Psr\Log\NullLogger());
  21. $call = fn () => (new app\common\service\sharing\ShareService(1))->claim(['request_id' => $params['request_id']]);
  22. file_put_contents($params['ready'], 'ready');
  23. $result = !empty($params['hold']) ? think\facade\Db::transaction(function () use ($params, $call) {
  24. think\facade\Db::table('bot_share_accounts')->where('user_id', 1)->lock(true)->find();
  25. SharingMysqlSupport::hold($params);
  26. return $call();
  27. }) : $call();
  28. } else {
  29. $loader = require $params['laravel_autoload'];
  30. $loader->setPsr4('App\\', dirname(__DIR__, 2) . '/app');
  31. SharingMysqlSupport::laravel($params['socket'], $params['database']);
  32. if (!empty($params['hold'])) {
  33. $held = false;
  34. Illuminate\Support\Facades\DB::listen(function ($event) use ($params, &$held) {
  35. $sync = $params['action'] === 'sync';
  36. $targetQuery = $sync
  37. ? str_starts_with(strtolower($event->sql), 'update `bot_share_bet_facts`')
  38. : (str_contains($event->sql, 'share_accounts') && str_contains(strtolower($event->sql), 'for update'));
  39. if (!$held && $targetQuery) {
  40. $held = true;
  41. if ($sync) {
  42. $partial = Illuminate\Support\Facades\DB::table('share_bet_facts')->where('source', 'lhc_order_item')->sum('effective_amount');
  43. file_put_contents($params['partial'], bcadd((string)$partial, '0', 10));
  44. }
  45. SharingMysqlSupport::hold($params);
  46. }
  47. });
  48. }
  49. file_put_contents($params['ready'], 'ready');
  50. $result = match ($params['action']) {
  51. 'audit' => (new App\Services\Sharing\ShareClaimAuditService())->audit((int)$params['claim_id'], 'approve', 8, 'mysql concurrency fixture'),
  52. 'sync' => (new App\Services\Sharing\BetFactSynchronizer())->sync('2026-10-24', '2026-10-24', true),
  53. default => (new App\Services\Sharing\ShareSettlementService())->settle('2026-10-24'),
  54. };
  55. }
  56. echo json_encode(['ok' => true, 'result' => $result], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
  57. } catch (Throwable $e) {
  58. echo json_encode(['ok' => false, 'exception' => get_class($e), 'message' => $e->getMessage()], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
  59. exit(1);
  60. }