@@ -24,6 +24,14 @@ class AsyncTask
2424 */
2525 private SerializableClosure |AsyncTaskInterface $ theTask ;
2626
27+ /**
28+ * The user-specified ID of the current task. (Null means user did not specify any ID).
29+ *
30+ * If null, the task will generate an unsaved random ID when it is started.
31+ * @var string|null
32+ */
33+ private string |null $ taskID ;
34+
2735 /**
2836 * The process that is actually running this task. Tasks that are not started will have null here.
2937 * @var InvokedProcess|null
@@ -92,14 +100,16 @@ class AsyncTask
92100 /**
93101 * Creates an AsyncTask instance.
94102 * @param Closure|AsyncTaskInterface $theTask The task to be executed in the background.
103+ * @param string|null $taskID (optional) The user-specified task ID of this AsyncTask. Should be unique.
95104 */
96- public function __construct (Closure |AsyncTaskInterface $ theTask )
105+ public function __construct (Closure |AsyncTaskInterface $ theTask, string | null $ taskID = null )
97106 {
98107 if ($ theTask instanceof Closure) {
99108 // convert to serializable closure first
100109 $ theTask = new SerializableClosure ($ theTask );
101110 }
102111 $ this ->theTask = $ theTask ;
112+ $ this ->taskID = $ taskID ;
103113 }
104114
105115 /**
@@ -165,8 +175,7 @@ public function run(): void
165175 public function start (): AsyncTaskStatus
166176 {
167177 // prepare the task details
168- // todo allow changing the task ID
169- $ taskID = null ?? Str::ulid ();
178+ $ taskID = $ this ->taskID ?? Str::ulid ()->toString ();
170179 $ taskStatus = new AsyncTaskStatus ($ taskID );
171180
172181 // prepare the runner command
0 commit comments