Re: Return a 200 0K status and send the content of the request later

Ed Sabol <[email protected]> Tue, 13 May 2025 22:57:05 -0400
Newsgroups gmane.comp.apache.mod-perl
Message-ID <[email protected]>
On May 13, 2025, at 8:23 PM, Vincent Veyron <[email protected]> wrote:
> Is there a way I could do :
>=20
> 	$r->print('<h3>Building tar file</h3>') ;
>=20
> every second or other until the tar file is built? I can't seem to =
think of one.
>=20
> My handler calls an sql script that dumps the database with system() :
>=20
>    @args =3D ('psql', '-f', '/path/to/script/to/export_raw_data.sql', =
'-v', 'id_client=3D' . $r->pnotes('session')->{id_client}, '-v',  =
'database=3D' . $database, 'postgres') ;
>=20
>    system(@args) =3D=3D 0 or warn "system @args failed: $!" ;
>=20
> then waits for the resulting file before proceeding to compress and =
tar it, also with a system() call.=20
>=20
> How can I wrap this in a loop that re-sends the 'Building tar file' =
message until the file exists?

Well, the standard way of doing that is you fork a child process to run =
the system command while the parent process loops and prints a period =
(or whatever you prefer) every 1 second (make sure you call flush() =
after printing!) while waiting for the child process to finish. Lots of =
good examples of this can be found here:

=
https://stackoverflow.com/questions/3193091/showing-progress-whilst-runnin=
g-a-system-command-in-perl/

The problem with this is that forking from a mod_perl process is more =
complicated than it is in a normal Perl process. The usual advice is to =
use a module called Apache2::SubProcess instead:

https://metacpan.org/pod/Apache2::SubProcess

But I don't know that that will work for what you want to do. I've never =
actually used that module, and I can't find an example in my Internet =
searches that shows the parent process doing something while the =
subprocess executes. Maybe try the example using detach_script.pl and =
then modify that detach_script.pl to build the tar file or execute psql =
or do whatever you want.

If that doesn't work, you can still fork from mod_perl, but you just =
need to know how to do it properly. Make sure you read this:

=
https://stackoverflow.com/questions/471681/how-do-i-fork-properly-with-mod=
-perl2/

Since I'm not convinced Apache2::SubProcess would actually work here, I =
would probably go with the "fork properly with mod_perl2" method, =
personally.

I also recommend that you still put an upper time limit on your =
subprocess and not just loop infinitely. 30 minutes to an hour seems =
like a reasonable choice. Depends on how big your tar files can be, of =
course.

When/if you get something along these lines working the way you want, =
please share your solution here on the mailing list for others to learn =
from.

Hope this helps,
Ed