|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +namespace Module\Vendor\Command; |
| 5 | + |
| 6 | +use Illuminate\Console\Command; |
| 7 | +use Illuminate\Support\Facades\Log; |
| 8 | +use Illuminate\Support\Str; |
| 9 | +use ModStart\Core\Util\ArrayUtil; |
| 10 | +use ModStart\Core\Util\FileUtil; |
| 11 | +use ModStart\Core\Util\ShellUtil; |
| 12 | +use ModStart\Core\Util\TimeUtil; |
| 13 | +use ModStart\ModStart; |
| 14 | +use Module\Vendor\Util\CacheUtil; |
| 15 | + |
| 16 | +class ScheduleRunAllCommand extends Command |
| 17 | +{ |
| 18 | + protected $signature = 'modstart:schedule-run-all {php} {dir}'; |
| 19 | + |
| 20 | + public function handle() |
| 21 | + { |
| 22 | + $php = $this->argument('php'); |
| 23 | + $dir = $this->argument('dir'); |
| 24 | + $hash = md5($php . ':' . $dir); |
| 25 | + $projects = CacheUtil::remember("Vendor:ScheduleRunAll:Projects:" . $hash, |
| 26 | + 3600, |
| 27 | + function () use ($dir) { |
| 28 | + $projects = FileUtil::listFiles($dir); |
| 29 | + $projects = array_filter($projects, function ($p) { |
| 30 | + return $p['isDir'] |
| 31 | + && @file_exists($p['pathname'] . '/artisan') |
| 32 | + && @file_exists($p['pathname'] . '/.env') |
| 33 | + && !Str::startsWith($p['filename'], '_delete.') |
| 34 | + && @file_exists($p['pathname'] . '/vendor/modstart/modstart-' . ModStart::env()); |
| 35 | + }); |
| 36 | + shuffle($projects); |
| 37 | + return ArrayUtil::keepItemsKeys($projects, ['pathname']); |
| 38 | + }); |
| 39 | + foreach ($projects as $project) { |
| 40 | + $start = TimeUtil::millitime(); |
| 41 | + $command = "$php {$project['pathname']}/artisan schedule:run"; |
| 42 | + Log::info("Vendor.ScheduleRunAllCommand.Run - {$command}"); |
| 43 | + $result = ShellUtil::run($command, false); |
| 44 | + $result = str_replace([ |
| 45 | + "\r" |
| 46 | + ], "", $result); |
| 47 | + $result = str_replace("\n", " ", $result); |
| 48 | + $result = str_replace("Running scheduled command: Closure", "√", $result); |
| 49 | + $result = str_replace("No scheduled commands are ready to run.", "〇", $result); |
| 50 | + $ms = TimeUtil::millitime() - $start; |
| 51 | + Log::info("Vendor.ScheduleRunAllCommand.Result - {$result} - {$ms}ms"); |
| 52 | + } |
| 53 | + } |
| 54 | +} |
0 commit comments