douzhi1924 2009-01-06 13:07
浏览 83

使用API​​ /函数调用或执行Cron Job方法 - 查询处理事务 - 您将如何处理它?

I wanted to run something by you guys. I am starting a new project which is roughly the following:

Important information: I am using PHP and MySQL

Every minute I get a list of to-do transactions from an API from different users.

Example:

user1 send $1 to user2
userx send $2 to usera
userw send $0.50 to user2

etc..

Lets say user1 wants to send $1 to user2. There are two posibilities, it's succesful or it's unsuccessful because there are insufficient funds or the user spelled the username wrong. If it's unsuccesfull I send out a message to the user.

I am now facing several options - please bare with me through my thought process.

Option 1

Create a database table with transactions that need to be processed and use a cronjob that processes them every minute. The risk here is that the script could run against an error or a timeout and the other transactions would still show In Progress in the database table. So I would need a second script to check that against a timestamp.

Option 2

Create an API or Function which gets called for each transaction after I receive them and brings me a response. From which then I can call another API or Function to deal with that response or move on to the next transaction. However I would still have to put them in a database table since I can't risk losing them if the script stops executing. So it would work as follow: put all transactions in database table - start transaction - when finished transaction delete from table - start transaction 2.


Both options are flawed because you don't know how long the list of transactions will be. If it's long PHP is definitely not optimal to run for a long time - using set timeout to zero is risky. I am looking to create a solution that will scale with PHP. So I was thinking about an Option 3.


Option 3 (Optimal solution?)

Use an API to return 10 transactions.

In database set a flag to say they are sent to a script and timestamp to say when they were sent

PHP script retreives 10 transactions from API - handles 10 transactions.

Once transaction completed - delete it from this table and copy it to completed transaction table.

Have a cronjob script check every 3 minutes if timesent is greater then X minutes (based on an upper bound of the execution time of 10 transactions). If it is greater - set them to not sent - so they can be sent out again.


As you can see I wrote down my whole thought process on this and am looking for input. There's bound to be stuff that I missed. Also please realize that these are not real financial transactions - it's just the best metafore I could use to make it clear.

Thank you very much,

Ice

  • 写回答

2条回答 默认 最新

  • douxiajia6309 2009-01-06 13:12
    关注

    I would suggest processing each transaction when the request for that transaction is received.

    I.E Bob clicks a 'Send money' button to send $20 to Alice. I would call a function to handle this transaction, and show them a 'Transaction successful' or 'There was a problem..' message right there.

    If a transaction processing will take long, you could have it done via AJAX, showing them a nice progress bar saying 'Processing..' while the processing is being done on the server side, and then either redirect them to yoursite.com/transactions.php?result=success&id=$id or yoursite.com/transactions.php?result=failure&id=$id

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100