470,866 Members | 1,903 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,866 developers. It's quick & easy.

User interface for process that takess a long time...

JGH
I want to create a web page that allows a user to run an oracle stored
procedure. That's easy enough but the problem is that the procedure
might take several seconds to complete.

I don't want the user to be able to cancel the process. So,
essentially, I want to call the procedure as a background task and have
the web page that the user sees display something like, "Please wait
while I update the database." And then every 10 seconds or so
automatically refresh the page.

So the tricky part, as far as I can tell, is to get it to NOT wait
while the stored procedure is executing.

<?php
$stmt = OCIparse ("BEGIN UPDATEDATABASE(); END;");
OCIexecute ($stmt);
?>

But this just sits there while the procedure executes.

Jul 17 '05 #1
4 4199
JGH wrote:
I want to create a web page that allows a user to run an oracle stored
procedure. That's easy enough but the problem is that the procedure
might take several seconds to complete.

I don't want the user to be able to cancel the process. So,
essentially, I want to call the procedure as a background task and have
the web page that the user sees display something like, "Please wait
while I update the database." And then every 10 seconds or so
automatically refresh the page.

So the tricky part, as far as I can tell, is to get it to NOT wait
while the stored procedure is executing.

<?php
$stmt = OCIparse ("BEGIN UPDATEDATABASE(); END;");
OCIexecute ($stmt);
?>

But this just sits there while the procedure executes.


I did this once to do something along the lines of what you are wanting to
do. It worked for me but I can't guarantee it will work for you...

set_time_limit(0);
ignore_user_abort(1);
register_shutdown_function("foo");
header("location: bar.php");
flush();
exit();

Then there was a function called "foo" (well it wasn't really called that
but is in this example) which does all the work. The only issue then is how
to get the progress page to work out if the work is done yet.

Ten seconds isn't really a very long time to wait for something to process.
You may just want to make them wait it out but use ignore_user_abort(1) to
ensure they can't stop the page from doing its magic.

http://www.php.net/set_time_limit
http://www.php.net/ignore_user_abort
http://www.php.net/register_shutdown_function
http://www.php.net/header

--
Chris Hope
The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #2
On Thu, 6 May 2004 22:00:24 +0000 (UTC), JGH <jo******@nospam.tds.net> wrote:
I want to create a web page that allows a user to run an oracle stored
procedure. That's easy enough but the problem is that the procedure
might take several seconds to complete.

I don't want the user to be able to cancel the process. So,
essentially, I want to call the procedure as a background task and have
the web page that the user sees display something like, "Please wait
while I update the database." And then every 10 seconds or so
automatically refresh the page.

So the tricky part, as far as I can tell, is to get it to NOT wait
while the stored procedure is executing.

<?php
$stmt = OCIparse ("BEGIN UPDATEDATABASE(); END;");
OCIexecute ($stmt);
?>

But this just sits there while the procedure executes.


One option would be to submit a job using DBMS_JOB, set to run once
immediately, and have it set some flag that you can query for in the subsequent
refreshes, or query USER_JOBS with the job ID. Something like:

async.php:
----------

<pre>
<?php
function check_oci_error($status, $handle = null) {
if (!$status) {
$error = $handle ? OCIError($handle) : OCIError();
die("error: " . $error['message']);
}
}

check_oci_error($conn = OCILogon('test', 'test', 'dev101'));
check_oci_error($stmt = OCIParse($conn, "
begin
dbms_job.submit(:job_id,
'begin dbms_lock.sleep(25); end;',
sysdate);
end;"), $conn);

OCIBindByName($stmt, ':job_id', &$job_id, 38);

check_oci_error(OCIExecute($stmt, OCI_DEFAULT), $stmt);
check_oci_error(OCICommit($conn));

echo "<a href='waitforjob.php?job_id=$job_id'>Wait for job $job_id</a>";
?>
</pre>
waitforjob.php:
---------------

<pre>
<?php
function check_oci_error($status, $handle = null) {
if (!$status) {
$error = $handle ? OCIError($handle) : OCIError();
die("error: " . $error['message']);
}
}

check_oci_error($conn = OCILogon('test', 'test', 'dev101'));
check_oci_error($stmt = OCIParse($conn, 'select broken from user_jobs where job
= :job_id'));

OCIBindByName($stmt, ':job_id', $_GET['job_id'], -1);

check_oci_error(OCIExecute($stmt, OCI_DEFAULT), $stmt);

if (OCIFetchInto($stmt, $row, OCI_ASSOC)) {
var_dump($row);
if ($row['BROKEN'] == 'Y') {
echo "Job broken.";
} else {
echo "Job still running. (<a
href='waitforjob.php?job_id={$_GET['job_id']}>refresh</a>)";
}
} else {
echo "Job complete.";
echo "<a href='async.php'>submit another</a>";
}
?>
</pre>

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #3
JGH
Chris Hope <ch***@electrictoolbox.com> wrote in
I did this once to do something along the lines of what you are
wanting to do. It worked for me but I can't guarantee it will work for
you...

set_time_limit(0);
ignore_user_abort(1);
register_shutdown_function("foo");
header("location: bar.php");
flush();
exit();

Then there was a function called "foo" (well it wasn't really called
that but is in this example) which does all the work. The only issue
then is how to get the progress page to work out if the work is done
yet.

Ten seconds isn't really a very long time to wait for something to

[...]

Well, the process might take a lot longer than that but I thought I'd check
it's status (via a flag) every 10 seconds or so. That would make the user
think something is happening.

I used a lot of your code but instead of the shutdown function and a call
to header(), I put a META refresh tag in the header and called the Oracle
stored procedure at the end of the script. So 2 seconds after the script
completes, the page is redirected to another page that shows the results.

The main problem with my approach is that it is subject to all the failings
of the flush() function -- which can't *force* a server (much less the
browser) to flush output. But, well, close enough for gub'ment work. It
works on the servers & browsers we're using today. The worst that can
happen is that the user doesn't get the "Please wait" message. He might
think his browser is hung but by the time he looks up my phone number it
will have finished.

<HTML>
<HEAD>
<META http-equiv=Refresh CONTENT="2; URL=billing.php">
</HEAD>
<BODY>

Please wait...

<?php
flush();
set_time_limit(0);
ignore_user_abort(1);

$stmt = OCIParse ($dbh, "BEGIN DOSOMETHING(); END;");
OCIExecute ($stmt, OCI_COMMIT_ON_SUCCESS);
?>

Finished! This page will refresh in 2 seconds!
</BODY>
</HTML>

Jul 17 '05 #4
JGH wrote:
Chris Hope <ch***@electrictoolbox.com> wrote in
I did this once to do something along the lines of what you are
wanting to do. It worked for me but I can't guarantee it will work for
you...

set_time_limit(0);
ignore_user_abort(1);
register_shutdown_function("foo");
header("location: bar.php");
flush();
exit();

Then there was a function called "foo" (well it wasn't really called
that but is in this example) which does all the work. The only issue
then is how to get the progress page to work out if the work is done
yet.

Ten seconds isn't really a very long time to wait for something to

[...]

Well, the process might take a lot longer than that but I thought I'd
check it's status (via a flag) every 10 seconds or so. That would make the
user think something is happening.

I used a lot of your code but instead of the shutdown function and a call
to header(), I put a META refresh tag in the header and called the Oracle
stored procedure at the end of the script. So 2 seconds after the script
completes, the page is redirected to another page that shows the results.

The main problem with my approach is that it is subject to all the
failings of the flush() function -- which can't *force* a server (much
less the browser) to flush output. But, well, close enough for gub'ment
work. It works on the servers & browsers we're using today. The worst that
can happen is that the user doesn't get the "Please wait" message. He
might think his browser is hung but by the time he looks up my phone
number it will have finished.

<HTML>
<HEAD>
<META http-equiv=Refresh CONTENT="2; URL=billing.php">
</HEAD>
<BODY>

Please wait...

<?php
flush();
set_time_limit(0);
ignore_user_abort(1);

$stmt = OCIParse ($dbh, "BEGIN DOSOMETHING(); END;");
OCIExecute ($stmt, OCI_COMMIT_ON_SUCCESS);
?>

Finished! This page will refresh in 2 seconds!
</BODY>
</HTML>


You probably need to add another flush() after the last closing HTML tag to
ensure it really is flushed. Although as you pointed out flush() doesn't
always do what you expect. In my actual code I also had a call to
ob_end_flush() directly under the flush() call. It was a long time ago I
coded it so I guess that must have been after experimentation.

--
Chris Hope
The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by zlst | last post: by
24 posts views Thread by Rob R. Ainscough | last post: by
8 posts views Thread by mark.norgate | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.