Skip to content

Commit

Permalink
Introduce isRunning()
Browse files Browse the repository at this point in the history
  • Loading branch information
Vectorial1024 committed Dec 6, 2024
1 parent b910a6f commit 21c8e1d
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/AsyncTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Vectorial1024\LaravelProcessAsync;

use Closure;
use Illuminate\Process\InvokedProcess;
use Illuminate\Support\Facades\Process;
use Laravel\SerializableClosure\SerializableClosure;

Expand All @@ -17,6 +18,12 @@ class AsyncTask
*/
private SerializableClosure|AsyncTaskInterface $theTask;

/**
* The process that is actually running this task. Tasks that are not started will have null here.
* @var InvokedProcess|null
*/
private InvokedProcess|null $runnerProcess = null;

/**
* Creates an AsyncTask instance.
* @param Closure|AsyncTaskInterface $theTask The task to be executed in the background.
Expand Down Expand Up @@ -63,7 +70,7 @@ public function start(): void
{
// assume unix for now
$serializedTask = $this->toBase64Serial();
Process::quietly()->start("php artisan async:run $serializedTask");
$this->theTask = Process::quietly()->start("php artisan async:run $serializedTask");
}

/**
Expand Down Expand Up @@ -107,4 +114,13 @@ public static function fromBase64Serial(string $serial): ?static
return null;
}
}

/**
* Returns whether this task is currently running in the background.
* @return bool
*/
public function isRunning(): bool
{
return $this->runnerProcess?->running() ?? false;
}
}

0 comments on commit 21c8e1d

Please sign in to comment.