Solaris Posted November 19, 2010 Share Posted November 19, 2010 I have searched this forum and the web, but I haven't been able to find a good answer. I'm interested in the best way (especially some working code snippet, hopefully!) to make a script stop when a limit (either number of rows or time limit or whatever) is reached, call itself and pass some variables to itself via sessions/cookies or $_REQUEST, and then resume where it left off the last time. Does anybody know a way to do this that works reliably in most (all?) environments? Any code that uses header(), output buffering, AJAX, or anything else, that is proven to work? What I want to do is make a generic script that accepts a (very large) local flat file as a data source, processes the contents and outputs the results either to another local flat file or to another application using the latter's routines. The result isn't necessarily going to be imported into MySQL (so no, LOAD DATA INFILE isn't applicable here). An example I can think of is processing a CSV file (say, about 100,000 records), converting it on-the-fly and importing the results into WordPress by calling WP's functions to do the actual import. Again, this is just an example. I can write all input, processing and output functions, it's just the best way to restart and resume the script that I'm asking about. BigDump is another script that does this, but it seems to use AJAX to restart itself. I wouldn't mind using AJAX/jQuery/whatever; in that case, I'd just need some working drop-in code, since my JavaScript/AJAX knowledge isn't very good, to say the least. Nevertheless, I'd rather prefer pure PHP, just in case the user has JavaScript disabled. Sorry for the long post and thanks in advance! Link to comment https://forums.phpfreaks.com/topic/219181-best-way-to-make-a-script-restart-itself-resume-repeatedly-without-timeouts/ Share on other sites More sharing options...
litebearer Posted November 19, 2010 Share Posted November 19, 2010 when (time frame from initial run) should it 'resume'? Link to comment https://forums.phpfreaks.com/topic/219181-best-way-to-make-a-script-restart-itself-resume-repeatedly-without-timeouts/#findComment-1136576 Share on other sites More sharing options...
Solaris Posted November 19, 2010 Author Share Posted November 19, 2010 Every 20-25 seconds, because I just cannot depend on the actual effect of set_time_limit(), apache_reset_timeout() and the like. Link to comment https://forums.phpfreaks.com/topic/219181-best-way-to-make-a-script-restart-itself-resume-repeatedly-without-timeouts/#findComment-1136579 Share on other sites More sharing options...
litebearer Posted November 19, 2010 Share Posted November 19, 2010 Just a random thought/idea start sessions check to see if session variable 'resume' is set if not - begin intitial running of script when limit is reached, set session variable 'resume' to true, and session variable where _to_resume 'sleep' for 25 secs call self if set get session variable where_to_resume begin resuming script when limit is reached, set new value to session variable where_to_resume 'sleep' for 25 secs call self Link to comment https://forums.phpfreaks.com/topic/219181-best-way-to-make-a-script-restart-itself-resume-repeatedly-without-timeouts/#findComment-1136584 Share on other sites More sharing options...
Solaris Posted November 19, 2010 Author Share Posted November 19, 2010 It's exactly the "call self" part that I'm asking about. I have no problem writing the rest of the code. Link to comment https://forums.phpfreaks.com/topic/219181-best-way-to-make-a-script-restart-itself-resume-repeatedly-without-timeouts/#findComment-1136607 Share on other sites More sharing options...
litebearer Posted November 19, 2010 Share Posted November 19, 2010 header ("Location: url_to_your_script"); As long has you have outputted nothing to the browser, the above should work for your needs Link to comment https://forums.phpfreaks.com/topic/219181-best-way-to-make-a-script-restart-itself-resume-repeatedly-without-timeouts/#findComment-1136622 Share on other sites More sharing options...
Solaris Posted November 19, 2010 Author Share Posted November 19, 2010 header ("Location: url_to_your_script"); As long has you have outputted nothing to the browser, the above should work for your needs Thanks, but the script does output to the browser before restarting. I have seen a few code examples that show how to do it, I just don't know what code works reliably in all environments. In other words, I need proven code, not just something that works in X or Y server setup but is not tested on others. Link to comment https://forums.phpfreaks.com/topic/219181-best-way-to-make-a-script-restart-itself-resume-repeatedly-without-timeouts/#findComment-1136645 Share on other sites More sharing options...
litebearer Posted November 19, 2010 Share Posted November 19, 2010 have you tried a metatag redirect Link to comment https://forums.phpfreaks.com/topic/219181-best-way-to-make-a-script-restart-itself-resume-repeatedly-without-timeouts/#findComment-1136649 Share on other sites More sharing options...
Solaris Posted November 19, 2010 Author Share Posted November 19, 2010 Perhaps I haven't been clear... I'm not asking for ideas or code that I can test, but rather for code that has already been tested and found to work correctly at all times. Link to comment https://forums.phpfreaks.com/topic/219181-best-way-to-make-a-script-restart-itself-resume-repeatedly-without-timeouts/#findComment-1136671 Share on other sites More sharing options...
litebearer Posted November 19, 2010 Share Posted November 19, 2010 My apologies, guess trying to help isn't called for here Link to comment https://forums.phpfreaks.com/topic/219181-best-way-to-make-a-script-restart-itself-resume-repeatedly-without-timeouts/#findComment-1136674 Share on other sites More sharing options...
Solaris Posted November 19, 2010 Author Share Posted November 19, 2010 My apologies, guess trying to help isn't called for here I am asking for help and I thank you for trying. Is the question too hard, perhaps? Is there a board here about advanced topics? Link to comment https://forums.phpfreaks.com/topic/219181-best-way-to-make-a-script-restart-itself-resume-repeatedly-without-timeouts/#findComment-1136682 Share on other sites More sharing options...
jdavidbakr Posted November 19, 2010 Share Posted November 19, 2010 You're asking for someone to write your code for you. That's not the purpose of these forums - we're here to help you figure out what you need to do. If you want us to write your code for you, you can contact us and we'll bid the job - Link to comment https://forums.phpfreaks.com/topic/219181-best-way-to-make-a-script-restart-itself-resume-repeatedly-without-timeouts/#findComment-1136685 Share on other sites More sharing options...
Solaris Posted November 19, 2010 Author Share Posted November 19, 2010 You're asking for someone to write your code for you. That's not the purpose of these forums - we're here to help you figure out what you need to do. If you want us to write your code for you, you can contact us and we'll bid the job - Just how much code is considered to be a paid job? What I'm asking about is whether anyone knows which is the most robust code to restart a script, nothing more. I'm not asking for someone to write a complete script for me. The restart code can even be readily-available from a well-known open source script, provided it's short (a few lines?) and generic enough to use in other scripts, like the one I'm writing. Again, I've seen a few ways to restart a script, I just don't know which is the most robust. I apologize if I'm asking in the wrong place. Link to comment https://forums.phpfreaks.com/topic/219181-best-way-to-make-a-script-restart-itself-resume-repeatedly-without-timeouts/#findComment-1136707 Share on other sites More sharing options...
jdavidbakr Posted November 19, 2010 Share Posted November 19, 2010 The fact that no one has given you an answer probably means that we don't know of an existing script that does what you ask. The fact that you're unhappy that instead of giving you a "robust script" we're giving you "ideas" on how to create your own gives off the impression that you are trying to leech your code from people who know how to do it. A good programmer doesn't indiscriminately throw in blocks of code that "work" to create their application. While there are libraries and APIs and functions you can get to speed your development, in general you're better off if you fully understand what every part of code is doing and often times that means writing it yourself. Link to comment https://forums.phpfreaks.com/topic/219181-best-way-to-make-a-script-restart-itself-resume-repeatedly-without-timeouts/#findComment-1136714 Share on other sites More sharing options...
Solaris Posted November 19, 2010 Author Share Posted November 19, 2010 The fact that no one has given you an answer probably means that we don't know of an existing script that does what you ask. The fact that you're unhappy that instead of giving you a "robust script" we're giving you "ideas" on how to create your own gives off the impression that you are trying to leech your code from people who know how to do it. A good programmer doesn't indiscriminately throw in blocks of code that "work" to create their application. While there are libraries and APIs and functions you can get to speed your development, in general you're better off if you fully understand what every part of code is doing and often times that means writing it yourself. I'm sorry, did you even read my last reply? I'm neither asking for a complete script nor for general programming advice. In other words, I'm not asking for someone to do a wheel for me, but rather which existing wheel is the best (there are plenty available already, and I certainly don't want to reinvent one). Oh, and please don't look at my post count. Link to comment https://forums.phpfreaks.com/topic/219181-best-way-to-make-a-script-restart-itself-resume-repeatedly-without-timeouts/#findComment-1136739 Share on other sites More sharing options...
jdavidbakr Posted November 19, 2010 Share Posted November 19, 2010 If you're wanting our opinion on which publicly available script is best you might point us to what you're looking at, and maybe describe why you think one is better than the other. I apologize if I've come across too strong, I have found that generally there are two types of posters here - those who want to learn how to do something and those who want us to do it for them. I try to be very helpful with the former, but the latter typically receive my ire. When you said "I'm not asking for ideas or code that I can test, but rather for code that has already been tested and found to work correctly at all times." I categorized you into the latter. Link to comment https://forums.phpfreaks.com/topic/219181-best-way-to-make-a-script-restart-itself-resume-repeatedly-without-timeouts/#findComment-1136745 Share on other sites More sharing options...
litebearer Posted November 19, 2010 Share Posted November 19, 2010 proven - robust - used by many many many ob_start(); echo "Test"; /* do your stuff here */ header("Location: http://www.php.net"); /* redirect here */ ob_flush(); Link to comment https://forums.phpfreaks.com/topic/219181-best-way-to-make-a-script-restart-itself-resume-repeatedly-without-timeouts/#findComment-1136749 Share on other sites More sharing options...
Solaris Posted November 19, 2010 Author Share Posted November 19, 2010 proven - robust - used by many many many ob_start(); echo "Test"; /* do your stuff here */ header("Location: http://www.php.net"); /* redirect here */ ob_flush(); Doesn't work for me: <?php ignore_user_abort(true); set_time_limit(0); ob_start(); $i = isset($_REQUEST['i']) ? (int) $_REQUEST['i'] : 1; $redirect_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?i=' . ($i + 1); echo $redirect_url; /* do your stuff here */ sleep(1); if ($i < 100) { $i++; header("Location: " . $redirect_url); /* redirect here */ } ob_flush(); ?> Tried on localhost with Firefox 3.6.12. Link to comment https://forums.phpfreaks.com/topic/219181-best-way-to-make-a-script-restart-itself-resume-repeatedly-without-timeouts/#findComment-1136881 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.