SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,104
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    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,

    PHP Code:
    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.
    What I lack in acuracy I make up for in misteaks

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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?

  3. #3
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,104
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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
    What I lack in acuracy I make up for in misteaks

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2011
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  5. #5
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,104
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.
    What I lack in acuracy I make up for in misteaks

  6. #6
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,104
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.
    What I lack in acuracy I make up for in misteaks

  7. #7
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,104
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I tried cURL but once again I get a 7 second delay, here is the snippet.
    PHP Code:
            $ch curl_init();
            
    curl_setopt($chCURLOPT_URL,$url);
            
    curl_setopt($chCURLOPT_POST1);
            
    curl_setopt($chCURLOPT_POSTFIELDS,$postf);
            
    curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
            
    $resultcurl_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.
    What I lack in acuracy I make up for in misteaks

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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?

  9. #9
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,104
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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,
    PHP Code:
    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.
    What I lack in acuracy I make up for in misteaks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •