Skip to content

Commit f4513c5

Browse files
committed
Fix "no constants" during shutdown functions
1 parent 2d31c12 commit f4513c5

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/AsyncTask.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ class AsyncTask
3535
*/
3636
private int|null $timeLimit = 30;
3737

38+
/**
39+
* The value of constant("LARAVEL_START") for future usage. Apparently, constants are not available during shutdown functions.
40+
* @var float|null
41+
*/
42+
private float|null $laravelStartVal = null;
43+
3844
/**
3945
* Indicates whether GNU coreutils is found in the system; in particular, we are looking for the timeout command inside coreutils.
4046
*
@@ -76,6 +82,9 @@ public function __construct(Closure|AsyncTaskInterface $theTask)
7682
public function run(): void
7783
{
7884
// todo startup configs
85+
// write down the LARAVEL_START constant value for future usage
86+
$this->laravelStartVal = constant("LARAVEL_START");
87+
7988
// install a timeout detector
8089
// this single function checks all kinds of timeouts
8190
register_shutdown_function([$this, 'checkTaskTimeout']);
@@ -248,7 +257,7 @@ protected function checkTaskTimeout(): void
248257

249258
// external killing; could be normal Unix timeout SIG_TERM or manual Windows taskkill
250259
// Laravel Artisan very conveniently has a LARAVEL_START = microtime(true) to let us check time elapsed
251-
$timeElapsed = microtime(true) - constant("LARAVEL_START");
260+
$timeElapsed = microtime(true) - $this->laravelStartVal;
252261
if ($timeElapsed > $this->timeLimit) {
253262
// timeout!
254263
$hasTimedOut = true;

0 commit comments

Comments
 (0)