Home  >  Article  >  Backend Development  >  How is this logic implemented to modify the order status after counting down n hours?

How is this logic implemented to modify the order status after counting down n hours?

WBOY
WBOYOriginal
2016-07-06 13:51:441048browse

Set up a scheduled execution program on the server?
Or should I modify it after the time has expired every time I visit this page?

Reply content:

Set up a scheduled execution program on the server?
Or should I modify it after the time has expired every time I visit this page?

I don’t think it is necessary to update in real time. For problems like this, I think a better way is to record an expiration time when creating an order. Then when reading the order, you only need to judge the timestamp to achieve verification. Whether it has expired and whether the current state can continue to operate, scheduled tasks and the like are not a good strategy. Let’s not talk about the efficiency of using PHP to interact with the system level, but the risk of program anomalies in the middle is not It’s very controllable. In addition, when the order volume is large, the load on the server is also very high

According to your logic, you should use a scheduled script. Otherwise, changing it every time you visit will not be real-time, and the efficiency will be low.

You can use the scheduled tasks of the Linux system, so you don’t need to loop

If you don’t use websocket on the browser side, you can just handle whether it expires when you access it;
If you use websocket, then run it honestly with cron~
Depend on the specific business scenario Requested

Are you talking about a function similar to canceling an order if there is no payment within 15 minutes? I haven't done it, so I can only give my thoughts. When generating an order, PHP calls the system function to create a task that will be executed in 15 minutes. The task statement is dynamically generated, for example, php -f checkOrderStatus.php -id 323003
checkOrderStatus.php. The system function is also used to delete this scheduled task. (If there is a one-time scheduled task, don’t bother. I actually don’t understand Linux, so I only search for information when I use it).
As for the load issue mentioned by @accyl, don’t worry too much, because this function does not use web services, so executing this file must be much less expensive than creating an order. If the order can handle this, it will be no problem.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn