@@ -39,7 +39,7 @@ public static function getInstance(): self
39
39
40
40
return self ::$ instance ;
41
41
}
42
-
42
+
43
43
public static function stop (): void
44
44
{
45
45
if (isset (self ::$ instance ) && isset (self ::$ instance ->receiver )) {
@@ -148,6 +148,18 @@ public function delete(int $id): bool
148
148
return true ;
149
149
}
150
150
151
+ public function tick (bool $ has_registered = true ): void
152
+ {
153
+ if ($ has_registered ) {
154
+ $ this ->tick_heap ->extract ();
155
+ }
156
+
157
+ // 如果队列正在执行状态,是无需再注册Tick的。因为当队列遇到一个时间未到的Task后会自动注册Tick
158
+ if ($ this ->receive_flag ) {
159
+ $ this ->receiver ->push (true );
160
+ }
161
+ }
162
+
151
163
protected function compare ($ task_id_1 , $ task_id_2 )
152
164
{
153
165
$ time_offset = $ this ->time_map [$ task_id_2 ] - $ this ->time_map [$ task_id_1 ];
@@ -172,24 +184,12 @@ private function registerTick(int $after_ms): bool
172
184
}
173
185
}
174
186
$ this ->tick_heap ->insert ($ tick_timestamp_ms );
175
- $ func = function () use ($ after_ms ) {
187
+ $ self = $ this ;
188
+ \Swoole \Coroutine::create (function () use ($ after_ms , $ self ) {
176
189
\Swoole \Coroutine::sleep (round ($ after_ms / 1000 + 0.0005 , 3 ));
177
- $ this ->tick ();
178
- };
179
- \Swoole \Coroutine::create ($ func ->bindTo ($ this ));
190
+ $ self ->tick ();
191
+ });
180
192
181
193
return true ;
182
194
}
183
-
184
- private function tick (bool $ has_registered = true ): void
185
- {
186
- if ($ has_registered ) {
187
- $ this ->tick_heap ->extract ();
188
- }
189
-
190
- // 如果队列正在执行状态,是无需再注册Tick的。因为当队列遇到一个时间未到的Task后会自动注册Tick
191
- if ($ this ->receive_flag ) {
192
- $ this ->receiver ->push (true );
193
- }
194
- }
195
195
}
0 commit comments