send_refillex.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. declare(strict_types=0);
  3. define('MOBILE_SERVER',true);
  4. define('USE_COROUTINE',true);
  5. define('SUPPORT_PTHREAD',false);
  6. define('APP_ID', 'sender');
  7. define('BASE_ROOT_PATH',str_replace('/server','',dirname(__FILE__)));
  8. define('BASE_PATH',BASE_ROOT_PATH . '/server');
  9. require_once(BASE_ROOT_PATH . '/global.php');
  10. require_once(BASE_ROOT_PATH . '/fooder.php');
  11. require_once(BASE_CORE_PATH . '/framework/function/http.php');
  12. require_once(BASE_HELPER_PATH . '/refill_proxy.php');
  13. Co::set(['hook_flags' => SWOOLE_HOOK_NATIVE_CURL|SWOOLE_HOOK_SLEEP]);
  14. class RefillSender
  15. {
  16. const co_orders = 100000;
  17. public function sendex()
  18. {
  19. $pThis = $this;
  20. $time = time() * 1000;
  21. for ($i = 0;$i < self::co_orders;)
  22. {
  23. $res = Swoole\Coroutine::stats();
  24. $num = $res['coroutine_num'];
  25. if($num > 1000) {
  26. sleep(1);
  27. continue;
  28. }
  29. $i++;
  30. Log::record(__FUNCTION__ . " index={$i}",Log::DEBUG);
  31. go(function () use ($pThis,$time,$i) {
  32. $pThis->push_order(1092, $time, $i);
  33. });
  34. }
  35. }
  36. private function push_order($mchid,$time,$index)
  37. {
  38. $pid = getmypid();
  39. $cid = Swoole\Coroutine::getCid();
  40. $order_sn = "{$pid}{$cid}{$time}" . sprintf("%'010d",$index);
  41. $notify_url = "https://test.xyzshops.cn/mobile/callback/bridge_test.php";
  42. $params = [ 'mchid' => $mchid,
  43. 'amount' => 30,
  44. 'order_sn' => $order_sn,
  45. 'cardno' => '13911129867',
  46. "act" => "refill",
  47. "op" => "add",
  48. 'notifyurl' => $notify_url
  49. ];
  50. $proxy = new refill_proxy("210fe406954220f56085997d6a4c5b80");
  51. $resp = $proxy->send("https://test.xyzshops.cn/mobile/index.php", $params);
  52. Log::record("resp={$resp}",Log::DEBUG);
  53. }
  54. }
  55. $process_count = 2;
  56. $workers = [];
  57. for ($i = 0; $i < $process_count;$i++)
  58. {
  59. $process = new Swoole\Process(function(Swoole\Process $worker)
  60. {
  61. Base::run_util();
  62. set_error_handler('handle_error');
  63. go(function () {
  64. $sender = new RefillSender();
  65. $sender->sendex();
  66. });
  67. }, false, false, true);
  68. $pid = $process->start();
  69. $workers[$pid] = $process;
  70. }
  71. Log::record("main process start wait sub process....",Log::DEBUG);
  72. while (true)
  73. {
  74. if($status = Swoole\Process::wait(true)) {
  75. Log::record("Sub process #{$status['pid']} quit, code={$status['code']}, signal={$status['signal']}",Log::DEBUG);
  76. }
  77. else
  78. {
  79. foreach ($workers as $pid => $worker)
  80. {
  81. Swoole\Process::kill($pid,SIGTERM);
  82. if($status = Swoole\Process::wait(true)) {
  83. Log::record("Graceful Recycled #{$status['pid']}, code={$status['code']}, signal={$status['signal']}",Log::DEBUG);
  84. }
  85. }
  86. break;
  87. }
  88. }
  89. Log::record("Quit all",Log::DEBUG);