duanlv5084 2016-11-19 07:19
浏览 40
已采纳

是否可以将PHP函数设置为在一天内执行?

Due to terrible management I'm currently building an ecommerce from scratch. I got to the point where I need to do some lightweight stock management, and I figured that after the user sets their intention to buy something (proceed to check-out from cart), I should diminish the stock amount and create an order in the DB with pending status. However, I want to check if, after one day of this order being created, the status is still pending. If it is, then I should return the stock.

This would be easy if PHP had a function like JavaScript's setTimeout(), but I'm finding it difficult to find similar functionality. I thought about creating a cron job, but I don't know how I could make it so that the executed script got all the necessary parameters to perform the status check (most importantly, the order ID).

Are there any other options?

  • 写回答

1条回答 默认 最新

  • douzhe2981 2016-11-19 08:18
    关注

    As you know, PHP is interpreted language, so once started execution, it will run until the program is ended or the maximum time limit ( As per your PHP settings ) is reached. So in a single script it wont be easy.

    There are two options

    1. Put maximum execution time limit 0 (ie. no time limit) in your config and do a PHP script which runs an infinite loop and check your database and calling your status checking and status change functions. But this will be resource intensive as it is an infinite loop and having 1000s of orders will create crashes .
    2. Use cronjobs -> Using cronjobs, you can execute a script file in a fixed interval of time. You have to create two cron job scripts.

      1. A script that checks the status of each order and push those with pending status to an intermediate table which stores id of those with pending status. That means

         foreach($orders as $order) {
            if($order->status=='pending' && $order->date<CURRENT_DATE)
                push_order_to_cancellable_orders_table();
         } 
        
    3. A script that cancels the cancellable orders and remove those from cancellable orders table.ie.

      $cancellable_orders = get_items_from_cancellable_orders_table();
      foreach($cancellable_orders as $order) {
          cancel_order($order);
          remove_from_cancellable_orders($order);
      }
      

    Run each of this scripts in two separate cron jobs and set a small interval such as 1 minute for the crons.
    Again, the two actions in the second script can be combined in to single action in a MYSQL stored procedure and you can call that procedure, which will be more fast.

    But still , the more efficient and faster way will be by using MYSQL events and procedures.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作