Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support colored output from queue:listen #52223

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/Illuminate/Queue/Console/WorkCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function handle()
// connection being run for the queue operation currently being executed.
$queue = $this->getQueue($connection);

if (Terminal::hasSttyAvailable()) {
if (Terminal::hasSttyAvailable() && ! self::isChildProcess()) {
$this->components->info(
sprintf('Processing jobs from the [%s] %s.', $queue, str('queue')->plural(explode(',', $queue)))
);
Expand Down Expand Up @@ -303,4 +303,30 @@ protected function downForMaintenance()
{
return $this->option('force') ? false : $this->laravel->isDownForMaintenance();
}

/**
* Determine if the the current process was created by php.
* We cannot use posix_getppid since symfony spawns the process with proc_open
* making posix_getppid return the pid of the shell instead of the parent process.
*/
private static function isChildProcess()
{
if (PHP_OS_FAMILY === 'Windows') {
$pid = getmypid();
$parentPid = shell_exec("wmic process where (processid=$pid) get parentprocessid");
$parentPid = explode("\n", $parentPid);
$parentPid = intval($parentPid[1]);
$parentProcessName = shell_exec("wmic process where (processid=$parentPid) get name");
$parentProcessName = explode("\n", $parentProcessName);
$parentProcessName = trim($parentProcessName[1]);
} else {
$pid = getmypid();
$parentPid = shell_exec("ps -o ppid= -p $pid");
$parentPid = intval(trim($parentPid));
$parentProcessName = shell_exec("ps -o comm= -p $parentPid");
$parentProcessName = trim($parentProcessName);
}

return $parentProcessName === 'php';
}
}
11 changes: 10 additions & 1 deletion src/Illuminate/Queue/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,22 @@ public function makeProcess($connection, $queue, ListenerOptions $options)
$command = $this->addEnvironment($command, $options);
}

return new Process(
$process = new Process(
$command,
$this->commandPath,
null,
null,
$options->timeout
);

// Enable TTY mode to propagate colored output if supported by the terminal
try {
$process->setTty(true);
} catch (\Throwable $th) {
// TTY mode might not be supported; handle any exceptions silently
}

return $process;
}

/**
Expand Down