作者 zhl

u

@@ -2,21 +2,17 @@ @@ -2,21 +2,17 @@
2 2
3 namespace App\Console\Commands; 3 namespace App\Console\Commands;
4 4
5 -use App\Http\Logic\Aside\Project\ProjectLogic;  
6 use App\Models\Com\NoticeLog; 5 use App\Models\Com\NoticeLog;
7 use App\Models\Product\Keyword; 6 use App\Models\Product\Keyword;
8 -use App\Models\Project\Project;  
9 use App\Models\RouteMap\RouteMap; 7 use App\Models\RouteMap\RouteMap;
10 use App\Services\ProjectServer; 8 use App\Services\ProjectServer;
11 use Illuminate\Console\Command; 9 use Illuminate\Console\Command;
12 use Illuminate\Support\Facades\DB; 10 use Illuminate\Support\Facades\DB;
  11 +use Illuminate\Support\Facades\Redis;
13 12
14 /** 13 /**
15 - * 初始化项目  
16 - * Class InitProject 14 + * Class InitKeyword
17 * @package App\Console\Commands 15 * @package App\Console\Commands
18 - * @author zbj  
19 - * @date 2023/10/8  
20 */ 16 */
21 class InitKeyword extends Command 17 class InitKeyword extends Command
22 { 18 {
@@ -50,29 +46,82 @@ class InitKeyword extends Command @@ -50,29 +46,82 @@ class InitKeyword extends Command
50 public function handle() 46 public function handle()
51 { 47 {
52 while (true){ 48 while (true){
53 - $list = NoticeLog::where('type', NoticeLog::TYPE_INIT_KEYWORD)->where('status', NoticeLog::STATUS_PENDING)->get();  
54 - if(!empty($list)){  
55 - foreach ($list as $item){  
56 - echo 'start:' . $item['data']['project_id'] . PHP_EOL;  
57 - ProjectServer::useProject($item['data']['project_id']);  
58 - $keywordModel = new Keyword();  
59 - $list = $keywordModel->list(['route'=>'']);  
60 - echo 'start:' . json_encode($list) . PHP_EOL;  
61 - foreach ($list as $v){  
62 - $route = RouteMap::setRoute($v['title'],RouteMap::SOURCE_PRODUCT_KEYWORD,$v['id'],$item['data']['project_id']);  
63 - if(empty($route)){  
64 - $keywordModel->del(['id'=>$v['id']]); 49 + $notice_id = $this->getTask();
  50 + if (empty($notice_id)) {
  51 + sleep(30);
65 continue; 52 continue;
66 } 53 }
67 - $keywordModel->edit(['route'=>$route],['id'=>$v['id']]); 54 + try {
  55 + $this->output(' taskID: ' . $notice_id . ' start');
  56 + $this->bind($notice_id);
  57 + $this->output(' taskID: ' . $notice_id . ' end');
  58 + } catch (\Exception $e) {
  59 + $this->output(' taskID: ' . $notice_id . ', error: ' . $e->getMessage());
68 } 60 }
69 - $item->status = NoticeLog::STATUS_SUCCESS;  
70 - $item->save();  
71 - DB::disconnect('custom_mysql'); 61 + sleep(2);
72 } 62 }
  63 + return true;
  64 + }
  65 +
  66 + /**
  67 + * 处理子任务
  68 + * @param $notice_id
  69 + * @return bool
  70 + * @throws \Exception
  71 + */
  72 + public function bind($notice_id)
  73 + {
  74 + $notice = NoticeLog::where(['id' => $notice_id])->first();
  75 + if (empty($notice) || $notice->type != NoticeLog::TYPE_INIT_KEYWORD || $notice->status != NoticeLog::STATUS_PENDING)
  76 + return true;
  77 +
  78 +
  79 + ProjectServer::useProject($notice['data']['project_id']);
  80 +
  81 + $keyword = Keyword::whereNull('route')->get();
  82 + foreach ($keyword as $val) {
  83 + $this->output(' keywordID: ' . $val->id . ', title: ' . $val->title);
  84 +
  85 + try {
  86 + $route = RouteMap::setRoute($val['title'],RouteMap::SOURCE_PRODUCT_KEYWORD, $val->id, $notice['data']['project_id']);
  87 + $val->route = $route;
  88 + $val->save();
  89 + } catch (\Exception $e) {
  90 + $this->output(' keywordID: ' . $val->id . ', title: ' . $val->title . ', error: ' . $e->getMessage());
73 } 91 }
74 - sleep(2);  
75 } 92 }
  93 +
  94 + $notice->status = NoticeLog::STATUS_SUCCESS;
  95 + $notice->save();
  96 +
  97 + DB::disconnect('custom_mysql');
  98 + return true;
76 } 99 }
77 100
  101 + /**
  102 + * 获取需要处理的任务
  103 + * @return mixed
  104 + */
  105 + public function getTask()
  106 + {
  107 + $key = 'notice_log_type_keyword';
  108 + $notice_id = Redis::rpop($key);
  109 + if ($notice_id)
  110 + return $notice_id;
  111 + $ids = NoticeLog::where('type', NoticeLog::TYPE_INIT_KEYWORD)->where('status', NoticeLog::STATUS_PENDING)->limit(100)->pluck('id');
  112 + foreach ($ids as $id) {
  113 + Redis::lpush($key, $id);
  114 + }
  115 + $notice_id = Redis::rpop($key);
  116 + return $notice_id;
  117 + }
  118 +
  119 + /**
  120 + * 输出message
  121 + * @param $message
  122 + */
  123 + public function output($message)
  124 + {
  125 + echo date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
  126 + }
78 } 127 }
@@ -100,11 +100,11 @@ class KeywordController extends BaseController @@ -100,11 +100,11 @@ class KeywordController extends BaseController
100 } 100 }
101 101
102 /** 102 /**
103 - * @remark :批量添加  
104 - * @name :batchAdd  
105 - * @author :lyh  
106 - * @method :post  
107 - * @time :2023/8/28 14:25 103 + * 批量添加关键词
  104 + * FIXME 添加通知, 异步处理任务
  105 + * @param KeywordLogic $logic
  106 + * @throws \App\Exceptions\AsideGlobalException
  107 + * @throws \App\Exceptions\BsideGlobalException
108 */ 108 */
109 public function batchAdd(KeywordLogic $logic){ 109 public function batchAdd(KeywordLogic $logic){
110 $this->request->validate([ 110 $this->request->validate([
@@ -115,7 +115,7 @@ class KeywordController extends BaseController @@ -115,7 +115,7 @@ class KeywordController extends BaseController
115 'title.max' => '批量操作不能超过1000条数据' 115 'title.max' => '批量操作不能超过1000条数据'
116 ]); 116 ]);
117 $logic->batchAdd(); 117 $logic->batchAdd();
118 - $this->response('路由生成中,请稍后刷新查看'); 118 + $this->response('关键词后台异步添加中,请稍后刷新查看!');
119 } 119 }
120 120
121 /** 121 /**
@@ -130,11 +130,10 @@ class KeywordLogic extends BaseLogic @@ -130,11 +130,10 @@ class KeywordLogic extends BaseLogic
130 } 130 }
131 131
132 /** 132 /**
133 - * @remark :批量添加数据  
134 - * @name :batchAdd  
135 - * @author :lyh  
136 - * @method :post  
137 - * @time :2023/8/28 14:03 133 + * 批量添加关键词任务, 异步处理
  134 + * @return array
  135 + * @throws BsideGlobalException
  136 + * @throws \App\Exceptions\AsideGlobalException
138 */ 137 */
139 public function batchAdd(){ 138 public function batchAdd(){
140 try { 139 try {
@@ -154,7 +153,7 @@ class KeywordLogic extends BaseLogic @@ -154,7 +153,7 @@ class KeywordLogic extends BaseLogic
154 } 153 }
155 NoticeLog::createLog(NoticeLog::TYPE_INIT_KEYWORD, ['project_id' => $this->user['project_id']]); 154 NoticeLog::createLog(NoticeLog::TYPE_INIT_KEYWORD, ['project_id' => $this->user['project_id']]);
156 }catch (\Exception $e){ 155 }catch (\Exception $e){
157 - $this->fail('error'); 156 + $this->fail('创建任务添加关键词任务失败,请稍后重试!');
158 } 157 }
159 return $this->success(); 158 return $this->success();
160 } 159 }