1313
1414abstract 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