Perl Batch Client

From Textserver wiki
Revision as of 13:13, 20 January 2016 by Padro (Talk | contribs)

Jump to: navigation, search
 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 "Output ZIP file: ";
26 my $outfname=<>; chomp $outfname;
27 print "Language: "; 
28 my $lang=<>; chomp $lang;
29 print "Output format (xml,json,conll,naf): ";
30 my $out=<>; chomp $out;
31 print "TextServer username: "; 
32 my $user=<>; chomp $user;
33 print "TextServer password: "; 
34 my $pwd=<>; chomp $pwd;
35 
36 # URL for the requested service
37 my $TextServer_URL = "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws";
38 my $service = "SERVICENAME";
39 
40 # create user agent
41 my $ua = LWP::UserAgent->new;
42 
43 # send request to server
44 my $resp = $ua->post($TextServer_URL."/processQuery/".$service,
45                      Content_Type => 'form-data',
46                      Content  => [
47                                   username => $user,   
48                                   password => $pwd,   
49                                   file => [$fname],
50                                   language => $lang,  
51                                   output => $out,
52                                   interactive => "0"
53                                  ]
54                     );
55 
56 # check for success in connection
57 die $resp->status_line." - ".$resp->decoded_content."\n" unless $resp->is_success;
58 
59 # Server response should include a job tokenID, retrieve it                                                
60 my $twig= new XML::Twig();
61 $twig->parse($resp->decoded_content);
62 my $tkid = $twig->root->text("job_token_id");
63 print "Job sumbitted. Token id: [$tkid]\n";
64 
65 # Periodically poll server until batch job is ended.                                                       
66 my $nseconds=20;
67 while (1) {
68 
69     print "Sleeping for $nseconds seconds\n";
70     sleep($nseconds);
71 
72     # send request to check for job termination
73     print "Polling server for job completion...\n";
74     $resp = $ua->post($TextServer_URL."/resultRetrieve",
75                       Content_Type => 'form-data',
76                       Content  => [username => $user, tokenID => $tkid] );
77     
78     # if status=200, we a got ZIP response, the job is done, exit loop.
79     if ($resp->is_success) { last; }
80 
81     # status!=200, check the error code
82     if ($resp->code==503 && $resp->decoded_content =~ /^\[TS\-125\]/ ) {
83         # http status 503, textserver code TS-125 means the job is not finished yet. keep waiting
84         print "Job not finished yet\n"; 
85     }
86     else {
87         # some actual error happened. abort
88         die $resp->status_line." - ".$resp->decoded_content."\n"
89     }
90 }
91 
92 # print zipped respose to output file
93 print "Job finished. Saving results to $outfname\n"; 
94 open(my $outf, '>', $outfname);
95 print $outf $resp->decoded_content;
96 close $outf;