Jump to content

sleeping a REST call

Recommended Posts

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? 

Share this post

Link to post
Share on other sites

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.

Share this post

Link to post
Share on other sites

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.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


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.