I want to make an automatic refund function? For example, if no one responds to his content within three days after payment, he will automatically enter the refund process! But how to write this timed task? Should I run the PHP script every second or write a scheduled task in mysql?
There are several options, each with pros and cons, see which one is more suitable for you:
The relevant table needs to set the order time field and expiration time field.
The first type: crontab scheduled task, execute a php script to scan the table, and if the expiration time minus the order time is more than three days, it will be changed to refund status.
Second method: Use mysql's scheduled tasks. The following is a demo. The specific logic can be written according to your own.
The third method: Use Redis to save, and the expiration time is 3 days when saving.
The first option
Advantages: Simple, the code logic does not change much, just use it after writing it
Disadvantages: The timeliness is not very good, because you must run the crontab task at a fixed time
The second option
Advantages: Direct Mysql is done, no other links are needed
Disadvantages: Similar to the advantages of crontab, and if you are not familiar with mysql scheduled tasks, you still need to familiarize yourself with it
The third option
Advantages: easy to use, good order timeliness, and you don’t have to go to mysql once io
Disadvantages: The logic layer code may need to add redis logic, and the persistence strategy, HA, and master-slave must be done well, otherwise a downtime will be tragic.
Use Linux crontab scheduled tasks to run a fixed php script every once in a while~
Write a scheduled task and execute the php script every second.
By delaying the message queue, register the writing task of the message queue after placing the order, and trigger it three days later; then write an interface to simply judge whether to perform a refund operation
Are refunds not subject to review? He also ran the script directly and broke the bank!