Jump to content

Sleep function via web access


Mike521

Recommended Posts

Hi all, I have a web script that calls an external function written in another language. The external function will take a few minutes to run, and then my script grabs the output file it creates. I have no control over the external function, just the ability to trigger it and grab it's output

 

so I needed a way to make my script wait for a little while, and I found the sleep() function. but I think I'm misunderstanding it.

 

the problem is that when I trigger my script via the web, it runs up until the sleep portion, then never seems to wake up. here's my basic code, leaving out the meaningless stuff:

 

// do some meaningless stuff
// trigger the external function. it will take at least 2 minutes to run
// so lets go to sleep 
echo "<br>I'm going to sleep for 10 minutes";
//sleep(600);
sleep(300); // 5 minutes, we're testing
echo "<br>I'm awake";
// grab the output file, play with it for a minute
// email a confirmation that we woke up
if ( $sendEmailVerification ) {
if( mail( $mail_to, $mail_subject, $email_html, $headers) ) {
	// great
	echo "<br>email sent";
} else {
	// something bad happened
	echo "something bad happened...";
}
}

 

everything works perfectly when I don't sleep, except that the output file isn't ready yet. if it doesn't exist, my script crashes. if it does exist from a previous run, then my script uses the old file rather than waiting for the new one.

 

I start my script just by visiting it's URL through firefox, and I think that's where the problem is. I think my browser doesn't wait for the script to finish, and then I guess the web server says "well no one is listening anymore, kill the process" -- or maybe it's the other way around, I dunno.

 

thanks in advance!

Link to comment
Share on other sites

damn! it still doesn't wake up. it waits the full amount of time (I even set max execution to 60 seconds longer than sleep time, just to give it some cushion) and then just dies

 

it prints all the echo statements right up to/including "I'm going to sleep" then never wakes up again!

 

my browser gets an hourglass for the max execution time, then after that expires it stops loading, hourglass goes away, but no more output is printed.. I also don't get the confirmation email

Link to comment
Share on other sites

ok I did some testing, everything works fine up to a 3:59 wait time. at 4:00 it stops waking up.

 

here's my slightly updated code:

$sleepTime = 240; // in seconds, this is how long we'll wait for the external function to process
ini_set( "max_execution_time", $sleepTime + 60 );
// do some meaningless stuff
// trigger the external function. it will take at least 2 minutes to run
// so lets go to sleep 
echo "<br>I'm going to sleep for " . $sleepTime/60 . " minutes";
sleep( $sleepTime );
echo "<br>I'm awake";
// grab the output file, play with it for a minute
// email a confirmation that we woke up
if ( $sendEmailVerification ) {
if( mail( $mail_to, $mail_subject, $email_html, $headers) ) {
	// great
	echo "<br>email sent";
} else {
	// something bad happened
	echo "something bad happened...";
}
}

Link to comment
Share on other sites

sorry Ken I was typing that response at the same time you sent yours --

 

I trigger the external function by calling it in an iframe:

 

/* call the external function */
if ( $callExternalFlag ) {
echo "<br><iframe src='$theFunctionURLA$theOptionSetting$theFunctionURLB' width='99%'></iframe>";
}

 

the src turns out to be something along the lines of "http://www.thesite.com/thefunction.mvc?bunchOfArguments=bunchofValues"

 

actually the external function itself only takes about a minute to process, but as the script builds I'll have to call several of these, and I can never really be sure exactly how long it'll take, so I overestimate

Link to comment
Share on other sites

I did more searching and added

ini_set( "max_execution_time", $sleepTime + 60 ); //this doesn't seem to work
set_time_limit( $sleepTime + 60 ); // doesn't work either
ignore_user_abort(TRUE); // and neither does this

 

none of them worked, it still stops waking up at 4 minutes or more. I guess I'll contact my host, there must be some sort of setting I don't know about

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.