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? 

Link to comment
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.

Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • 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.