Best way to use sleep() in the background?

Hi All, this is for a project that I’ve inherited. The site has a three page questionnaire, each time a form is filled out data goes into the database.

After the first page/form is filled out, I need a script that will sleep 7 minutes and then query the database and get all of the info that was submitted and then send what data got entered off to a lead tracking website via cURL.

Obviously if I put sleep in the main signup script it will wait 7 minutes for the next page to load.

I’m thinking of placing the query and cURL in a separate script and caling it using touch, something like,


touch('./sleeper.php?email=me@me.com&id=777');

Something about this seems roundabout to me, does anyone have any thoughts or opinions on how to do this?

Thanks.

Just to clarify, do the 3 forms get saved to the db individually and on successful completion of the final form (3) the form contents should be sent on?

Or are you really saying as long as form1 is filled in, even if the rest of the operation is abandoned by the user, you then wait and send it on?

The three forms get saved individually and people can bail out at any stage. I may get one page or all three pages.

In any event I need to send what data I did get to the lead tracking website.
Thanks

I am thinking you may instead use a cron to start a script every couple of munites. That script will query database and select all records that have been created 7 minutes ago, send then out to some API, then mark them as ‘processed’ in the database.

All you need to do is to make sure there is a ‘created_time’ and ‘processed’ fields in the leads table.
No need to use sleep at all for something like this

I thought about a cron but if the cron runs right after the first form is submitted that is all the data I get.
This has to be a timer based on when the first form gets submitted.
As always all input is appreciated.

OK touch is not the answer, it creates files with the name “sleeper.php?email=me@me.com&id=777”.
I need some way to pass a query string to a script that will run in the background.

I tried cURL but once again I get a 7 second delay, here is the snippet.


		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS,$postf);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		$result= curl_exec ($ch);
		curl_close ($ch);

I thought that cURL without “CURLOPT_RETURNTRANSFER, 1” would just post the data and not wait for the sleeper script to finish executing.

There must be a simple answer.

If I am right in thinking, you can link all the 3 forms with some token like a user id.

How about you had a table which records the events of each form being saved.

timer_table

id_ref - a UID
form_1 - a timestamp
form_2 - a timestamp
form_3 - a timestamp
sent - BOOL 1/0

Then you can tell cron every minute say, to call a script which polls that table for rows which have a form1 time of > 7 minutes but < 8 minutes AND a sent of 0

EDIT

OR a form1 time as above and anything in form3…

ENDEDIT

… (maybe sent is YAGNI) actually then look up that user id_ref and pass to curl?

Something like that?

Thanks for the ideas, trying to get php to do something in the background turned out to be a lot more challenging than I thought.
Bash to the recue,


exec("nohup nice php -q ./sleeper.php  -f filename -t filetype > /dev/null & echo $!");

Bash at first was not executing in the background. Add the trimmings like so and it works a treat.
Thanks again, hope this helps someone else.