GoodsTask.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Services\Api\GoodsCategoryService;
  4. use App\Services\Api\GoodsService;
  5. use App\Services\ConfigService;
  6. use App\Services\RedisService;
  7. use Illuminate\Console\Command;
  8. use Illuminate\Support\Facades\DB;
  9. class GoodsTask extends Command
  10. {
  11. protected $serv;
  12. protected $host = '127.0.0.1';
  13. protected $port = 6628;
  14. // 进程名称
  15. protected $taskName = 'goodsTask';
  16. // PID路径
  17. protected $pidPath = '/storage/swoole.pid';
  18. // task
  19. protected $onlyReloadTaskWorker = false;
  20. // 设置运行时参数
  21. protected $options = [
  22. 'worker_num' => 8, //worker进程数,一般设置为CPU数的1-4倍
  23. 'daemonize' => true, //启用守护进程
  24. 'log_file' => '/storage/logs/goods-task.log', //指定swoole错误日志文件
  25. 'log_level' => 0, //日志级别 范围是0-5,0-DEBUG,1-TRACE,2-INFO,3-NOTICE,4-WARNING,5-ERROR
  26. 'dispatch_mode' => 1, //数据包分发策略,1-轮询模式
  27. 'task_worker_num' => 6, //task进程的数量
  28. 'task_ipc_mode' => 3, //使用消息队列通信,并设置为争抢模式
  29. ];
  30. /**
  31. * The name and signature of the console command.
  32. *
  33. * @var string
  34. */
  35. protected $signature = 'goods:task {op}';
  36. /**
  37. * The console command description.
  38. *
  39. * @var string
  40. */
  41. protected $description = 'Goods task server description';
  42. /**
  43. * Create a new command instance.
  44. *
  45. * @return void
  46. */
  47. public function __construct()
  48. {
  49. parent::__construct();
  50. }
  51. /**
  52. * 入口
  53. * Execute the console command.
  54. *
  55. * @return mixed
  56. */
  57. public function handle()
  58. {
  59. ini_set("default_socket_timeout", -1);
  60. // 项目根目录
  61. defined('ROOT_PATH') or define('ROOT_PATH', base_path());
  62. // 文件上传目录
  63. defined('ATTACHMENT_PATH') or define('ATTACHMENT_PATH', base_path('public/uploads'));
  64. // 图片上传目录
  65. defined('IMG_PATH') or define('IMG_PATH', base_path('public/uploads/images'));
  66. // 临时存放目录
  67. defined('UPLOAD_TEMP_PATH') or define('UPLOAD_TEMP_PATH', ATTACHMENT_PATH . "/temp");
  68. // 定义普通图片域名
  69. defined('IMG_URL') or define('IMG_URL', env('IMG_URL'));
  70. // 数据表前缀
  71. defined('DB_PREFIX') or define('DB_PREFIX', DB::connection()->getTablePrefix());
  72. $this->options['log_file'] = base_path() . $this->options['log_file'];
  73. $this->pidPath = base_path() . $this->pidPath;
  74. $op = $this->argument('op');
  75. switch ($op) {
  76. case 'status': // 状态
  77. $res = $this->status();
  78. echo $res ? $res : 0;
  79. break;
  80. case 'start': // 运行
  81. return $this->start();
  82. break;
  83. case 'reload': // 平滑重启
  84. return $this->reload();
  85. break;
  86. case 'stop': // 停止运行
  87. return $this->stop();
  88. break;
  89. default:
  90. exit("{$op} command does not exist");
  91. break;
  92. }
  93. }
  94. /**
  95. * 启动
  96. */
  97. public function start()
  98. {
  99. date_default_timezone_set('PRC');
  100. // 构建Server对象,监听对应地址
  101. $this->serv = new \Swoole\Server($this->host, $this->port);
  102. $this->serv->set($this->options);
  103. // 注册事件
  104. $this->serv->on('start', [$this, 'onStart']);
  105. $this->serv->on('receive', [$this, 'onReceive']);
  106. $this->serv->on('task', [$this, 'onTask']);
  107. $this->serv->on('finish', [$this, 'onFinish']);
  108. // Run worker
  109. echo "swoole start...\n";
  110. $this->serv->start();
  111. }
  112. // 安全重启
  113. public function reload()
  114. {
  115. $pids = file_exists($this->pidPath) ? file_get_contents($this->pidPath) : '';
  116. $pids = $pids ? explode("\n", $pids) : [];
  117. $masterPid = isset($pids[0]) ? $pids[0] : '';
  118. $managePid = isset($pids[1]) ? $pids[1] : '';
  119. if (empty($masterPid)) {
  120. return false;
  121. }
  122. if (!$this->status($masterPid)) {
  123. return false;
  124. }
  125. \Swoole\Process::kill($managePid, SIGUSR1);
  126. echo "swoole reload...\n";
  127. }
  128. /**
  129. * 停止
  130. * @param bool $smooth
  131. * @return bool
  132. */
  133. public function stop($smooth = false)
  134. {
  135. $pids = file_exists($this->pidPath) ? file_get_contents($this->pidPath) : '';
  136. $pids = $pids ? explode("\n", $pids) : [];
  137. $masterPid = isset($pids[0]) ? $pids[0] : '';
  138. $managePid = isset($pids[1]) ? $pids[1] : '';
  139. if (empty($masterPid)) {
  140. return false;
  141. }
  142. if (!$this->status($masterPid)) {
  143. return false;
  144. }
  145. // 直接杀
  146. $stoSh = base_path().'/crontab/goodsTaskStop.sh';
  147. echo $stoSh;
  148. if(file_exists($stoSh) && function_exists('exec')){
  149. exec("{$stoSh}");
  150. }
  151. @unlink($this->pidPath);
  152. echo "swoole stop...\n";
  153. }
  154. /**
  155. * 状态
  156. * @return mixed
  157. */
  158. public function status($masterPid = 0)
  159. {
  160. $res = false;
  161. if (empty($masterPid) && file_exists($this->pidPath)) {
  162. $pids = file_get_contents($this->pidPath);
  163. $pids = $pids ? explode("\n", $pids) : [];
  164. $masterPid = isset($pids[0]) ? $pids[0] : '';
  165. }
  166. if ($masterPid) {
  167. $res = \Swoole\Process::kill($masterPid, 0);
  168. }
  169. return $res;
  170. }
  171. public function onStart($serv)
  172. {
  173. if (!is_dir(dirname($this->pidPath))) {
  174. @mkdir(dirname($this->pidPath), true, 755);
  175. }
  176. //记录进程id,脚本实现自动重启
  177. $pid = "{$serv->master_pid}\n{$serv->manager_pid}";
  178. file_put_contents($this->pidPath, $pid);
  179. // 定时任务
  180. $time = 0;
  181. $date = date('Y-m-d H:i:s');
  182. if(file_exists($this->options['log_file'])){
  183. $time = 0;
  184. file_put_contents($this->options['log_file'],"Task {$date}:清空日志\n");
  185. }
  186. // TODO 更新商品数据
  187. \swoole_timer_tick(300000, function ($timer) use ($serv, &$time) { // 启用定时器,每120秒执行一次
  188. $date = date('Y-m-d H:i:s');
  189. if($time>3600 && file_exists($this->options['log_file'])){
  190. $time = 0;
  191. file_put_contents($this->options['log_file'],"Task {$date}:清空日志\n");
  192. }
  193. $time++;
  194. if(!RedisService::get('caches:task:lock:goods_loaded')){
  195. $taskData = [
  196. 'taskName' => 'UpdateGoods',
  197. 'name' => "更新商品数据",
  198. 'date' => date('Y-m-d'),
  199. ];
  200. $res = $serv->task($taskData);
  201. RedisService::set('caches:task:lock:goods_loaded', true, rand(3,5));
  202. echo "[Task UpdateGoods {$date}] 更新商品数据:{$res}\n";
  203. }else{
  204. echo "[Task UpdateGoods {$date}] 间隔时间调用\n";
  205. }
  206. });
  207. // TODO 按分类更新同步商品数据
  208. \swoole_timer_tick(3600000, function ($timer) use ($serv, &$time) { // 启用定时器,每120秒执行一次
  209. $date = date('Y-m-d H:i:s');
  210. if($time>3600 && file_exists($this->options['log_file'])){
  211. $time = 0;
  212. file_put_contents($this->options['log_file'],"Task {$date}:清空日志\n");
  213. }
  214. $time++;
  215. $cateIds = GoodsCategoryService::make()->getCateSubIds();
  216. if($cateIds){
  217. foreach($cateIds as $item){
  218. $pid = isset($item['cate_id'])? $item['cate_id'] : 0;
  219. if($pid && !RedisService::get("caches:task:lock:goods_buy_category_loaded_{$pid}")){
  220. $taskData = [
  221. 'taskName' => 'UpdateGoodsByCategory',
  222. 'name' => "更新商品数据按【{$pid}】的分类同步",
  223. 'pid'=> $pid,
  224. 'date' => date('Y-m-d'),
  225. ];
  226. $res = $serv->task($taskData);
  227. RedisService::set("caches:task:lock:goods_buy_category_loaded_{$pid}", true, rand(3,5));
  228. echo "[Task UpdateGoodsByCategory {$date}] 更新商品数据按【{$pid}】的分类同步:{$res}\n";
  229. }else{
  230. echo "[Task UpdateGoodsByCategory {$date}] 间隔时间调用\n";
  231. }
  232. }
  233. }else{
  234. echo "[Task UpdateGoodsByCategory {$date}] 没有分类\n";
  235. }
  236. });
  237. // TODO 更新商品分类
  238. \swoole_timer_tick(600000, function ($timer) use ($serv, &$time) { // 启用定时器,每120秒执行一次
  239. $date = date('Y-m-d H:i:s');
  240. if($time>3600 && file_exists($this->options['log_file'])){
  241. $time = 0;
  242. file_put_contents($this->options['log_file'],"Task {$date}:清空日志\n");
  243. }
  244. $time++;
  245. if(!RedisService::get('caches:task:lock:goods_category_loaded')){
  246. $taskData = [
  247. 'taskName' => 'UpdateGoodsCategory',
  248. 'name' => "更新商品分类数据",
  249. 'date' => date('Y-m-d'),
  250. ];
  251. $res = $serv->task($taskData);
  252. RedisService::set('caches:task:lock:goods_category_loaded', true, rand(3,5));
  253. echo "[Task UpdateGoodsCategory {$date}] 更新商品分类数据:{$res}\n";
  254. }else{
  255. echo "[Task UpdateGoodsCategory {$date}] 间隔时间调用\n";
  256. }
  257. });
  258. // TODO 更新商品分类
  259. \swoole_timer_tick(300000, function ($timer) use ($serv, &$time) { // 启用定时器,每120秒执行一次
  260. $date = date('Y-m-d H:i:s');
  261. if($time>3600 && file_exists($this->options['log_file'])){
  262. $time = 0;
  263. file_put_contents($this->options['log_file'],"Task {$date}:清空日志\n");
  264. }
  265. $time++;
  266. $cateIds = GoodsCategoryService::make()->getCateIds();
  267. if($cateIds){
  268. foreach($cateIds as $item){
  269. $pid = isset($item['cate_id'])? $item['cate_id'] : 0;
  270. if($pid && !RedisService::get("caches:task:lock:goods_category_sub_loaded_{$pid}")){
  271. $taskData = [
  272. 'taskName' => 'UpdateGoodsCategorySub',
  273. 'name' => "更新商品分类【{$pid}】的子分类数据",
  274. 'pid'=> $pid,
  275. 'date' => date('Y-m-d'),
  276. ];
  277. $res = $serv->task($taskData);
  278. RedisService::set("caches:task:lock:goods_category_sub_loaded_{$pid}", true, rand(3,5));
  279. echo "[Task UpdateGoodsCategorySub {$date}] 更新商品分类【{$pid}】的子分类数据:{$res}\n";
  280. }else{
  281. echo "[Task UpdateGoodsCategorySub {$date}] 间隔时间调用\n";
  282. }
  283. }
  284. }else{
  285. echo "[Task UpdateGoodsCategorySub {$date}] 没有父级数据\n";
  286. }
  287. });
  288. // TODO 更新商品SKU数据
  289. \swoole_timer_tick(120000, function ($timer) use ($serv, &$time) { // 启用定时器,每120秒执行一次
  290. $date = date('Y-m-d H:i:s');
  291. if($time>3600 && file_exists($this->options['log_file'])){
  292. $time = 0;
  293. file_put_contents($this->options['log_file'],"Task {$date}:清空日志\n");
  294. }
  295. $time++;
  296. if(!RedisService::get('caches:task:lock:goods_sku_loaded')){
  297. $taskData = [
  298. 'taskName' => 'UpdateGoodsSku',
  299. 'name' => "更新商品SKU数据",
  300. 'date' => date('Y-m-d'),
  301. ];
  302. $res = $serv->task($taskData);
  303. RedisService::set('caches:task:lock:goods_sku_loaded', true, rand(3,5));
  304. echo "[Task UpdateGoodsSku {$date}] 更新商品SKU数据:{$res}\n";
  305. }else{
  306. echo "[Task UpdateGoodsSku {$date}] 间隔时间调用\n";
  307. }
  308. });
  309. }
  310. //监听连接进入事件
  311. public function onConnect($serv, $fd, $from_id)
  312. {
  313. $serv->send($fd, "Success {$fd}!");
  314. }
  315. // 监听数据接收事件
  316. public function onReceive(\Swoole\Server $serv, $fd, $from_id, $data)
  317. {
  318. echo "Get Message From Client {$fd}:{$data}\n";
  319. $res['result'] = 'success';
  320. $serv->send($fd, json_encode($res)); // 同步返回消息给客户端
  321. $serv->task($data); // 执行异步任务
  322. }
  323. /**
  324. * @param \Swoole\Server $serv
  325. * @param $task_id
  326. * @param $from_id
  327. * @param $data
  328. * @return false|string
  329. */
  330. public function onTask(\Swoole\Server $serv, $task_id, $from_id, $data)
  331. {
  332. $date = date('Y-m-d H:i:s');
  333. $taskName = isset($data['taskName']) ? $data['taskName'] : '';
  334. try {
  335. switch ($taskName) {
  336. case 'UpdateGoods': // 更新商品
  337. // 时间限制
  338. $updateTimeLimit = ConfigService::make()->getConfigByCode('update_goods_limit_time', 1);
  339. if($updateTimeLimit==1 && (date('H:i') <= '03:00' || (date('H:i') >= '08:00' && date('H:i') <= '20:00'))){
  340. echo "[Task {$taskName} {$date}] 不在运行时间段内\n";
  341. return false;
  342. }
  343. // 调用处理
  344. if($res = GoodsService::make()->updateGoods()){
  345. $res = is_array($res) && $res? json_encode($res, 256) : 'success';
  346. echo "[Task {$taskName} {$date}] 商品数据获取更新结果:{$res}\n";
  347. }else{
  348. $error = GoodsService::make()->getError();
  349. $error = $error? lang($error) : 'failed';
  350. echo "[Task {$taskName} {$date}] 商品数据获取更新结果:{$error}\n";
  351. }
  352. break;
  353. case 'UpdateGoodsByCategory': // 按分类更新商品
  354. // 时间限制
  355. $updateTimeLimit = ConfigService::make()->getConfigByCode('update_goods_limit_time', 1);
  356. if($updateTimeLimit==1 && (date('H:i') <= '03:00' || (date('H:i') >= '08:00' && date('H:i') <= '20:00'))){
  357. echo "[Task {$taskName} {$date}] 不在运行时间段内\n";
  358. return false;
  359. }
  360. // 调用处理
  361. $pid = isset($data['pid'])? $data['pid'] : 0;
  362. if($res = GoodsService::make()->updateGoods(200, ['cate_id'=> $pid])){
  363. $res = is_array($res) && $res? json_encode($res, 256) : 'success';
  364. echo "[Task {$taskName} {$date}] 按分类更新商品结果:{$res}\n";
  365. }else{
  366. $error = GoodsService::make()->getError();
  367. $error = $error? lang($error) : 'failed';
  368. echo "[Task {$taskName} {$date}] 按分类更新商品结果:{$error}\n";
  369. }
  370. break;
  371. case 'UpdateGoodsSku': // 更新商品SKu数据
  372. // 时间限制
  373. if(date('H:i') <= '03:00' || (date('H:i') >= '08:00' && date('H:i') <= '20:00')){
  374. echo "[Task {$taskName} {$date}] 不在运行时间段内\n";
  375. return false;
  376. }
  377. // 调用处理
  378. if($res = GoodsService::make()->updateGoodsSku()){
  379. $res = is_array($res) && $res? json_encode($res, 256) : 'success';
  380. echo "[Task {$taskName} {$date}] 更新商品SKu数据结果:{$res}\n";
  381. }else{
  382. $error = GoodsService::make()->getError();
  383. $error = $error? lang($error) : 'failed';
  384. echo "[Task {$taskName} {$date}] 更新商品SKu数据结果:{$error}\n";
  385. }
  386. break;
  387. case 'UpdateGoodsCategory': // 更新商品分类数据
  388. // 时间限制
  389. if(date('H:i') <= '04:00' || (date('H:i') >= '08:00' && date('H:i') <= '20:00')){
  390. echo "[Task {$taskName} {$date}] 不在运行时间段内\n";
  391. return false;
  392. }
  393. // 调用处理
  394. if($res = GoodsService::make()->updateGoodsCategory()){
  395. $res = is_array($res) && $res? json_encode($res, 256) : 'success';
  396. echo "[Task {$taskName} {$date}] 更新商品分类数据结果:{$res}\n";
  397. }else{
  398. $error = GoodsService::make()->getError();
  399. $error = $error? lang($error) : 'failed';
  400. echo "[Task {$taskName} {$date}] 更新商品分类数据结果:{$error}\n";
  401. }
  402. break;
  403. case 'UpdateGoodsCategorySub': // 更新商品分类数据
  404. // 时间限制
  405. if(date('H:i') <= '04:00' || (date('H:i') >= '08:00' && date('H:i') <= '20:00')){
  406. echo "[Task {$taskName} {$date}] 不在运行时间段内\n";
  407. return false;
  408. }
  409. // 调用处理
  410. $pid = isset($data['pid'])? $data['pid'] : 0;
  411. if($res = GoodsService::make()->updateGoodsCategory($pid)){
  412. $res = is_array($res) && $res? json_encode($res, 256) : 'success';
  413. echo "[Task {$taskName} {$date}] 更新商品分类子类数据结果:{$res}\n";
  414. }else{
  415. $error = GoodsService::make()->getError();
  416. $error = $error? lang($error) : 'failed';
  417. echo "[Task {$taskName} {$date}] 更新商品分类子类数据结果:{$error}\n";
  418. }
  419. break;
  420. }
  421. } catch(\Exception $exception){
  422. return $exception->getMessage();
  423. }
  424. return '暂无任务处理';
  425. }
  426. /**
  427. * @param $serv swoole_server swoole_server对象
  428. * @param $task_id int 任务id
  429. * @param $data string 任务返回的数据
  430. */
  431. public function onFinish(\Swoole\Server $serv, $task_id, $data)
  432. {
  433. //
  434. echo "任务处理完成...\n";
  435. }
  436. // 监听连接关闭事件
  437. public function onClose($serv, $fd, $from_id)
  438. {
  439. echo "Client {$fd} close connection\n";
  440. $serv->close();
  441. }
  442. }