On May 13, 2025, at 8:23 PM, Vincent Veyron <vv.li...@wanadoo.fr> wrote:
> Is there a way I could do :
> 
>       $r->print('<h3>Building tar file</h3>') ;
> 
> every second or other until the tar file is built? I can't seem to think of 
> one.
> 
> My handler calls an sql script that dumps the database with system() :
> 
>    @args = ('psql', '-f', '/path/to/script/to/export_raw_data.sql', '-v', 
> 'id_client=' . $r->pnotes('session')->{id_client}, '-v',  'database=' . 
> $database, 'postgres') ;
> 
>    system(@args) == 0 or warn "system @args failed: $!" ;
> 
> then waits for the resulting file before proceeding to compress and tar it, 
> also with a system() call. 
> 
> 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-running-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

Reply via email to