SwooleTask.php 27 KB

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