Jump to content

Execution time = mismatch


Jonob

Recommended Posts

I am running a php script that:

-queries a local database to retrieve an amount

-executes a curl statement to update an external database with the amount + x

-queries the local database again to insert a new row

 

One of the problems that I am having is that if I have two different users from the same company running the same script at the same time, the execution time of the curl command (2-4 seconds), can cause a mismatch in what should be updated in the external database. This is the because the curl statement has not yet returned from the first user...so the second user is working off incorrect figures.

 

I am not sure of the best options here.

 

I thought of creating a field in the company table that records when a curl statement is being executed for that company - this flag is to prevent any simultaneous curl statements being executed until a response is received. The flag is automatically reset when a response from curl is received.

 

If another user tries to execute the script then I could check to see if the flag is locked or not. If it is locked, then I could either:

-Loop the the code and sleep for (5) seconds, and then check again if the flag has been reset. If after (3) loops, then reset the flag automatically (i've never seen the curl take longer than 5 seconds) and continue processing; OR

-Just notify the second user that they should wait for (5) seconds before trying again.

 

Are there any other (more elegant) ways of approaching this?

Link to comment
https://forums.phpfreaks.com/topic/203506-execution-time-mismatch/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.