13
13
14
14
abstract class Task
15
15
{
16
- protected static $ taskid_map = [];
17
16
protected static $ finish_func ;
18
17
protected $ task_callback ;
19
18
protected $ params ;
@@ -34,12 +33,7 @@ public function __construct(callable $task_callback, ?array $params = null)
34
33
*/
35
34
public static function getCurrentTaskId (): ?int
36
35
{
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 ;
43
37
}
44
38
45
39
/**
@@ -68,8 +62,8 @@ protected function callFunc(&$ret, bool $clear_after_finish = true): ?\Throwable
68
62
}
69
63
70
64
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 ;
73
67
$ ret = ($ this ->task_callback )(...$ this ->params );
74
68
$ return = null ;
75
69
if (isset (self ::$ finish_func )) {
@@ -81,7 +75,7 @@ protected function callFunc(&$ret, bool $clear_after_finish = true): ?\Throwable
81
75
(self ::$ finish_func )($ this ->id , null , $ e );
82
76
}
83
77
}
84
- unset( self :: $ taskid_map [ $ uid ]) ;
78
+ $ context -> swlib_archer_taskid = null ;
85
79
if ($ clear_after_finish ) {
86
80
$ this ->task_callback = null ;
87
81
$ this ->params = null ;
0 commit comments