cqp 7 jam lalu
induk
melakukan
036129d16b
4 mengubah file dengan 37 tambahan dan 18 penghapusan
  1. 25 11
      app/Console/Kernel.php
  2. 6 4
      app/Jobs/ProcessWMSDataJob.php
  3. 0 3
      artisan_runner.bat
  4. 6 0
      config/logging.php

+ 25 - 11
app/Console/Kernel.php

@@ -25,6 +25,31 @@ class Kernel extends ConsoleKernel
      */
     protected function schedule(Schedule $schedule)
     {
+        //开启队列
+        if (str_contains(PHP_OS, 'WIN')) {
+            $schedule->call(function () {
+                $queueName = "sync_wms_order";
+                $phpPath = "D:/phpstudy_pro/Extensions/php/php7.4.3nts/php.exe";
+                $artisan = base_path('artisan');
+
+                // 修改点:增加一个 AND 过滤,排除掉包含 "schedule:run" 的进程
+                // 并且确保匹配的是 "queue:work"
+                $checkCmd = 'wmic process where "name=\'php.exe\' and commandline like \'%queue:work%--queue=' . $queueName . '%\' and not commandline like \'%schedule:run%\'" get processid /format:list';
+
+                $output = shell_exec($checkCmd);
+
+                if (empty(trim($output)) || str_contains($output, 'No Instance')) {
+                    // 使用绝对路径启动
+                    $runCmd = "start /B \"\" \"$phpPath\" \"$artisan\" queue:work redis --queue=$queueName --memory=512";
+                    pclose(popen($runCmd, "r"));
+
+                    Log::channel('queue_daily')->info("单进程守卫:未检测到队列[$queueName],已尝试拉起。");
+                } else {
+                    Log::channel('queue_daily')->info('单进程守卫:队列正在运行中,无需拉起。', ['pid' => trim($output)]);
+                }
+            })->everyMinute();
+        }
+
         // 每五分钟同步一次 外部定时任务 然后 物料第一次同步检验手动同步
         $schedule->command('command:u8_settle_inventory')
             ->everyMinute()
@@ -36,17 +61,6 @@ class Kernel extends ConsoleKernel
             ->onFailure(function(){
                 Log::channel('u8_daily')->warning('物料同步失败,单据同步已挂起。');
             });
-
-        if (str_contains(PHP_OS, 'WIN')) {
-            $schedule->call(function () {
-                // 启动新队列
-                // 使用 start /B 配合 php 运行,WindowStyle Hidden 隐藏窗口
-                $cmd = "start /B php artisan queue:work redis --queue=sync_wms_order";
-                pclose(popen($cmd, "r"));
-
-                \Illuminate\Support\Facades\Log::channel('u8_daily')->info("单进程守卫:未检测到运行中的队列,已成功拉起新进程。");
-            })->everyMinute();
-        }
     }
 
     /**

+ 6 - 4
app/Jobs/ProcessWMSDataJob.php

@@ -19,7 +19,7 @@ class ProcessWMSDataJob implements ShouldQueue
     use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
 
     protected $data;
-    public $timeout = 30;
+    public $timeout = 60;
 
     public function __construct($data)
     {
@@ -32,8 +32,10 @@ class ProcessWMSDataJob implements ShouldQueue
         try {
             list($bool, $msg) = $this->settle();
             if(! $bool) $this->finalDo($msg);
+            Log::channel('queue_daily')->info('队列执行任务');
         } catch (\Throwable $e) {
             $this->finalDo("异常:" . $e->getMessage());
+            Log::channel('queue_daily')->info('队列异常', ['msg' => $e->getMessage() . '|' . $e->getLine()]);
             $this->delete();
         }
     }
@@ -280,7 +282,7 @@ class ProcessWMSDataJob implements ShouldQueue
 
     public function post_helper($url, $data, $header = [], $timeout = 30)
     {
-        Log::channel('apiLog')->info('WMS_POST_START', ["api" => $url, "param" => $data]);
+        Log::channel('apiLog')->info('入参', ["api" => $url, "param" => $data]);
 
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $url);
@@ -296,13 +298,13 @@ class ProcessWMSDataJob implements ShouldQueue
         if ($r === false) {
             $errorMessage = curl_error($ch);
             curl_close($ch);
-            Log::channel('apiLog')->error('WMS_CURL_ERROR', ["msg" => $errorMessage]);
+            Log::channel('apiLog')->error('CURL错误', ["msg" => $errorMessage]);
             return [false, "网络错误: " . $errorMessage];
         }
         curl_close($ch);
 
         $return = json_decode($r, true);
-        Log::channel('apiLog')->info('WMS_POST_RETURN', ["res" => $return]);
+        Log::channel('apiLog')->info('出参', ["res" => $return]);
 
         // 判断逻辑:code 存在且为 200 才算 true
         if (isset($return['code']) && $return['code'] == 200) {

+ 0 - 3
artisan_runner.bat

@@ -1,7 +1,4 @@
 @echo off
-:: 1. 依然建议保留清理,防止僵尸进程堆积
-taskkill /F /IM php.exe /T >nul 2>&1
-
 d:
 cd D:\phpstudy_pro\WWW\drb
 

+ 6 - 0
config/logging.php

@@ -127,6 +127,12 @@ return [
             'level' => 'info',
             'days' => 14, // 自动只保留最近14天的日志
         ],
+        'queue_daily' => [
+            'driver' => 'daily',
+            'path' => storage_path('logs/queue_details.log'),
+            'level' => 'info',
+            'days' => 14, // 自动只保留最近14天的日志
+        ],
     ],
 
 ];