swoole_timer_tick 间隔的时钟控制器 | |
swoole_timer_after 指定的时间后执行 | |
swoole_timer_clear 删除定时器 | |
//每隔3000ms触发一次 | |
$timer_id = swoole_timer_tick(3000, function () { | |
echo "tick 3000ms - ".date('Y-m-d H:i:s')."\n"; | |
}); | |
//9000ms后删除定时器 | |
swoole_timer_after(9000, function () use ($timer_id) { | |
echo "after 9000ms - ".date('Y-m-d H:i:s')."\n"; | |
swoole_timer_clear($timer_id); | |
}); | |
每五分钟执行一次,到了第五次就删除定时器 | |
$api_url = 'xxx'; //接口地址 | |
$exec_num = 0; //执行次数 | |
swoole_timer_tick(5*60*1000, function($timer_id) use ($api_url, &$exec_num) { | |
$exec_num ++ ; | |
$result = $this->requestUrl($api_url); | |
echo date('Y-m-d H:i:s'). " 执行任务中...(".$exec_num.")\n"; | |
if ($result) { | |
//业务代码... | |
swoole_timer_clear($timer_id); // 停止定时器 | |
echo date('Y-m-d H:i:s'). " 第(".$exec_num.")次请求接口任务执行成功\n"; | |
} else { | |
if ($exec_num >= 5) { | |
swoole_timer_clear($timer_id); // 停止定时器 | |
echo date('Y-m-d H:i:s'). " 请求接口失败,已失败5次,停止执行\n"; | |
} else { | |
echo date('Y-m-d H:i:s'). " 请求接口失败,5分钟后再次尝试\n"; | |
} | |
} | |
}); |
swoole-timer操作
PHP技术
508
0
0
2022-11-01
登录后可点赞和收藏
标签
Swoole
登录后可点赞和收藏