Kernel.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Console;
  3. use Illuminate\Console\Scheduling\Schedule;
  4. use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
  5. use Illuminate\Support\Facades\Log;
  6. class Kernel extends ConsoleKernel
  7. {
  8. /**
  9. * The Artisan commands provided by your application.
  10. *
  11. * @var array
  12. */
  13. protected $commands = [
  14. //
  15. ];
  16. /**
  17. * Define the application's command schedule.
  18. *
  19. * @param \Illuminate\Console\Scheduling\Schedule $schedule
  20. * @return void
  21. */
  22. protected function schedule(Schedule $schedule)
  23. {
  24. //开启队列
  25. if (str_contains(PHP_OS, 'WIN')) {
  26. $schedule->call(function () {
  27. $queueName = "sync_wms_order";
  28. $phpPath = "D:/phpstudy_pro/Extensions/php/php7.4.3nts/php.exe";
  29. $artisan = base_path('artisan');
  30. // 修改点:增加一个 AND 过滤,排除掉包含 "schedule:run" 的进程
  31. // 并且确保匹配的是 "queue:work"
  32. $checkCmd = 'wmic process where "name=\'php.exe\' and commandline like \'%queue:work%--queue=' . $queueName . '%\' and not commandline like \'%schedule:run%\'" get processid /format:list';
  33. $output = shell_exec($checkCmd);
  34. if (empty(trim($output)) || str_contains($output, 'No Instance')) {
  35. // 使用绝对路径启动
  36. $runCmd = "start /B \"\" \"$phpPath\" \"$artisan\" queue:work redis --queue=$queueName --memory=512";
  37. pclose(popen($runCmd, "r"));
  38. Log::channel('queue_daily')->info("单进程守卫:未检测到队列[$queueName],已尝试拉起。");
  39. } else {
  40. Log::channel('queue_daily')->info('单进程守卫:队列正在运行中,无需拉起。', ['pid' => trim($output)]);
  41. }
  42. })->everyMinute();
  43. }
  44. // 每五分钟同步一次 外部定时任务 然后 物料第一次同步检验手动同步
  45. $schedule->command('command:u8_settle_inventory')
  46. ->everyMinute()
  47. ->withoutOverlapping(10) // 锁长时间一点,给第一次同步留足时间
  48. ->onSuccess(function () {
  49. // 2. 只有当 U8SettleInventory 的 handle() 返回 0 时,才执行单据同步
  50. \Illuminate\Support\Facades\Artisan::call('command:u8_settle');
  51. })
  52. ->onFailure(function(){
  53. Log::channel('u8_daily')->warning('物料同步失败,单据同步已挂起。');
  54. });
  55. }
  56. /**
  57. * Register the commands for the application.
  58. *
  59. * @return void
  60. */
  61. protected function commands()
  62. {
  63. $this->load(__DIR__.'/Commands');
  64. require base_path('routes/console.php');
  65. }
  66. }