Kernel.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. // 获取环境模式,默认为 'test'
  25. $appMode = env('MY_APP_MODE', 'test');
  26. if ($appMode === 'production') {
  27. $this->production($schedule);
  28. }else{
  29. $this->test($schedule);
  30. }
  31. }
  32. public function production($schedule){
  33. //开启队列
  34. if (str_contains(PHP_OS, 'WIN')) {
  35. $schedule->call(function () {
  36. $queueName = "sync_wms_order";
  37. $phpPath = "D:/phpstudy_pro/Extensions/php/php7.4.3nts/php.exe";
  38. $artisan = base_path('artisan');
  39. // 修改点:增加一个 AND 过滤,排除掉包含 "schedule:run" 的进程
  40. // 并且确保匹配的是 "queue:work"
  41. $checkCmd = 'wmic process where "name=\'php.exe\' and commandline like \'%queue:work%--queue=' . $queueName . '%\' and not commandline like \'%schedule:run%\'" get processid /format:list';
  42. $output = shell_exec($checkCmd);
  43. if (empty(trim($output)) || str_contains($output, 'No Instance')) {
  44. // 使用绝对路径启动
  45. $runCmd = "start /B \"\" \"$phpPath\" \"$artisan\" queue:work redis --queue=$queueName --memory=512";
  46. pclose(popen($runCmd, "r"));
  47. Log::channel('queue_daily')->info("单进程守卫:未检测到队列[$queueName],已尝试拉起。");
  48. } else {
  49. Log::channel('queue_daily')->info('单进程守卫:队列正在运行中,无需拉起。', ['pid' => trim($output)]);
  50. }
  51. })->everyMinute();
  52. }
  53. // 每五分钟同步一次 外部定时任务 然后 物料第一次同步检验手动同步
  54. $schedule->command('command:u8_settle_inventory')
  55. ->everyMinute()
  56. ->withoutOverlapping(10) // 锁长时间一点,给第一次同步留足时间
  57. ->onSuccess(function () {
  58. // 2. 只有当 U8SettleInventory 的 handle() 返回 0 时,才执行单据同步
  59. \Illuminate\Support\Facades\Artisan::call('command:u8_settle');
  60. })
  61. ->onFailure(function(){
  62. Log::channel('u8_daily')->warning('物料同步失败,单据同步已挂起。');
  63. });
  64. }
  65. public function test($schedule){
  66. //开启队列
  67. if (str_contains(PHP_OS, 'WIN')) {
  68. $schedule->call(function () {
  69. $queueName = "sync_wms_order";
  70. $phpPath = "D:/phpstudy_pro/Extensions/php/php7.4.3nts/php.exe";
  71. $artisan = base_path('artisan');
  72. // 修改点:增加一个 AND 过滤,排除掉包含 "schedule:run" 的进程
  73. // 并且确保匹配的是 "queue:work"
  74. $checkCmd = 'wmic process where "name=\'php.exe\' and commandline like \'%queue:work%--queue=' . $queueName . '%\' and not commandline like \'%schedule:run%\'" get processid /format:list';
  75. $output = shell_exec($checkCmd);
  76. if (empty(trim($output)) || str_contains($output, 'No Instance')) {
  77. // 使用绝对路径启动
  78. $runCmd = "start /B \"\" \"$phpPath\" \"$artisan\" queue:work redis --queue=$queueName --memory=512";
  79. pclose(popen($runCmd, "r"));
  80. Log::channel('queue_daily')->info("单进程守卫:未检测到队列[$queueName],已尝试拉起。");
  81. } else {
  82. Log::channel('queue_daily')->info('单进程守卫:队列正在运行中,无需拉起。', ['pid' => trim($output)]);
  83. }
  84. })->everyMinute();
  85. }
  86. $schedule->call(function () {
  87. Log::channel('u8_daily')->info('测试环境:正在跳过物料同步,直接执行单据同步...');
  88. // 直接调用单据同步命令
  89. \Illuminate\Support\Facades\Artisan::call('command:u8_settle');
  90. })->everyMinute();
  91. }
  92. /**
  93. * Register the commands for the application.
  94. *
  95. * @return void
  96. */
  97. protected function commands()
  98. {
  99. $this->load(__DIR__.'/Commands');
  100. require base_path('routes/console.php');
  101. }
  102. }