SwooleTask.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Services\Api\FinanceService;
  4. use App\Services\Api\GoodsCategoryService;
  5. use App\Services\Api\GoodsService;
  6. use App\Services\Api\OrderService;
  7. use App\Services\RedisService;
  8. use App\Services\WalletService;
  9. use Illuminate\Console\Command;
  10. use Illuminate\Support\Facades\DB;
  11. class SwooleTask extends Command
  12. {
  13. protected $serv;
  14. protected $host = '127.0.0.1';
  15. protected $port = 6622;
  16. // 进程名称
  17. protected $taskName = 'swooleTask';
  18. // PID路径
  19. protected $pidPath = '/storage/swoole.pid';
  20. // task
  21. protected $onlyReloadTaskWorker = false;
  22. // 设置运行时参数
  23. protected $options = [
  24. 'worker_num' => 8, //worker进程数,一般设置为CPU数的1-4倍
  25. 'daemonize' => true, //启用守护进程
  26. 'log_file' => '/storage/logs/swoole-task.log', //指定swoole错误日志文件
  27. 'log_level' => 0, //日志级别 范围是0-5,0-DEBUG,1-TRACE,2-INFO,3-NOTICE,4-WARNING,5-ERROR
  28. 'dispatch_mode' => 1, //数据包分发策略,1-轮询模式
  29. 'task_worker_num' => 6, //task进程的数量
  30. 'task_ipc_mode' => 3, //使用消息队列通信,并设置为争抢模式
  31. ];
  32. /**
  33. * The name and signature of the console command.
  34. *
  35. * @var string
  36. */
  37. protected $signature = 'swoole:task {op}';
  38. /**
  39. * The console command description.
  40. *
  41. * @var string
  42. */
  43. protected $description = 'Swoole task server description';
  44. /**
  45. * Create a new command instance.
  46. *
  47. * @return void
  48. */
  49. public function __construct()
  50. {
  51. parent::__construct();
  52. }
  53. /**
  54. * 入口
  55. * Execute the console command.
  56. *
  57. * @return mixed
  58. */
  59. public function handle()
  60. {
  61. ini_set("default_socket_timeout", -1);
  62. // 项目根目录
  63. defined('ROOT_PATH') or define('ROOT_PATH', base_path());
  64. // 文件上传目录
  65. defined('ATTACHMENT_PATH') or define('ATTACHMENT_PATH', base_path('public/uploads'));
  66. // 图片上传目录
  67. defined('IMG_PATH') or define('IMG_PATH', base_path('public/uploads/images'));
  68. // 临时存放目录
  69. defined('UPLOAD_TEMP_PATH') or define('UPLOAD_TEMP_PATH', ATTACHMENT_PATH . "/temp");
  70. // 定义普通图片域名
  71. defined('IMG_URL') or define('IMG_URL', env('IMG_URL'));
  72. // 数据表前缀
  73. defined('DB_PREFIX') or define('DB_PREFIX', DB::connection()->getTablePrefix());
  74. $this->options['log_file'] = base_path() . $this->options['log_file'];
  75. $this->pidPath = base_path() . $this->pidPath;
  76. $op = $this->argument('op');
  77. switch ($op) {
  78. case 'status': // 状态
  79. $res = $this->status();
  80. echo $res ? $res : 0;
  81. break;
  82. case 'start': // 运行
  83. return $this->start();
  84. break;
  85. case 'reload': // 平滑重启
  86. return $this->reload();
  87. break;
  88. case 'stop': // 停止运行
  89. return $this->stop();
  90. break;
  91. default:
  92. exit("{$op} command does not exist");
  93. break;
  94. }
  95. }
  96. /**
  97. * 启动
  98. */
  99. public function start()
  100. {
  101. date_default_timezone_set('PRC');
  102. // 构建Server对象,监听对应地址
  103. $this->serv = new \Swoole\Server($this->host, $this->port);
  104. $this->serv->set($this->options);
  105. // 注册事件
  106. $this->serv->on('start', [$this, 'onStart']);
  107. $this->serv->on('receive', [$this, 'onReceive']);
  108. $this->serv->on('task', [$this, 'onTask']);
  109. $this->serv->on('finish', [$this, 'onFinish']);
  110. // Run worker
  111. echo "swoole start...\n";
  112. $this->serv->start();
  113. }
  114. // 安全重启
  115. public function reload()
  116. {
  117. $pids = file_exists($this->pidPath) ? file_get_contents($this->pidPath) : '';
  118. $pids = $pids ? explode("\n", $pids) : [];
  119. $masterPid = isset($pids[0]) ? $pids[0] : '';
  120. $managePid = isset($pids[1]) ? $pids[1] : '';
  121. if (empty($masterPid)) {
  122. return false;
  123. }
  124. if (!$this->status($masterPid)) {
  125. return false;
  126. }
  127. \Swoole\Process::kill($managePid, SIGUSR1);
  128. echo "swoole reload...\n";
  129. }
  130. /**
  131. * 停止
  132. * @param bool $smooth
  133. * @return bool
  134. */
  135. public function stop($smooth = false)
  136. {
  137. $pids = file_exists($this->pidPath) ? file_get_contents($this->pidPath) : '';
  138. $pids = $pids ? explode("\n", $pids) : [];
  139. $masterPid = isset($pids[0]) ? $pids[0] : '';
  140. $managePid = isset($pids[1]) ? $pids[1] : '';
  141. if (empty($masterPid)) {
  142. return false;
  143. }
  144. if (!$this->status($masterPid)) {
  145. return false;
  146. }
  147. // if ($smooth) {
  148. // \Swoole\Process::kill($masterPid, SIGTERM);
  149. // try {
  150. // while (true) {
  151. // \Swoole\Process::kill($masterPid, 0);
  152. // }
  153. // } catch (\Exception $exception) {
  154. //
  155. // }
  156. // } else {
  157. // \Swoole\Process::kill($masterPid, SIGKILL);
  158. // }
  159. //
  160. // if($managePid){
  161. // \Swoole\Process::kill($managePid, SIGKILL);
  162. // }
  163. // 直接杀
  164. $stoSh = base_path().'/crontab/swooleTaskStop.sh';
  165. echo $stoSh;
  166. if(file_exists($stoSh) && function_exists('exec')){
  167. exec("{$stoSh}");
  168. }
  169. @unlink($this->pidPath);
  170. echo "swoole stop...\n";
  171. }
  172. /**
  173. * 状态
  174. * @return mixed
  175. */
  176. public function status($masterPid = 0)
  177. {
  178. $res = false;
  179. if (empty($masterPid) && file_exists($this->pidPath)) {
  180. $pids = file_get_contents($this->pidPath);
  181. $pids = $pids ? explode("\n", $pids) : [];
  182. $masterPid = isset($pids[0]) ? $pids[0] : '';
  183. }
  184. if ($masterPid) {
  185. $res = \Swoole\Process::kill($masterPid, 0);
  186. }
  187. return $res;
  188. }
  189. public function onStart($serv)
  190. {
  191. if (!is_dir(dirname($this->pidPath))) {
  192. @mkdir(dirname($this->pidPath), true, 755);
  193. }
  194. //记录进程id,脚本实现自动重启
  195. $pid = "{$serv->master_pid}\n{$serv->manager_pid}";
  196. file_put_contents($this->pidPath, $pid);
  197. // 定时任务
  198. $time = 0;
  199. $date = date('Y-m-d H:i:s');
  200. if(file_exists($this->options['log_file'])){
  201. $time = 0;
  202. file_put_contents($this->options['log_file'],"Task {$date}:清空日志\n");
  203. }
  204. // TODO 更新USDT价格
  205. \swoole_timer_tick(30000, function ($timer) use ($serv, &$time) { // 启用定时器,每120秒执行一次
  206. $date = date('Y-m-d H:i:s');
  207. if($time>3600 && file_exists($this->options['log_file'])){
  208. $time = 0;
  209. file_put_contents($this->options['log_file'],"Task {$date}:清空日志\n");
  210. }
  211. $time++;
  212. if(!RedisService::get('caches:task:lock:usdt_loaded')){
  213. $taskData = [
  214. 'taskName' => 'UpdateUsdtPrice',
  215. 'name' => "更新USDT价格",
  216. 'date' => date('Y-m-d'),
  217. ];
  218. $res = $serv->task($taskData);
  219. RedisService::set('caches:task:lock:usdt_loaded', true, rand(3,5));
  220. echo "[Task UpdateUsdtPrice {$date}] 更新USDT价格:{$res}\n";
  221. }else{
  222. echo "[Task UpdateUsdtPrice {$date}] 间隔时间调用\n";
  223. }
  224. });
  225. // TODO 更新商品数据
  226. \swoole_timer_tick(120000, function ($timer) use ($serv, &$time) { // 启用定时器,每120秒执行一次
  227. $date = date('Y-m-d H:i:s');
  228. if($time>3600 && file_exists($this->options['log_file'])){
  229. $time = 0;
  230. file_put_contents($this->options['log_file'],"Task {$date}:清空日志\n");
  231. }
  232. $time++;
  233. if(!RedisService::get('caches:task:lock:goods_loaded')){
  234. $taskData = [
  235. 'taskName' => 'UpdateGoods',
  236. 'name' => "更新商品数据",
  237. 'date' => date('Y-m-d'),
  238. ];
  239. $res = $serv->task($taskData);
  240. RedisService::set('caches:task:lock:goods_loaded', true, rand(3,5));
  241. echo "[Task UpdateGoods {$date}] 更新商品数据:{$res}\n";
  242. }else{
  243. echo "[Task UpdateGoods {$date}] 间隔时间调用\n";
  244. }
  245. });
  246. // TODO 更新商品分类
  247. \swoole_timer_tick(120000, function ($timer) use ($serv, &$time) { // 启用定时器,每120秒执行一次
  248. $date = date('Y-m-d H:i:s');
  249. if($time>3600 && file_exists($this->options['log_file'])){
  250. $time = 0;
  251. file_put_contents($this->options['log_file'],"Task {$date}:清空日志\n");
  252. }
  253. $time++;
  254. if(!RedisService::get('caches:task:lock:goods_category_loaded')){
  255. $taskData = [
  256. 'taskName' => 'UpdateGoodsCategory',
  257. 'name' => "更新商品分类数据",
  258. 'date' => date('Y-m-d'),
  259. ];
  260. $res = $serv->task($taskData);
  261. RedisService::set('caches:task:lock:goods_category_loaded', true, rand(3,5));
  262. echo "[Task UpdateGoodsCategory {$date}] 更新商品分类数据:{$res}\n";
  263. }else{
  264. echo "[Task UpdateGoodsCategory {$date}] 间隔时间调用\n";
  265. }
  266. });
  267. // TODO 更新商品分类
  268. \swoole_timer_tick(120000, function ($timer) use ($serv, &$time) { // 启用定时器,每120秒执行一次
  269. $date = date('Y-m-d H:i:s');
  270. if($time>3600 && file_exists($this->options['log_file'])){
  271. $time = 0;
  272. file_put_contents($this->options['log_file'],"Task {$date}:清空日志\n");
  273. }
  274. $time++;
  275. $cateIds = GoodsCategoryService::make()->getCateIds();
  276. if($cateIds){
  277. foreach($cateIds as $item){
  278. $pid = isset($item['cate_id'])? $item['cate_id'] : 0;
  279. if($pid && !RedisService::get("caches:task:lock:goods_category_sub_loaded_{$pid}")){
  280. $taskData = [
  281. 'taskName' => 'UpdateGoodsCategorySub',
  282. 'name' => "更新商品分类【{$pid}】的子分类数据",
  283. 'pid'=> $pid,
  284. 'date' => date('Y-m-d'),
  285. ];
  286. $res = $serv->task($taskData);
  287. RedisService::set("caches:task:lock:goods_category_sub_loaded_{$pid}", true, rand(3,5));
  288. echo "[Task UpdateGoodsCategorySub {$date}] 更新商品分类【{$pid}】的子分类数据:{$res}\n";
  289. }else{
  290. echo "[Task UpdateGoodsCategorySub {$date}] 间隔时间调用\n";
  291. }
  292. }
  293. }else{
  294. echo "[Task UpdateGoodsCategorySub {$date}] 没有父级数据\n";
  295. }
  296. });
  297. // TODO 更新商品SKU数据
  298. \swoole_timer_tick(120000, function ($timer) use ($serv, &$time) { // 启用定时器,每120秒执行一次
  299. $date = date('Y-m-d H:i:s');
  300. if($time>3600 && file_exists($this->options['log_file'])){
  301. $time = 0;
  302. file_put_contents($this->options['log_file'],"Task {$date}:清空日志\n");
  303. }
  304. $time++;
  305. if(!RedisService::get('caches:task:lock:goods_sku_loaded')){
  306. $taskData = [
  307. 'taskName' => 'UpdateGoodsSku',
  308. 'name' => "更新商品SKU数据",
  309. 'date' => date('Y-m-d'),
  310. ];
  311. $res = $serv->task($taskData);
  312. RedisService::set('caches:task:lock:goods_sku_loaded', true, rand(3,5));
  313. echo "[Task UpdateGoodsSku {$date}] 更新商品SKU数据:{$res}\n";
  314. }else{
  315. echo "[Task UpdateGoodsSku {$date}] 间隔时间调用\n";
  316. }
  317. });
  318. // TODO 发放积分
  319. \swoole_timer_tick(180000, function ($timer) use ($serv, &$time) { // 启用定时器,每180秒执行一次
  320. $date = date('Y-m-d H:i:s');
  321. if($time>3600 && file_exists($this->options['log_file'])){
  322. $time = 0;
  323. file_put_contents($this->options['log_file'],"Task {$date}:清空日志\n");
  324. }
  325. $time++;
  326. if(!RedisService::get('caches:task:lock:grant_score_loaded')){
  327. $taskData = [
  328. 'taskName' => 'GrantScore',
  329. 'name' => "每日发放积分",
  330. 'date' => date('Y-m-d'),
  331. ];
  332. $res = $serv->task($taskData);
  333. RedisService::set('caches:task:lock:grant_score_loaded', true, rand(3,5));
  334. echo "[Task GrantScore {$date}] 每日发放积分:{$res}\n";
  335. }else{
  336. echo "[Task GrantScore {$date}] 间隔时间调用\n";
  337. }
  338. });
  339. // TODO 待返积分返还
  340. \swoole_timer_tick(120000, function ($timer) use ($serv, &$time) { // 启用定时器,每120秒执行一次
  341. $date = date('Y-m-d H:i:s');
  342. if($time>3600 && file_exists($this->options['log_file'])){
  343. $time = 0;
  344. file_put_contents($this->options['log_file'],"Task {$date}:清空日志\n");
  345. }
  346. $time++;
  347. if(!RedisService::get('caches:task:lock:wait_score_loaded')){
  348. $taskData = [
  349. 'taskName' => 'ReturnWaitScore',
  350. 'name' => "待返积分每日返还",
  351. 'date' => date('Y-m-d'),
  352. ];
  353. $res = $serv->task($taskData);
  354. RedisService::set('caches:task:lock:wait_score_loaded', true, rand(3,5));
  355. echo "[Task ReturnWaitScore {$date}] 待返积分每日返还:{$res}\n";
  356. }else{
  357. echo "[Task ReturnWaitScore {$date}] 间隔时间调用\n";
  358. }
  359. });
  360. // TODO 全球分红结算
  361. \swoole_timer_tick(120000, function ($timer) use ($serv, &$time) { // 启用定时器,每120秒执行一次
  362. $date = date('Y-m-d H:i:s');
  363. if($time>3600 && file_exists($this->options['log_file'])){
  364. $time = 0;
  365. file_put_contents($this->options['log_file'],"Task {$date}:清空日志\n");
  366. }
  367. $time++;
  368. if(!RedisService::get('caches:task:lock:global_loaded')){
  369. $taskData = [
  370. 'taskName' => 'GlobalBonus',
  371. 'name' => "待返积分每日返还",
  372. 'date' => date('Y-m-d'),
  373. ];
  374. $res = $serv->task($taskData);
  375. RedisService::set('caches:task:lock:global_loaded', true, rand(3,5));
  376. echo "[Task GlobalBonus {$date}] 全球分红结算:{$res}\n";
  377. }else{
  378. echo "[Task GlobalBonus {$date}] 间隔时间调用\n";
  379. }
  380. });
  381. // TODO 更新订单状态
  382. \swoole_timer_tick(300000, function ($timer) use ($serv, &$time) { // 启用定时器,每300秒执行一次
  383. $date = date('Y-m-d H:i:s');
  384. if($time>3600 && file_exists($this->options['log_file'])){
  385. $time = 0;
  386. file_put_contents($this->options['log_file'],"Task {$date}:清空日志\n");
  387. }
  388. $time++;
  389. if(!RedisService::get('caches:task:lock:order_loaded')){
  390. $taskData = [
  391. 'taskName' => 'UpdateOrderStatus',
  392. 'name' => "更新订单状态",
  393. 'date' => date('Y-m-d'),
  394. ];
  395. $res = $serv->task($taskData);
  396. RedisService::set('caches:task:lock:order_loaded', true, rand(3,5));
  397. echo "[Task UpdateOrderStatus {$date}] 更新订单状态:{$res}\n";
  398. }else{
  399. echo "[Task UpdateOrderStatus {$date}] 间隔时间调用\n";
  400. }
  401. });
  402. }
  403. //监听连接进入事件
  404. public function onConnect($serv, $fd, $from_id)
  405. {
  406. $serv->send($fd, "Success {$fd}!");
  407. }
  408. // 监听数据接收事件
  409. public function onReceive(\Swoole\Server $serv, $fd, $from_id, $data)
  410. {
  411. echo "Get Message From Client {$fd}:{$data}\n";
  412. $res['result'] = 'success';
  413. $serv->send($fd, json_encode($res)); // 同步返回消息给客户端
  414. $serv->task($data); // 执行异步任务
  415. }
  416. /**
  417. * @param \Swoole\Server $serv
  418. * @param $task_id
  419. * @param $from_id
  420. * @param $data
  421. * @return false|string
  422. */
  423. public function onTask(\Swoole\Server $serv, $task_id, $from_id, $data)
  424. {
  425. $date = date('Y-m-d H:i:s');
  426. $taskName = isset($data['taskName']) ? $data['taskName'] : '';
  427. switch ($taskName) {
  428. case 'UpdateOrderStatus': // 更新订单状态
  429. // 时间限制
  430. if(date('H:i') >= '00:00' && date('H:i') <= '05:00'){
  431. echo "[Task {$taskName} {$date}] 不在运行时间段内\n";
  432. return false;
  433. }
  434. // 调用处理
  435. if($res = OrderService::make()->updateOrderStatus()){
  436. $res = is_array($res) && $res? json_encode($res, 256) : 'success';
  437. echo "[Task {$taskName} {$date}] 更新订单状态结果:{$res}\n";
  438. }else{
  439. $error = OrderService::make()->getError();
  440. $error = $error? lang($error) : 'failed';
  441. echo "[Task {$taskName} {$date}] 更新订单状态结果:{$error}\n";
  442. }
  443. break;
  444. case 'UpdateOrderRefundStatus': // 更新售后订单状态
  445. // 时间限制
  446. if(date('H:i') >= '00:00' && date('H:i') <= '05:00'){
  447. echo "[Task {$taskName} {$date}] 不在运行时间段内\n";
  448. return false;
  449. }
  450. // 调用处理
  451. if($res = OrderService::make()->updateOrderRefundStatus()){
  452. $res = is_array($res) && $res? json_encode($res, 256) : 'success';
  453. echo "[Task {$taskName} {$date}] 更新售后订单状态结果:{$res}\n";
  454. }else{
  455. $error = OrderService::make()->getError();
  456. $error = $error? lang($error) : 'failed';
  457. echo "[Task {$taskName} {$date}] 更新售后订单状态结果:{$error}\n";
  458. }
  459. break;
  460. case 'UpdateGoods': // 更新商品
  461. // 时间限制
  462. if(date('H:i') >= '00:00' && date('H:i') <= '03:00'){
  463. echo "[Task {$taskName} {$date}] 不在运行时间段内\n";
  464. return false;
  465. }
  466. // 调用处理
  467. if($res = GoodsService::make()->updateGoods()){
  468. $res = is_array($res) && $res? json_encode($res, 256) : 'success';
  469. echo "[Task {$taskName} {$date}] 商品数据获取更新结果:{$res}\n";
  470. }else{
  471. $error = GoodsService::make()->getError();
  472. $error = $error? lang($error) : 'failed';
  473. echo "[Task {$taskName} {$date}] 商品数据获取更新结果:{$error}\n";
  474. }
  475. break;
  476. case 'UpdateGoodsSku': // 更新商品SKu数据
  477. // 时间限制
  478. if(date('H:i') >= '00:00' && date('H:i') <= '03:00'){
  479. echo "[Task {$taskName} {$date}] 不在运行时间段内\n";
  480. return false;
  481. }
  482. // 调用处理
  483. if($res = GoodsService::make()->updateGoodsSku()){
  484. $res = is_array($res) && $res? json_encode($res, 256) : 'success';
  485. echo "[Task {$taskName} {$date}] 更新商品SKu数据结果:{$res}\n";
  486. }else{
  487. $error = GoodsService::make()->getError();
  488. $error = $error? lang($error) : 'failed';
  489. echo "[Task {$taskName} {$date}] 更新商品SKu数据结果:{$error}\n";
  490. }
  491. break;
  492. case 'UpdateGoodsCategory': // 更新商品分类数据
  493. // 时间限制
  494. if(date('H:i') >= '00:00' && date('H:i') <= '04:00'){
  495. echo "[Task {$taskName} {$date}] 不在运行时间段内\n";
  496. return false;
  497. }
  498. // 调用处理
  499. if($res = GoodsService::make()->updateGoodsCategory()){
  500. $res = is_array($res) && $res? json_encode($res, 256) : 'success';
  501. echo "[Task {$taskName} {$date}] 更新商品分类数据结果:{$res}\n";
  502. }else{
  503. $error = GoodsService::make()->getError();
  504. $error = $error? lang($error) : 'failed';
  505. echo "[Task {$taskName} {$date}] 更新商品分类数据结果:{$error}\n";
  506. }
  507. break;
  508. case 'UpdateGoodsCategorySub': // 更新商品分类数据
  509. // 时间限制
  510. if(date('H:i') >= '00:00' && date('H:i') <= '04:00'){
  511. echo "[Task {$taskName} {$date}] 不在运行时间段内\n";
  512. return false;
  513. }
  514. // 调用处理
  515. $pid = isset($data['pid'])? $data['pid'] : 0;
  516. if($res = GoodsService::make()->updateGoodsCategory($pid)){
  517. $res = is_array($res) && $res? json_encode($res, 256) : 'success';
  518. echo "[Task {$taskName} {$date}] 更新商品分类数据结果:{$res}\n";
  519. }else{
  520. $error = GoodsService::make()->getError();
  521. $error = $error? lang($error) : 'failed';
  522. echo "[Task {$taskName} {$date}] 更新商品分类数据结果:{$error}\n";
  523. }
  524. break;
  525. case 'UpdateUsdtPrice': // 更新USDT价格
  526. // 时间限制
  527. if(date('H:i') >= '01:00' && date('H:i') <= '04:00'){
  528. echo "[Task {$taskName} {$date}] 不在运行时间段内\n";
  529. return false;
  530. }
  531. // 调用处理
  532. if($res = WalletService::make()->getBianRatePrice(true)){
  533. $res = is_array($res) && $res? json_encode($res, 256) : 'success';
  534. echo "[Task {$taskName} {$date}] 更新USDT价格结果:{$res}\n";
  535. }else{
  536. $error = WalletService::make()->getError();
  537. $error = $error? lang($error) : 'failed';
  538. echo "[Task {$taskName} {$date}] 更新USDT价格结果:{$error}\n";
  539. }
  540. break;
  541. case 'GrantScore':
  542. // 时间限制
  543. if(date('H:i') >= '09:00'){
  544. echo "[Task {$taskName} {$date}] 不在运行时间段内\n";
  545. return false;
  546. }
  547. // 调用处理
  548. if($res = FinanceService::make()->grantScore()){
  549. $res = is_array($res) && $res? json_encode($res, 256) : 'success';
  550. echo "[Task {$taskName} {$date}] 每日发放积分结果:{$res}\n";
  551. }else{
  552. $error = FinanceService::make()->getError();
  553. $error = $error? lang($error) : 'failed';
  554. echo "[Task {$taskName} {$date}] 每日发放积分结果:{$error}\n";
  555. }
  556. break;
  557. case 'ReturnWaitScore':
  558. // 时间限制
  559. if(date('H:i') >= '06:00'){
  560. echo "[Task {$taskName} {$date}] 不在运行时间段内\n";
  561. return false;
  562. }
  563. // 调用处理
  564. if($res = FinanceService::make()->returnWaitScore()){
  565. $res = is_array($res) && $res? json_encode($res, 256) : 'success';
  566. echo "[Task {$taskName} {$date}] 待返积分每日返还结果:{$res}\n";
  567. }else{
  568. $error = FinanceService::make()->getError();
  569. $error = $error? lang($error) : 'failed';
  570. echo "[Task {$taskName} {$date}] 待返积分每日返还结果:{$error}\n";
  571. }
  572. break;
  573. case 'GlobalBonus': // 全球分红
  574. // 时间限制
  575. if(date('H:i') >= '05:00'){
  576. echo "[Task {$taskName} {$date}] 不在运行时间段内\n";
  577. return false;
  578. }
  579. // 调用处理
  580. if($res = FinanceService::make()->globalBonus()){
  581. $res = is_array($res) && $res? json_encode($res, 256) : 'success';
  582. echo "[Task {$taskName} {$date}] 全球分红结算结果:{$res}\n";
  583. }else{
  584. $error = FinanceService::make()->getError();
  585. $error = $error? lang($error) : 'failed';
  586. echo "[Task {$taskName} {$date}] 全球分红结算结果:{$error}\n";
  587. }
  588. break;
  589. }
  590. return '暂无任务处理';
  591. }
  592. /**
  593. * @param $serv swoole_server swoole_server对象
  594. * @param $task_id int 任务id
  595. * @param $data string 任务返回的数据
  596. */
  597. public function onFinish(\Swoole\Server $serv, $task_id, $data)
  598. {
  599. //
  600. echo "任务处理完成...\n";
  601. }
  602. // 监听连接关闭事件
  603. public function onClose($serv, $fd, $from_id)
  604. {
  605. echo "Client {$fd} close connection\n";
  606. $serv->close();
  607. }
  608. }