Difference between revisions of "Perl Batch Client"

From Textserver wiki
Jump to: navigation, search
 
Line 24: Line 24:
 
print "Input ZIP file: ";
 
print "Input ZIP file: ";
 
my $fname=<>; chomp $fname;
 
my $fname=<>; chomp $fname;
print "Output ZIP file: ";
 
my $outfname=<>; chomp $outfname;
 
 
print "Language: ";  
 
print "Language: ";  
 
my $lang=<>; chomp $lang;
 
my $lang=<>; chomp $lang;
Line 62: Line 60:
 
$twig->parse($resp->decoded_content);
 
$twig->parse($resp->decoded_content);
 
my $tkid = $twig->root->text("job_token_id");
 
my $tkid = $twig->root->text("job_token_id");
print "Job sumbitted. Token id: [$tkid]\n";
+
print "Job sumbitted. Token id=$tkid\n";
 
+
# Periodically poll server until batch job is ended.                                                     
+
my $nseconds=20;
+
while (1) {
+
 
+
    print "Sleeping for $nseconds seconds\n";
+
    sleep($nseconds);
+
 
+
    # send request to check for job termination
+
    print "Polling server for job completion...\n";
+
    $resp = $ua->post($TextServer_URL."/resultRetrieve",
+
                      Content_Type => 'form-data',
+
                      Content  => [username => $user, tokenID => $tkid] );
+
   
+
    # if status=200, we a got ZIP response, the job is done, exit loop.
+
    if ($resp->is_success) { last; }
+
 
+
    # status!=200, check the error code
+
    if ($resp->code==503 && $resp->decoded_content =~ /^\[TS\-125\]/ ) {
+
        # http status 503, textserver code TS-125 means the job is not finished yet. keep waiting
+
        print "Job not finished yet\n";
+
    }
+
    else {
+
        # some actual error happened. abort
+
        die $resp->status_line." - ".$resp->decoded_content."\n"
+
    }
+
}
+
 
+
# print zipped respose to output file
+
print "Job finished. Saving results to $outfname\n";
+
open(my $outf, '>', $outfname);
+
print $outf $resp->decoded_content;
+
close $outf;
+
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 12:02, 21 January 2016

 1 #! /usr/bin/perl
 2 
 3 ########################################################################
 4 #
 5 #  Example client to submit a batch request to TextServer 
 6 # SERVICENAME service, and wait for the job to be finished.
 7 #
 8 #  Input must be a ZIP file containing one or more text files to analyze
 9 #  Output will be a ZIP file containing results for each input file in
10 # the requested format (XML, json, conll)
11 #
12 ########################################################################
13 
14 use strict;
15 
16 # import needeed libraries
17 use HTTP::Request::Common;
18 use LWP::UserAgent;
19 use XML::Twig;
20 use XML::LibXML;
21 
22 # get query parameters
23 print "Input ZIP file: ";
24 my $fname=<>; chomp $fname;
25 print "Language: "; 
26 my $lang=<>; chomp $lang;
27 print "Output format (xml,json,conll,naf): ";
28 my $out=<>; chomp $out;
29 print "TextServer username: "; 
30 my $user=<>; chomp $user;
31 print "TextServer password: "; 
32 my $pwd=<>; chomp $pwd;
33 
34 # URL for the requested service
35 my $TextServer_URL = "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws";
36 my $service = "SERVICENAME";
37 
38 # create user agent
39 my $ua = LWP::UserAgent->new;
40 
41 # send request to server
42 my $resp = $ua->post($TextServer_URL."/processQuery/".$service,
43                      Content_Type => 'form-data',
44                      Content  => [
45                                   username => $user,   
46                                   password => $pwd,   
47                                   file => [$fname],
48                                   language => $lang,  
49                                   output => $out,
50                                   interactive => "0"
51                                  ]
52                     );
53 
54 # check for success in connection
55 die $resp->status_line." - ".$resp->decoded_content."\n" unless $resp->is_success;
56 
57 # Server response should include a job tokenID, retrieve it                                                
58 my $twig= new XML::Twig();
59 $twig->parse($resp->decoded_content);
60 my $tkid = $twig->root->text("job_token_id");
61 print "Job sumbitted. Token id=$tkid\n";