佇列
使用此技能於此原始碼儲存庫中進行 WelineFramework 佇列檢查、修復與操作。除非需要原始碼層級診斷而必須檢查模型內部,否則請優先使用框架查詢提供者與佇列 CLI,而非直接操作資料庫。
初次上手
- 在假設文件為最新之前,請先閱讀目前的佇列原始碼:
app/code/Weline/Queue/extends/module/Weline_Framework/Query/QueueQueryProvider.phpapp/code/Weline/Queue/Console/Queue/Collect.phpapp/code/Weline/Queue/Console/Queue/Run.phpapp/code/Weline/Queue/Console/Queue/Type/Listing.phpapp/code/Weline/Queue/Model/Queue.php
- 使用
w_query('queue', ...)進行執行階段的佇列讀取與商業層級的寫入。直接資料庫讀取可能遺漏框架型別轉換、EAV/模型行為、事件分發以及目前的查詢提供者語義。 - 維護副作用邊界。
stats、get、getByBizKey、list、getTypeIdByClass和queue:type:listing屬於診斷操作;create、update、delete、queue:collect和queue:run會變更狀態或執行工作。 - 新的跨模組消費者應實作
Weline\Queue\Api\QueueConsumerInterface,並接收QueueTaskContextInterface。Weline\Queue\QueueInterface僅作為現有第三方消費者的執行階段相容性邊界。
佇列 CLI
在儲存庫根目錄下使用下列命令。
php bin/w queue:collect
從模組中收集佇列類型至 weline_queue_type。在新增或變更佇列類別後,或當 getTypeIdByClass 無法解析類別時執行此命令。收集器僅註冊實作公開 Weline\Queue\Api\QueueConsumerInterface 或舊版 Weline\Queue\QueueInterface 的可實例化類別;位於 Queue/ 目錄中的輔助/靜態類別並非佇列實作。
php bin/w queue:type:listing
php bin/w queue:type:listing ExampleQueue
php bin/w queue:type:listing Vendor_Module
列出已註冊的佇列類型。額外引數為搜尋條件,會與名稱、模組和類別進行比對。
php bin/w queue:run --id=77
php bin/w queue:run --id=77 -f
php bin/w queue:run --id=77 --force
透過 weline_queue.queue_id 執行一個佇列項目。
僅在有意接管或重建相同佇列項目時使用 -f/--force。目前行為:
- 如果相同佇列標記為
running,強制模式會在繼續執行前終止舊的相同 ID 程序。 - 若內容為 JSON,則注入
_force_rebuild=1至佇列內容。 - 清除先前的
result與process,使新日誌更易閱讀。
除非目前原始碼中包含該命令的類別,否則請勿將 queue:status 視為實際命令。
w_query 範例
引導應用程式進行臨時診斷:
php -r "require __DIR__ . '/app/bootstrap.php'; var_export(w_query('queue', 'stats'));"
讀取一個佇列:
php -r "require __DIR__ . '/app/bootstrap.php'; print_r(w_query('queue', 'get', ['queue_id' => 77]));"
透過業務金鑰尋找最新佇列:
php -r "require __DIR__ . '/app/bootstrap.php'; print_r(w_query('queue', 'getByBizKey', ['biz_key' => 'example:1']));"
列出最近的佇列,避免傾印大量串流日誌:
php -r "require __DIR__ . '/app/bootstrap.php'; $r=w_query('queue','list',['page_size'=>10]); foreach($r['items'] as $it){ $row=is_object($it)&&method_exists($it,'getData')?$it->getData():$it; echo json_encode(['queue_id'=>$row['queue_id']??null,'status'=>$row['status']??null,'module'=>$row['module']??null,'name'=>$row['name']??null,'biz_key'=>$row['biz_key']??null], JSON_UNESCAPED_UNICODE), PHP_EOL; }"
依狀態或搜尋文字篩選:
php -r "require __DIR__ . '/app/bootstrap.php'; $r=w_query('queue','list',['status'=>'error','q'=>'example','page_size'=>20]); foreach($r['items'] as $it){ $row=is_object($it)&&method_exists($it,'getData')?$it->getData():$it; echo ($row['queue_id']??'') . ' ' . ($row['status']??'') . ' ' . ($row['name']??'') . PHP_EOL; }"
透過類別解析類型 ID:
php -r "require __DIR__ . '/app/bootstrap.php'; var_export(w_query('queue','getTypeIdByClass',['class'=>'Vendor\\Module\\Queue\\ExampleQueue']));"
透過提供者建立佇列:
php -r "require __DIR__ . '/app/bootstrap.php'; print_r(w_query('queue','create',['class'=>'Vendor\\Module\\Queue\\ExampleQueue','name'=>'Example task','module'=>'Vendor_Module','content'=>['foo'=>'bar'],'biz_key'=>'example:1']));"
更新安全欄位子集:
php -r "require __DIR__ . '/app/bootstrap.php'; print_r(w_query('queue','update',['queue_id'=>77,'patch'=>['status'=>'pending','pid'=>0,'result'=>'','process'=>'']]));"
僅在有意時刪除:
php -r "require __DIR__ . '/app/bootstrap.php'; print_r(w_query('queue','delete',['queue_id'=>77]));"
僅在佇列正在執行且明確意圖刪除時,使用 force => true。
提供者操作
w_query('queue', OP, PARAMS) 目前支援:
get/load:需提供queue_id或id;回傳單一佇列資料列或null。getByBizKey:需提供biz_key;回傳最新符合的資料列。list:篩選條件包含page、page_size、module、status、type_id、queue_id、biz_key和q;回傳items與pagination。stats:回傳all、pending、running、done、error和stop的計數。getTypeIdByClass:解析公開或舊版佇列消費者類別至type_id;若缺少則內部執行收集。create:需提供name、module,以及type_id或class;可選content、status、auto、biz_key。update:透過queue_id/id或biz_key定位;傳入patch或頂層欄位。安全修補欄位包括name、module、status、content、result、process、biz_key、auto、finished、pid和type_id。delete:透過queue_id/id或biz_key定位;執行中的佇列需使用force。
有效狀態為 pending、running、done、error 和 stop。
驗證
在佇列註冊或消費者合約篩選變更後:
php -l app/code/Weline/Queue/Helper/Helper.php
php bin/w queue:collect
php bin/w queue:type:listing
在命令行為變更後:
php bin/w queue:run --help
php bin/w queue:collect --help
php bin/w queue:type:listing --help