运行 for 循环函数内部 Laravel Observable

Running for loop function inside Laravel Observable

我有一个包含 orderStatuspaymentStatus 字段的订单 table。下订单时,orderStatus 设置为 initializedpaymentStatus 设置为 pending

在创建订单时,我想检查 paymentStatus 是否已更改。如果没有,请在 12 分钟后更改 我想将 orderStatus 更新为 completed 和 'paymentStatustoaborted`。

我有一个每分钟检查一次的计划任务,但不幸的是我无法在 Bluehost 上 运行 cron 作业。所以我尝试在 OrderObservercreate 方法中使用 for 循环,但代码不起作用。

public function created(Order $order)
{
    // check if user reservation record exist
    $reservation = Reservation::where([
        ['user_id', $order->user_id],
        ['product_id', $order->product_id]
    ]);

    if ($reservation) {

        // delete reservation record
        $reservation->delete();
    }

    // start 12 mins count down for payment
    $period = ($order->created_at)->diffInMinutes();

    for ($counter = 0; $period >= 12; ++$counter) {
        $order->update([
            'orderStatus'   => 'completed',
            'paymentStatus' => 'aborted'
        ]);
    }
}

来自 php artisan tinker,我可以看到这部分代码有效

for ($counter = 0; $period >= 12; ++$counter) {
    $order->update([
        'orderStatus'   => 'completed',
        'paymentStatus' => 'aborted'
    ]);
}

为什么代码不在 observable 中 运行?

这可能与您阻止 php 执行 12 分钟这一事实有关,方法是使其卡在同一个 for 循环中。你可能超过了 max executing time.

max_execution_time integer

This sets the maximum time in seconds a script is allowed to run before it is terminated by the parser. This helps prevent poorly written scripts from tying up the server. The default setting is 30. When running PHP from the command line the default setting is 0.

看到 artisan tinker 从命令行运行,它在那里工作是有道理的。