Skip to content

Commit

Permalink
Fix "no constants" during shutdown functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Vectorial1024 committed Dec 30, 2024
1 parent 2d31c12 commit f4513c5
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/AsyncTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ class AsyncTask
*/
private int|null $timeLimit = 30;

/**
* The value of constant("LARAVEL_START") for future usage. Apparently, constants are not available during shutdown functions.
* @var float|null
*/
private float|null $laravelStartVal = null;

/**
* Indicates whether GNU coreutils is found in the system; in particular, we are looking for the timeout command inside coreutils.
*
Expand Down Expand Up @@ -76,6 +82,9 @@ public function __construct(Closure|AsyncTaskInterface $theTask)
public function run(): void
{
// todo startup configs
// write down the LARAVEL_START constant value for future usage
$this->laravelStartVal = constant("LARAVEL_START");

// install a timeout detector
// this single function checks all kinds of timeouts
register_shutdown_function([$this, 'checkTaskTimeout']);
Expand Down Expand Up @@ -248,7 +257,7 @@ protected function checkTaskTimeout(): void

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

0 comments on commit f4513c5

Please sign in to comment.