1515 */
1616class AsyncTaskStatus
1717{
18+ private const MSG_CANNOT_CHECK_STATUS = "Could not check the status of the AsyncTask. " ;
19+
1820 /**
1921 * The cached task ID for quick ID reusing. We will most probably reuse this ID many times.
2022 * @var string|null
@@ -114,7 +116,7 @@ private function findTaskRunnerProcess(): bool
114116 $ tmpEncoded = base64_encode ($ tmpPsCmd );
115117 $ status = exec ("powershell -EncodedCommand $ tmpEncoded " , $ results );
116118 if (!$ status ) {
117- throw new RuntimeException (" Could not query whether the AsyncTask is still running. " );
119+ throw new RuntimeException (self :: MSG_CANNOT_CHECK_STATUS );
118120 }
119121 // the way it works, it prints out many lines, and some of them contain the PID that we are interested in
120122 $ expectedCmdName = "artisan async:run " ;
@@ -129,7 +131,7 @@ private function findTaskRunnerProcess(): bool
129131 $ innerResults = [];
130132 $ status = exec ("powershell -Command $ tmpPsCmd " , $ innerResults );
131133 if (!$ status ) {
132- throw new RuntimeException (" Could not query whether the AsyncTask is still running. " );
134+ throw new RuntimeException (self :: MSG_CANNOT_CHECK_STATUS );
133135 }
134136 foreach ($ innerResults as $ cmdArgs ) {
135137 if ($ cmdArgs == "" ) {
@@ -146,7 +148,7 @@ private function findTaskRunnerProcess(): bool
146148 $ innerResults = [];
147149 exec ("powershell -Command $ tmpPsCmd " , $ innerResults );
148150 if (!$ status ) {
149- throw new RuntimeException (" Could not query whether the AsyncTask is still running. " );
151+ throw new RuntimeException (self :: MSG_CANNOT_CHECK_STATUS );
150152 }
151153 foreach ($ innerResults as $ executableName ) {
152154 if ($ executableName == "" ) {
@@ -177,15 +179,15 @@ private function findTaskRunnerProcess(): bool
177179 // then use ps to see what really is it
178180 $ fullCmd = exec ("ps -p $ candidatePID -o args= " );
179181 if ($ fullCmd === false ) {
180- throw new RuntimeException (" Could not query whether the AsyncTask is still running. " );
182+ throw new RuntimeException (self :: MSG_CANNOT_CHECK_STATUS );
181183 }
182184 if (!str_contains ($ fullCmd , $ expectedCmdName )) {
183185 // not really
184186 continue ;
185187 }
186188 $ executable = exec ("ps -p $ candidatePID -o comm= " );
187189 if ($ executable === false ) {
188- throw new RuntimeException (" Could not query whether the AsyncTask is still running. " );
190+ throw new RuntimeException (self :: MSG_CANNOT_CHECK_STATUS );
189191 }
190192 if ($ executable !== "php " ) {
191193 // not really
@@ -213,7 +215,7 @@ private function observeTaskRunnerProcess(): bool
213215 $ tmpPsCmd = "Get-CimInstance Win32_Process -Filter \"CommandLine LIKE '%id=\' {$ this ->lastKnownPID }\'%' \" | Select ProcessId | Format-List " ;
214216 $ status = exec ("powershell -Command $ tmpPsCmd " , $ results );
215217 if (!$ status ) {
216- throw new RuntimeException (" Could not query whether the AsyncTask is still running. " );
218+ throw new RuntimeException (self :: MSG_CANNOT_CHECK_STATUS );
217219 }
218220 // extract the PID
219221 $ echoedPid = null ;
@@ -228,7 +230,7 @@ private function observeTaskRunnerProcess(): bool
228230 // assume anything not Windows to be Unix
229231 $ echoedPid = exec ("ps -p {$ this ->lastKnownPID } -o pid= " );
230232 if ($ echoedPid === false ) {
231- throw new RuntimeException (" Could not query whether the AsyncTask is still running. " );
233+ throw new RuntimeException (self :: MSG_CANNOT_CHECK_STATUS );
232234 }
233235 $ echoedPid = (int ) $ echoedPid ;
234236 return $ this ->lastKnownPID === $ echoedPid ;
0 commit comments