Skip to content

Commit 8d2c6b2

Browse files
committed
use Swoole\Coroutine::getContext
1 parent 37bce35 commit 8d2c6b2

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

src/Task.php

+4-10
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
abstract class Task
1515
{
16-
protected static $taskid_map = [];
1716
protected static $finish_func;
1817
protected $task_callback;
1918
protected $params;
@@ -34,12 +33,7 @@ public function __construct(callable $task_callback, ?array $params = null)
3433
*/
3534
public static function getCurrentTaskId(): ?int
3635
{
37-
$uid = \Swoole\Coroutine::getuid();
38-
if (array_key_exists($uid, self::$taskid_map)) {
39-
return self::$taskid_map[$uid];
40-
}
41-
42-
return null;
36+
return \Swoole\Coroutine::getContext()->swlib_archer_taskid ?? null;
4337
}
4438

4539
/**
@@ -68,8 +62,8 @@ protected function callFunc(&$ret, bool $clear_after_finish = true): ?\Throwable
6862
}
6963

7064
try {
71-
$uid = \Swoole\Coroutine::getuid();
72-
self::$taskid_map[$uid] = $this->id;
65+
$context = \Swoole\Coroutine::getContext();
66+
$context->swlib_archer_taskid = $this->id;
7367
$ret = ($this->task_callback)(...$this->params);
7468
$return = null;
7569
if (isset(self::$finish_func)) {
@@ -81,7 +75,7 @@ protected function callFunc(&$ret, bool $clear_after_finish = true): ?\Throwable
8175
(self::$finish_func)($this->id, null, $e);
8276
}
8377
}
84-
unset(self::$taskid_map[$uid]);
78+
$context->swlib_archer_taskid = null;
8579
if ($clear_after_finish) {
8680
$this->task_callback = null;
8781
$this->params = null;

0 commit comments

Comments
 (0)