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

Hi All

first timer here :-)

I have an odd problem. I have written a perl module that I use to post data to an ESB. It uses LWP and HTTP::Request. I have a test unit that uses this and it works great. My first real use for this module was to do some processing within a cgi script (which happens to use cgi.pm). Once the processing is done, I call my module in order to post a message to the ESB, and then return OK to the browser. That's the theory. What happens in reality is that the call to the ESB hangs for about 10 seconds when called from within the CGI.

It seems like this may be due to STDIN/STDOUT redirection but I am not sure. I just cannot make it work and would appreciate some guidance.

Thx Z

Replies are listed 'Best First'.
Re: Post within CGI
by GrandFather (Saint) on Dec 14, 2011 at 00:34 UTC

    It's line 42. It's always line 42 and all monks have tremendous ESP ability - we can visualise other people's code from a hard disk in a server locked in a safe around the far side of the world.

    Ah, what what the question again?

    True laziness is hard work

      Ah, what what the question again?

      "What do you get if you multiply six by nine?"

Re: Post within CGI
by Anonymous Monk on Dec 14, 2011 at 00:34 UTC
Re: Post within CGI
by TJPride (Pilgrim) on Dec 14, 2011 at 09:14 UTC
    Maybe the CGI script is having to look up where the ESB (whatever the heck that is) is located before it can post to it. This can cause the 10 second delay you're mentioning if the nameservers lag. I had the same problem on my DSL for a while. If you can access the ESB (again, what is that?) directly without lagging, I'd try figuring out what the CGI script is using for nameservers.

    Can the script post to other things without lag? Or does it lag period?

      OK learnt my lesson :-)

      Here is the module code (only the relevant bit)

      package PoxPost; use strict; use LWP::UserAgent; use HTTP::Request::Common; use CGI; use CGI::Carp qw ( fatalsToBrowser ); use XML::Simple; my $esbhost = "http://5.0.10.212:8280/services/"; my $query; ###################################################################### # PostESB ###################################################################### sub PostESB { my $servicename = $_[0]; my $payload = $_[1]; my $userAgent = LWP::UserAgent->new(agent => 'perl post'); my $message = "<tag>".$payload."</tag>"; my $link = $esbhost.$servicename; my $response = $userAgent->request(POST $link, Content_Type => 'application/xml', Content => $message); if ($response->is_success){ return 1; } else{ return 0; } }

      Now here is my test code

      print "start\n"; $svc = "PRUNE"; $msg = "<job>90099368</job><root>/gpfs/STUDIOS/HQ/</root><version>2</v +ersion>"; PoxPost::PostESB($svc, $msg); print "sent\n";

      This test works exactly as expected

      Now here is part of the CGI that is called, and which in turn tries to call the above method

      #!/usr/bin/perl -w use CGI; use CGI::Carp qw ( fatalsToBrowser ); use File::Basename; use File::Copy; use File::Find; use File::Path; use DBI; use POSIX; use lib "/home/zed/ZLIB"; require "poxpost.pl"; ##################################################### # CGI Initialisation ##################################################### # get the form details my $query = new CGI; HTMLHeader(); umask(0); # check action requested my $action = $query->param("action"); # now call required handler if ($action eq "jobversion"){ JobVersionCreate(); } # end header print qq(</body> </html>); # END ###################################################################### +######## # HTMLHeader # ###################################################################### +######## sub HTMLHeader{ print $query->header( ); print qq(<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" +"DTD/xhtml1-strict.dtd">); print qq(<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" +lang="en"> <head>); print qq(<meta http-equiv="Content-Type" content="text/html; chars +et=utf-8" />); print qq(<link href="/compare/style.css" rel="stylesheet" type="te +xt/css" media="screen" />); print qq(</head> <body>); } ###################################################################### +######## # JobVersionCreate ###################################################################### +######## sub JobVersionCreate{ print "creating job version "; # now send message to prune previous versions $payload = "<job>job</job><root>pathroot</root><version>ver</ver>" +; print "<result>OK</result>"; PoxPost::PostESB("COMPARE_PRUNE", $payload); }

      The problem is the call to PostESB. This is where the browser just hangs for several seconds. If I remove this line, it works fine. The test code for the PostESB runs very quick. It is the use within the CGI that is the issue.

      I hope that the above gives you enough info

      Thx Z

        In sub HTMLHeader, "print $query->header()" will create your header... but all the lines that follow, er, create the header... again. That may be part of the problem.

        It appears, if I'm reading this correctly, that your test code does not send the same information as your real code. How about you have your test code duplicate the call that's lagging for you and see if you get the same results? If so, then it's probably the ESB service that's lagging, for whatever reason.

        Of course, I could be entirely wrong.