作者 邓超

1

... ... @@ -17,9 +17,12 @@ function start(){
break;
}
$lists = db()->all(\Model\sendJobsSql::sendList());
$lists = db()->all(\Model\sendJobsSql::sendList(500-$cNum));
// 循环
foreach ($lists as $list){
// 占用 id
if(redis()->add('send_job_run_id_'.$list['id'],$list['id'])){
go(function ($data) use (&$cNum){
$cNum++; // 协程数+1
// 表单数据
... ... @@ -87,15 +90,20 @@ function start(){
}
// 协程结束后
co::defer(function () use(&$cNum){
co::defer(function ($id) use(&$cNum,$data){
$cNum--;
// 结束后要关闭数据库链接,不然链接一直暂用
db()->close();
// 删除占用
redis()->delete('send_job_run_id_'.$data['id']);
redis()->close();
});
},$list);
}
}
}catch (Throwable $e){
logs($e->getMessage().$e->getTraceAsString());
}
... ...
... ... @@ -24,9 +24,9 @@ class sendJobsSql {
* @time 2023/4/11 14:56
* @return string
*/
public static function sendList():string {
public static function sendList($limit):string {
// 控制在500数量,协程数量就控制
return "select * from `".self::$table."` where `status` = 0 and `send_time` <= ".time()." limit 500";
return "select * from `".self::$table."` where `status` = 0 and `send_time` <= ".time()." limit {$limit}";
}
... ...