作者 lyh

gx

<?php
/**
* @remark :
* @name :GeneratePage.php
* @author :lyh
* @method :post
* @time :2025/2/10 17:00
*/
namespace App\Console\Commands\Project;
use App\Helper\Translate;
use App\Models\Com\NoticeLog;
use App\Models\Product\Keyword;
use App\Models\RouteMap\RouteMap;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
class GeneratePage extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'generate_page';
/**
* The console command description.
*
* @var string
*/
protected $description = '批量导入关键字生成路由';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* @return bool
*/
public function handle()
{
$noticeModel = new NoticeLog();
while (true){
$noticeInfo = $noticeModel->read(['status'=>0]);
if (empty($noticeInfo)) {
sleep(10);
continue;
}
try {
$this->output(' taskID: ' . $noticeInfo['id'] . ' start');
$c_url = $noticeInfo['data']['c_url'];
$c_params = $noticeInfo['data']['c_params'];
try {
$re = http_post($c_url, $c_params, [], true);
if (isset($re['status']) && $re['status'] == 200) {
$this->output($c_url . ' | 请求成功');
} else {
$this->output($c_url . ' | ' . ($re['message'] ?? '未返回失败原因'));
}
} catch (\Exception $e) {
$this->output($c_url . ' | 请求异常:' . $e->getMessage());
}
$this->output(' taskID: ' . $noticeInfo['id'] . ' end');
} catch (\Exception $e) {
$this->output(' taskID: ' . $noticeInfo['id'] . ', error: ' . $e->getMessage());
}
sleep(2);
}
return true;
}
/**
* 输出message
* @param $message
*/
public function output($message)
{
echo date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
}
}
... ...
... ... @@ -15,6 +15,7 @@ use App\Http\Controllers\Bside\BaseController;
use App\Jobs\UpdatePageJob;
use App\Models\Blog\Blog;
use App\Models\Blog\BlogCategory;
use App\Models\Com\NoticeLog;
use App\Models\Com\Notify;
use App\Models\Com\UpdateLog;
use App\Models\Com\UpdateNotify;
... ... @@ -290,9 +291,7 @@ class CNoticeController extends BaseController
'is_sitemap' => $is_sitemap
];
// http_post($c_url, json_encode($c_param));
//2024-10-11:改为异步请求
UpdatePageJob::dispatch(['c_url'=>$c_url,'c_params'=>json_encode($c_param)]);
NoticeLog::createLog(NoticeLog::GENERATE_PAGE, json_encode(['c_url'=>$c_url,'c_params'=>$c_param]));
}
$this->response('更新中请稍后, 更新完成将会发送站内信通知更新结果!');
}
... ...
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class UpdatePageJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $tries = 3; // 可配置任务重试次数
protected $param;
/**
* Create a new job instance.
*
* @param $data
* @return void
*/
public function __construct($data)
{
$this->param = $data;
}
/**
* Execute the job.
* B端更新页面异步请求
* @return bool
*/
public function handle()
{
$c_url = $this->param['c_url'];
$c_params = $this->param['c_params'];
try {
$re = http_post($c_url, $c_params, [], true);
if (isset($re['status']) && $re['status'] == 200) {
$this->output($c_url . ' | 请求成功');
} else {
$this->output($c_url . ' | ' . ($re['message'] ?? '未返回失败原因'));
}
} catch (\Exception $e) {
$this->output($c_url . ' | 请求异常:' . $e->getMessage());
}
return true;
}
/**
* 输出处理日志
* @param $message
*/
public function output($message)
{
echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
}
}
... ... @@ -19,6 +19,7 @@ class NoticeLog extends Base
const DELETE_BLOG_CATEGORY = 'delete_blog_category';
const DELETE_NEWS_CATEGORY = 'delete_news_category';
const DELETE_CUSTOM_CATEGORY = 'delete_custom_category';
const GENERATE_PAGE = 'generate_page';//生成页面单独改为守护进程
const STATUS_PENDING = 0;
const STATUS_SUCCESS = 1;
const STATUS_FAIL = 2;
... ...