Skip to content

Commit

Permalink
Allow specifying task IDs
Browse files Browse the repository at this point in the history
Also fixes a vartype error
  • Loading branch information
Vectorial1024 committed Jan 5, 2025
1 parent 7dfda17 commit 5d09e50
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/AsyncTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5d09e50

Please sign in to comment.