|
@@ -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']]);
|
|
|
|
65
|
- continue;
|
|
|
|
66
|
- }
|
|
|
|
67
|
- $keywordModel->edit(['route'=>$route],['id'=>$v['id']]);
|
|
|
|
68
|
- }
|
|
|
|
69
|
- $item->status = NoticeLog::STATUS_SUCCESS;
|
|
|
|
70
|
- $item->save();
|
|
|
|
71
|
- DB::disconnect('custom_mysql');
|
|
|
|
72
|
- }
|
49
|
+ $notice_id = $this->getTask();
|
|
|
|
50
|
+ if (empty($notice_id)) {
|
|
|
|
51
|
+ sleep(30);
|
|
|
|
52
|
+ continue;
|
|
|
|
53
|
+ }
|
|
|
|
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());
|
|
73
|
}
|
60
|
}
|
|
74
|
sleep(2);
|
61
|
sleep(2);
|
|
75
|
}
|
62
|
}
|
|
|
|
63
|
+ return true;
|
|
76
|
}
|
64
|
}
|
|
77
|
|
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());
|
|
|
|
91
|
+ }
|
|
|
|
92
|
+ }
|
|
|
|
93
|
+
|
|
|
|
94
|
+ $notice->status = NoticeLog::STATUS_SUCCESS;
|
|
|
|
95
|
+ $notice->save();
|
|
|
|
96
|
+
|
|
|
|
97
|
+ DB::disconnect('custom_mysql');
|
|
|
|
98
|
+ return true;
|
|
|
|
99
|
+ }
|
|
|
|
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
|
} |