Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Queue::later不好用 没有延迟执行而是立即执行了 #195

Open
zhaoxin987 opened this issue Feb 24, 2025 · 2 comments
Open

Queue::later不好用 没有延迟执行而是立即执行了 #195

zhaoxin987 opened this issue Feb 24, 2025 · 2 comments

Comments

@zhaoxin987
Copy link

zhaoxin987 commented Feb 24, 2025

控制器
public function queuelater($orderid)
{
echo date("Y-m-d H:i:s");
$queue=Queue::later(60, 'app\common\job\TimeoutOrder', ['orderid' => $orderid],$queue = 'OrderSure');
if($queue){
echo '异步执行成功';
}else{
echo '异步执行失败';
}
}

任务方法

class TimeoutOrder
{
    // 任务执行的逻辑
    public function fire(Job $job, $data)
    {
       
        $orderid = $data['orderid'];
      
        $order = Db::table('re_reserve_order')->where('id', $orderid)->find();
        if ($order) {
            Db::table('re_reserve_order')
              ->where('id',$orderid)
              ->update(['is_status' => 5,'agree'=>0]); // 更改订单状态为已确认
        } 
        // 删除任务标记,表示任务已完成
        $job->delete();
    }
}

配置文件
return [
'default' => 'redis',
'connections' => [
'sync' => [
'driver' => 'sync',
],
'database' => [
'driver' => 'database',
'queue' => 'default',
'table' => 'jobs',
],
'redis' => [
'driver' => 'redis',
'queue' => 'default',
'host' => '127.0.0.1',
'port' => 6379,
'password' => '',
'select' => 1,
'timeout' => 0,
'persistent' => false,
'type'=>'redis',
],
],
'failed' => [
'type' => 'none',
'table' => 'failed_jobs',
],
];
每次执行 都是立即执行了,没有异步等待60秒执行 这是什么原因

@zhaoxin987
Copy link
Author

任务方法

public function fire(Job $job, $data)
// public function push(Job $job, $data)
{

    // 获取订单ID
    $orderid = $data['orderid'];
    // sleep(60);
    
    $order = Db::table('re_reserve_order')->where('id', $orderid)->find();
    if ($order) {
        Db::table('re_reserve_order')
          ->where('id',$orderid)
          ->update(['is_status' => 5,'agree'=>0]); // 更改订单状态为已确认
    }
    
    if ($job->attempts() > 3) {
              //通过这个方法可以检查这个任务已经重试了几次了
              echo 'AAA';
    }
    
    // 删除任务标记,表示任务已完成
    $job->delete();
}

@zhaoxin987
Copy link
Author

配置文件
return [ 'default' => 'redis', 'connections' => [ 'sync' => [ 'driver' => 'sync', ], 'database' => [ 'driver' => 'database', 'queue' => 'default', 'table' => 'jobs', ], 'redis' => [ 'driver' => 'redis', 'queue' => 'default', 'host' => '127.0.0.1', 'port' => 6379, 'password' => '', 'select' => 1, 'timeout' => 0, 'persistent' => false, 'type'=>'redis', ], ], 'failed' => [ 'type' => 'none', 'table' => 'failed_jobs', ], ];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant