http://qs1969.pair.com?node_id=284530

tja_ariani has asked for the wisdom of the Perl Monks concerning the following question:

Hi PerlMonks,
I have a structure that consists of a webserver(Server A) and a tcp server (Server B).

First a user will upload a file through a website to the webserver A,
Secondly, when receiving this new file it will automatically trigger a Perl program in A, that will send the uploaded file to Server B.
This second part should be hidden from public.

I have done the website for uploading to A, and have written a simple program for sending the a file to B.

But the problem is, I don't know how to integrate them, i.e. detecting the new uploaded program, and execute/trigger the simple program to send the new file to B.

Thanks

Replies are listed 'Best First'.
Re: triggering a program in web server
by bm (Hermit) on Aug 18, 2003 at 10:41 UTC
    The first question that comes to my mind is: why do you need two scripts for this? The public cannot read your cgi-script (assuming a correctly configured webserver and filesystem permsissions).

    Having said that....

    Simply call your 2nd program like any other system call ( system, exec, ...) from within your first script.

    Be aware that if you do call an external program, you should:

  • read  perldoc perlsec carefully
  • definitely enable taint checking (perl -T)
    --
    bm
Re: triggering a program in web server
by arthas (Hermit) on Aug 18, 2003 at 10:34 UTC

    Hi!

    You can launch the upload-to-B program directly from the script which, on A, receives the file uploaded via web. This might however slow the response to the user down, depending on how fast is the connection between A and B.

    Another option is to have upload-to-B program run as a daemon, which every now and then (how much often you wish) checks for new uploaded files and sends them to B.

    Hope this helps!

    Michele.

      Hi,

      you're rite, I was thinkin to have the program run as a daemon, but I do not know how to do the scheduling part.
      Is there any simple program or piece of code as an example to show how to do this ?

      I was thinkin using infinite loop in the program to check, but will it slow down the server ?
      Or is there any command to 'sleep' the program periodically ?

      Thanks

        A possible approach is: every time a file is uploaded you put a link to that file in a special directory; then every n seconds (via cron or using sleep) you check the directory, move new files and delete links.

        HTH, Valerio

        I was thinkin to have the program run as a daemon, but I do not know how to do the scheduling part.

        Take a look at Net::Daemon . But I can't see the advantage of running this as a daemon is (the small performance gain of not loading Perl?).
        --
        bm

Re: triggering a program in web server
by Abigail-II (Bishop) on Aug 18, 2003 at 10:54 UTC
    So, what service do you use on server B? If you really need this convoluted scheme, why not use http to upload the file on server B, the same way it was uploaded to server A? LWP will help you to do this.

    Abigail

Re: triggering a program in web server
by gjb (Vicar) on Aug 18, 2003 at 10:44 UTC

    As an addendum to arthas's post, you may also use a cron facility on server A that runs the transfer program every so often (modified so that it tests whether there's something to trasnfer).

    Just my 2 cents, -gjb-

      Probably the reason why choosing synchronous execution (script A calls script B) or asynchronous is how much time B takes and how you want to handle B's errors. In a synchronous environment you may report B's error to the user (say, destination server is down) and abort the process. If B is expected to be quite fats (say less than a few seconds) handling B synchronously is probably both easier and sensible. If B runs in 'daemon mode', ie as a daemon or using repeated sleep's, you'll have to handle B's errors anyhow using a log or something.
      My $.02