作者 zhl

u

... ... @@ -2,21 +2,17 @@
namespace App\Console\Commands;
use App\Http\Logic\Aside\Project\ProjectLogic;
use App\Models\Com\NoticeLog;
use App\Models\Product\Keyword;
use App\Models\Project\Project;
use App\Models\RouteMap\RouteMap;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
/**
* 初始化项目
* Class InitProject
* Class InitKeyword
* @package App\Console\Commands
* @author zbj
* @date 2023/10/8
*/
class InitKeyword extends Command
{
... ... @@ -50,29 +46,82 @@ class InitKeyword extends Command
public function handle()
{
while (true){
$list = NoticeLog::where('type', NoticeLog::TYPE_INIT_KEYWORD)->where('status', NoticeLog::STATUS_PENDING)->get();
if(!empty($list)){
foreach ($list as $item){
echo 'start:' . $item['data']['project_id'] . PHP_EOL;
ProjectServer::useProject($item['data']['project_id']);
$keywordModel = new Keyword();
$list = $keywordModel->list(['route'=>'']);
echo 'start:' . json_encode($list) . PHP_EOL;
foreach ($list as $v){
$route = RouteMap::setRoute($v['title'],RouteMap::SOURCE_PRODUCT_KEYWORD,$v['id'],$item['data']['project_id']);
if(empty($route)){
$keywordModel->del(['id'=>$v['id']]);
continue;
}
$keywordModel->edit(['route'=>$route],['id'=>$v['id']]);
}
$item->status = NoticeLog::STATUS_SUCCESS;
$item->save();
DB::disconnect('custom_mysql');
}
$notice_id = $this->getTask();
if (empty($notice_id)) {
sleep(30);
continue;
}
try {
$this->output(' taskID: ' . $notice_id . ' start');
$this->bind($notice_id);
$this->output(' taskID: ' . $notice_id . ' end');
} catch (\Exception $e) {
$this->output(' taskID: ' . $notice_id . ', error: ' . $e->getMessage());
}
sleep(2);
}
return true;
}
/**
* 处理子任务
* @param $notice_id
* @return bool
* @throws \Exception
*/
public function bind($notice_id)
{
$notice = NoticeLog::where(['id' => $notice_id])->first();
if (empty($notice) || $notice->type != NoticeLog::TYPE_INIT_KEYWORD || $notice->status != NoticeLog::STATUS_PENDING)
return true;
ProjectServer::useProject($notice['data']['project_id']);
$keyword = Keyword::whereNull('route')->get();
foreach ($keyword as $val) {
$this->output(' keywordID: ' . $val->id . ', title: ' . $val->title);
try {
$route = RouteMap::setRoute($val['title'],RouteMap::SOURCE_PRODUCT_KEYWORD, $val->id, $notice['data']['project_id']);
$val->route = $route;
$val->save();
} catch (\Exception $e) {
$this->output(' keywordID: ' . $val->id . ', title: ' . $val->title . ', error: ' . $e->getMessage());
}
}
$notice->status = NoticeLog::STATUS_SUCCESS;
$notice->save();
DB::disconnect('custom_mysql');
return true;
}
/**
* 获取需要处理的任务
* @return mixed
*/
public function getTask()
{
$key = 'notice_log_type_keyword';
$notice_id = Redis::rpop($key);
if ($notice_id)
return $notice_id;
$ids = NoticeLog::where('type', NoticeLog::TYPE_INIT_KEYWORD)->where('status', NoticeLog::STATUS_PENDING)->limit(100)->pluck('id');
foreach ($ids as $id) {
Redis::lpush($key, $id);
}
$notice_id = Redis::rpop($key);
return $notice_id;
}
/**
* 输出message
* @param $message
*/
public function output($message)
{
echo date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
}
}
... ...
... ... @@ -100,11 +100,11 @@ class KeywordController extends BaseController
}
/**
* @remark :批量添加
* @name :batchAdd
* @author :lyh
* @method :post
* @time :2023/8/28 14:25
* 批量添加关键词
* FIXME 添加通知, 异步处理任务
* @param KeywordLogic $logic
* @throws \App\Exceptions\AsideGlobalException
* @throws \App\Exceptions\BsideGlobalException
*/
public function batchAdd(KeywordLogic $logic){
$this->request->validate([
... ... @@ -115,7 +115,7 @@ class KeywordController extends BaseController
'title.max' => '批量操作不能超过1000条数据'
]);
$logic->batchAdd();
$this->response('路由生成中,请稍后刷新查看');
$this->response('关键词后台异步添加中,请稍后刷新查看!');
}
/**
... ...
... ... @@ -130,11 +130,10 @@ class KeywordLogic extends BaseLogic
}
/**
* @remark :批量添加数据
* @name :batchAdd
* @author :lyh
* @method :post
* @time :2023/8/28 14:03
* 批量添加关键词任务, 异步处理
* @return array
* @throws BsideGlobalException
* @throws \App\Exceptions\AsideGlobalException
*/
public function batchAdd(){
try {
... ... @@ -154,7 +153,7 @@ class KeywordLogic extends BaseLogic
}
NoticeLog::createLog(NoticeLog::TYPE_INIT_KEYWORD, ['project_id' => $this->user['project_id']]);
}catch (\Exception $e){
$this->fail('error');
$this->fail('创建任务添加关键词任务失败,请稍后重试!');
}
return $this->success();
}
... ...