Skip to content

Commit b51c467

Browse files
committed
Simplify expressions
1 parent 9d32cf7 commit b51c467

File tree

1 file changed

+27
-57
lines changed

1 file changed

+27
-57
lines changed

src/AsyncTaskStatus.php

Lines changed: 27 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -104,62 +104,41 @@ private function findTaskRunnerProcess(): bool
104104
{
105105
// find the runner in the system
106106
// we might have multiple PIDs; in this case, pick the first one that appears
107+
/*
108+
* note: while the OS may allow reading multiple properties at the same time,
109+
* we won't risk it because localizations might produce unexpected strings or unusual separators
110+
* an example would be CJK potentially having an alternate character to replace ":"
111+
*/
107112
if (OsInfo::isWindows()) {
108113
// Windows uses GCIM to discover processes
109114
$results = [];
110115
$encodedTaskID = $this->getEncodedTaskID();
111-
// we don't know whether we are in cmd or powershell, so we do this
112-
// powershell allows inputting base64-encoded commands
113-
// note: while powershell gcim allows reading everything in the same run, we cannot risk this since localizations might mess with string splitting
114-
// things like CJK ":" will already break this.
115-
$tmpPsCmd = "Get-CimInstance Win32_Process -Filter \"CommandLine LIKE '%id=\'$encodedTaskID\'%'\" | Select ProcessId | Format-List";
116-
$tmpEncoded = base64_encode($tmpPsCmd);
117-
$status = exec("powershell -EncodedCommand $tmpEncoded", $results);
118-
if (!$status) {
119-
throw new RuntimeException(self::MSG_CANNOT_CHECK_STATUS);
120-
}
121-
// the way it works, it prints out many lines, and some of them contain the PID that we are interested in
122116
$expectedCmdName = "artisan async:run";
123-
foreach ($results as $possiblePid) {
124-
if ($possiblePid == "") {
125-
// blank line
126-
continue;
127-
}
128-
$candidatePID = (int) $possiblePid;
117+
// we can assume we are in cmd, but wcim in cmd is deprecated, and the replacement gcim requires powershell
118+
$results = [];
119+
$fullCmd = "powershell echo \"\"(gcim Win32_Process -Filter \\\"CommandLine LIKE '%id=\'$encodedTaskID\'%'\\\").ProcessId\"\"";
120+
\Illuminate\Support\Facades\Log::info($fullCmd);
121+
exec("powershell echo \"\"(gcim Win32_Process -Filter \\\"CommandLine LIKE '%id=\'$encodedTaskID\'%'\\\").ProcessId\"\"", $results);
122+
// will output many lines, each line being a PID
123+
foreach ($results as $candidatePID) {
124+
$candidatePID = (int) $candidatePID;
129125
// then use gcim again to see the cmd args
130-
$tmpPsCmd = "Get-CimInstance Win32_Process -Filter \"ProcessId = $candidatePID\" | Select CommandLine | Format-Wide";
131-
$innerResults = [];
132-
$status = exec("powershell -Command $tmpPsCmd", $innerResults);
133-
if (!$status) {
126+
$cmdArgs = exec("powershell echo \"\"(gcim Win32_Process -Filter \\\"ProcessId = $candidatePID\\\").CommandLine\"\"");
127+
if ($cmdArgs === false) {
134128
throw new RuntimeException(self::MSG_CANNOT_CHECK_STATUS);
135129
}
136-
foreach ($innerResults as $cmdArgs) {
137-
if ($cmdArgs == "") {
138-
// blank line
139-
continue;
140-
}
141-
if (!str_contains($cmdArgs, $expectedCmdName)) {
142-
// not this PID!
143-
continue 2;
144-
}
130+
if (!str_contains($cmdArgs, $expectedCmdName)) {
131+
// not really
132+
continue;
145133
}
146-
// then use gcim again to see the task runner
147-
$tmpPsCmd = "Get-CimInstance Win32_Process -Filter \"ProcessId = $candidatePID\" | Select ProcessName | Format-Wide";
148-
$innerResults = [];
149-
exec("powershell -Command $tmpPsCmd", $innerResults);
150-
if (!$status) {
134+
$executable = exec("powershell echo \"\"(gcim Win32_Process -Filter \\\"ProcessId = $candidatePID\\\").Name\"\"");
135+
if ($executable === false) {
151136
throw new RuntimeException(self::MSG_CANNOT_CHECK_STATUS);
152137
}
153-
foreach ($innerResults as $executableName) {
154-
if ($executableName == "") {
155-
// blank line
156-
continue;
157-
}
158-
if ($executableName != "php.exe") {
159-
// wrong process
160-
// note: we currently hard-code "php.exe" as the executable name
161-
continue 2;
162-
}
138+
if ($executable !== "php.exe") {
139+
// not really
140+
// note: we currently hard-code "php" as the executable name
141+
continue;
163142
}
164143
// all checks passed; it is this one
165144
$this->lastKnownPID = $candidatePID;
@@ -211,20 +190,11 @@ private function observeTaskRunnerProcess(): bool
211190
// supposedly, the PID has not rolled over yet, right...?
212191
if (OsInfo::isWindows()) {
213192
// Windows uses GCIM to discover processes
214-
$results = [];
215-
$tmpPsCmd = "Get-CimInstance Win32_Process -Filter \"CommandLine LIKE '%id=\'{$this->lastKnownPID}\'%'\" | Select ProcessId | Format-List";
216-
$status = exec("powershell -Command $tmpPsCmd", $results);
217-
if (!$status) {
193+
$echoedPid = exec("powershell echo \"\"(gcim Win32_Process -Filter \\\"ProcessId = {$this->lastKnownPID}\\\").ProcessId\"\"");
194+
if ($echoedPid === false) {
218195
throw new RuntimeException(self::MSG_CANNOT_CHECK_STATUS);
219196
}
220-
// extract the PID
221-
$echoedPid = null;
222-
foreach ($results as $possiblePid) {
223-
if ($possiblePid == "") {
224-
continue;
225-
}
226-
$echoedPid = (int) $possiblePid;
227-
}
197+
$echoedPid = (int) $echoedPid;
228198
return $this->lastKnownPID === $echoedPid;
229199
}
230200
// assume anything not Windows to be Unix

0 commit comments

Comments
 (0)