From 5d09e500305370a9b628d57eb166321af5b8c0df Mon Sep 17 00:00:00 2001 From: Vincent Wong Date: Sun, 5 Jan 2025 21:34:17 +0800 Subject: [PATCH] Allow specifying task IDs Also fixes a vartype error --- src/AsyncTask.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/AsyncTask.php b/src/AsyncTask.php index fcdc539..aa1e4f7 100644 --- a/src/AsyncTask.php +++ b/src/AsyncTask.php @@ -24,6 +24,14 @@ class AsyncTask */ private SerializableClosure|AsyncTaskInterface $theTask; + /** + * The user-specified ID of the current task. (Null means user did not specify any ID). + * + * If null, the task will generate an unsaved random ID when it is started. + * @var string|null + */ + private string|null $taskID; + /** * The process that is actually running this task. Tasks that are not started will have null here. * @var InvokedProcess|null @@ -92,14 +100,16 @@ class AsyncTask /** * Creates an AsyncTask instance. * @param Closure|AsyncTaskInterface $theTask The task to be executed in the background. + * @param string|null $taskID (optional) The user-specified task ID of this AsyncTask. Should be unique. */ - public function __construct(Closure|AsyncTaskInterface $theTask) + public function __construct(Closure|AsyncTaskInterface $theTask, string|null $taskID = null) { if ($theTask instanceof Closure) { // convert to serializable closure first $theTask = new SerializableClosure($theTask); } $this->theTask = $theTask; + $this->taskID = $taskID; } /** @@ -165,8 +175,7 @@ public function run(): void public function start(): AsyncTaskStatus { // prepare the task details - // todo allow changing the task ID - $taskID = null ?? Str::ulid(); + $taskID = $this->taskID ?? Str::ulid()->toString(); $taskStatus = new AsyncTaskStatus($taskID); // prepare the runner command