knowramphp Posted June 22, 2017 Share Posted June 22, 2017 I am using curl to work with a REST API which is working great to perform the calls however there are times that based on server load on the far end I get a response to my rest call asking to try again in 200+ seconds. simply putting a sleep() step in to wait doesn't seem like the best option as the over all script stops and then often times times out. is there a way to offload the retry to a different script and schedule it to run in x seconds with php? Quote Link to comment https://forums.phpfreaks.com/topic/304190-sleeping-a-rest-call/ Share on other sites More sharing options...
Jacques1 Posted June 22, 2017 Share Posted June 22, 2017 200 seconds? What kind of service is this? Sounds like you'll need a very different approach to overcome those extreme limitations. You should refrain from making straightforward HTTP requests. Instead, create a queue (which can be a simple database table) to hold the request data. Whenever you want to make a request, you append it to the queue. Then set up a cron job which regularly executes a PHP script, ideally when the remote service isn't too busy. This script picks the first entry from the queue and tries to make a request. If that succeeds, it processes the response, removes the queue entry and stops (or makes another request), otherwise it just stops and tries again next time. Quote Link to comment https://forums.phpfreaks.com/topic/304190-sleeping-a-rest-call/#findComment-1547647 Share on other sites More sharing options...
gizmola Posted June 24, 2017 Share Posted June 24, 2017 This seems like a chicken/egg problem. If your program worked reliably you could have it exit with a return value which could perhaps, if positive, be the number of seconds the program should wait before the next run. A simple shell script could then wrap your script in an infinite loop. Unix has a simple built in sleep() function. In your loop, using sh/bash you take the return value and if > 0, use that to sleep in the shell script. The problem with this and any other more involved solution is that it depends on your script not locking up. Unless I misunderstood, that is not the situation now, so you would need to solve the reliability issue for anything better to work. Quote Link to comment https://forums.phpfreaks.com/topic/304190-sleeping-a-rest-call/#findComment-1547690 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.