Jump to content

Best way to make a script restart itself (resume) repeatedly without timeouts?


Solaris

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

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