« blib/lib/auto/share/dist/MOBY/cgi/MOBY-Central.pl
↑ Summary
blib/lib/auto/share/dist/MOBY/cgi/service_unit_tester.pl »
| 1 | #!/usr/bin/perl -w | ||
| 2 | #----------------------------------------------------------------- | ||
| 3 | # service_tester.pl | ||
| 4 | # Author: Edward Kawas <edward.kawas@gmail.com>, | ||
| 5 | # For copyright and disclaimer see below. | ||
| 6 | # | ||
| 7 | # $Id: service_tester.pl,v 1.6 2008/11/25 19:37:10 kawas Exp $ | ||
| 8 | # | ||
| 9 | # BETA | ||
| 10 | # | ||
| 11 | # This script goes ahead an asks a registry what services exist | ||
| 12 | # and then goes ahead and calls them all determining who is alive | ||
| 13 | # and who is not. This info is kept in a file as XML. | ||
| 14 | # | ||
| 15 | # Configurable options: | ||
| 16 | # TIMEOUT - the timeout in seconds to wait for each service | ||
| 17 | # THREAD_COUNT - the number of workers that will call services | ||
| 18 | # CATEGORIES - the moby service categories to test | ||
| 19 | # URL - the registry endpoint <optional> | ||
| 20 | # URI - the registry namespace <optional> | ||
| 21 | # DIRECTORY - the place to store details between jobs <optional> | ||
| 22 | # | ||
| 23 | # IMPORTANT NOTE: | ||
| 24 | # This script currently does not run on windows because of the | ||
| 25 | # IPC::Shareable module. A work around is being looked into. | ||
| 26 | # | ||
| 27 | # This script works on unix/linux. Other machines have not been | ||
| 28 | # tested. | ||
| 29 | #----------------------------------------------------------------- | ||
| 30 | |||
| 31 | use strict; | ||
| 32 | use warnings; | ||
| 33 | |||
| 34 | use MOBY::Config; | ||
| 35 | use MOBY::Client::Central; | ||
| 36 | use SOAP::Lite; | ||
| 37 | use XML::LibXML; | ||
| 38 | use HTTP::Request::Common qw(POST); | ||
| 39 | use LWP::UserAgent; | ||
| 40 | # Because of this library, cannot run on windows | ||
| 41 | use IPC::Shareable; | ||
| 42 | |||
| 43 | ######-------USER CONFIGURABLE PARAMETERS-------###### | ||
| 44 | # how long in seconds to wait for a service to respond | ||
| 45 | my $TIMEOUT = 20; | ||
| 46 | |||
| 47 | # the total number of 'threads' to use ... | ||
| 48 | my $THREAD_COUNT = 15; | ||
| 49 | |||
| 50 | # the categories of services to ping | ||
| 51 | my @CATEGORIES = qw / moby /; | ||
| 52 | |||
| 53 | ######-------------------------------------------###### | ||
| 54 | |||
| 55 | # the registry to query | ||
| 56 | my $URL = $ENV{MOBY_SERVER} || 'http://moby.ucalgary.ca/moby/MOBY-Central.pl'; | ||
| 57 | my $URI = $ENV{MOBY_URI} || 'http://moby.ucalgary.ca/MOBY/Central'; | ||
| 58 | |||
| 59 | # The directory to store the job details | ||
| 60 | my $CONF = MOBY::Config->new; | ||
| 61 | my $DIRECTORY = $CONF->{mobycentral}->{service_tester_path} || '/tmp/'; | ||
| 62 | |||
| 63 | # hashes whose key is the service provider and the value is an array of service names | ||
| 64 | my %ALIVE = (); | ||
| 65 | my $FILENAME = 'isAliveStats.xml'; | ||
| 66 | |||
| 67 | # this is just to test if I'm going to have permissions | ||
| 68 | # right now, rather than an hour from now... Arghghg! | ||
| 69 | open( OUT, ">>$DIRECTORY/$FILENAME" ) || die("Cannot Open File '$DIRECTORY/$FILENAME' $!"); | ||
| 70 | close OUT; | ||
| 71 | |||
| 72 | # create some shared variables | ||
| 73 | my $alive_handle = tie %ALIVE, 'IPC::Shareable', undef, { } or die "\n\tCouldn't tie shared variable:\n$!"; | ||
| 74 | |||
| 75 | # create the central client and get all service providers once | ||
| 76 | my $central = | ||
| 77 | MOBY::Client::Central->new( | ||
| 78 | Registries => { | ||
| 79 | mobycentral => { | ||
| 80 | URL => $URL, | ||
| 81 | URI => $URI | ||
| 82 | } | ||
| 83 | } | ||
| 84 | ); | ||
| 85 | my @providers = $central->retrieveServiceProviders(); | ||
| 86 | |||
| 87 | foreach my $cat (@CATEGORIES) { | ||
| 88 | foreach my $authURI (@providers) { | ||
| 89 | my ( $second, $minute, $hour, @whatever ) = localtime(); | ||
| 90 | $hour = "0$hour" if $hour <= 9; | ||
| 91 | $second = "0$second" if $second <= 9; | ||
| 92 | $minute = "0$minute" if $minute <= 9; | ||
| 93 | |||
| 94 | print "Finding services registered by '$authURI' as '$cat' @ $hour:$minute:$second\n"; | ||
| 95 | my ( $services, $reg ) = $central->findService( Registry => "mobycentral",category => $cat, authURI => $authURI ); | ||
| 96 | ( $second, $minute, $hour, @whatever ) = localtime(); | ||
| 97 | $hour = "0$hour" if $hour <= 9; | ||
| 98 | $second = "0$second" if $second <= 9; | ||
| 99 | $minute = "0$minute" if $minute <= 9; | ||
| 100 | print "Services found " | ||
| 101 | . scalar @$services | ||
| 102 | . "... processing @ $hour:$minute:$second \n"; | ||
| 103 | |||
| 104 | my $count = 0; | ||
| 105 | print "\tservice count: " . scalar (@$services) . "\n"; | ||
| 106 | foreach (@$services) { | ||
| 107 | # ignore test services | ||
| 108 | next if $_->authority eq 'samples.jmoby.net'; | ||
| 109 | wait, $count-- while $count >= $THREAD_COUNT; | ||
| 110 | $count++; | ||
| 111 | my $pid = fork(); | ||
| 112 | $count-- unless defined $pid; | ||
| 113 | do { IPC::Shareable->clean_up_all; die "Couldn't fork: $!"; } | ||
| 114 | unless defined $pid; | ||
| 115 | if ($pid) { | ||
| 116 | # parent - do nothing ...s | ||
| 117 | } elsif ( $pid == 0 ) { | ||
| 118 | my $name = $_->name; | ||
| 119 | my $auth = $_->authority; | ||
| 120 | my $url = $_->URL; | ||
| 121 | |||
| 122 | do { | ||
| 123 | # dont process localhost addresses ... | ||
| 124 | exit(0); | ||
| 125 | } if $url =~ /localhost/; | ||
| 126 | |||
| 127 | # child - stuff to do goes here | ||
| 128 | my $out = undef; | ||
| 129 | my $input = _empty_input(); | ||
| 130 | |||
| 131 | #print "Calling: " . $auth . "," . $name . "\n"; | ||
| 132 | do { | ||
| 133 | my $soap = | ||
| 134 | SOAP::Lite->uri("http://biomoby.org/") | ||
| 135 | ->proxy( $url, timeout => $TIMEOUT )->on_fault( | ||
| 136 | sub { | ||
| 137 | my $soap = shift; | ||
| 138 | my $res = shift; | ||
| 139 | |||
| 140 | #TODO add to DEAD hash ... | ||
| 141 | $alive_handle->shlock(); | ||
| 142 | $ALIVE{$auth} = () if not exists $ALIVE{$auth}; | ||
| 143 | push @{ $ALIVE{$auth} }, {name=>$name, alive=>undef}; | ||
| 144 | $alive_handle->shunlock(); | ||
| 145 | |||
| 146 | #print "\t" . $auth . "," . $name . " ~isAlive\n"; | ||
| 147 | exit(0); | ||
| 148 | } | ||
| 149 | ); | ||
| 150 | |||
| 151 | $out = | ||
| 152 | $soap->$name( SOAP::Data->type( 'string' => "$input" ) )->result; | ||
| 153 | } unless $cat eq 'cgi'; | ||
| 154 | # test cgi services | ||
| 155 | do { | ||
| 156 | my $ua = LWP::UserAgent->new; | ||
| 157 | $ua->timeout($TIMEOUT); | ||
| 158 | my $req = POST $url, [ data => $input]; | ||
| 159 | $req = $ua->request($req); | ||
| 160 | $out = $req->content if $req->is_success; | ||
| 161 | } if $cat eq 'cgi'; | ||
| 162 | |||
| 163 | do { | ||
| 164 | #TODO add to ALIVE hash ... | ||
| 165 | #print "\t" . $auth . "," . $name . " isAlive\n"; | ||
| 166 | $alive_handle->shlock(); | ||
| 167 | $ALIVE{$auth} = () if not exists $ALIVE{$auth}; | ||
| 168 | push @{ $ALIVE{$auth} }, {name=>$name, alive=>1}; | ||
| 169 | $alive_handle->shunlock(); | ||
| 170 | exit(0); | ||
| 171 | } if $out; | ||
| 172 | do { | ||
| 173 | #TODO add to DEAD hash ... | ||
| 174 | #print "\t" . $auth . "," . $name . " ~isAlive\n"; | ||
| 175 | $alive_handle->shlock(); | ||
| 176 | $ALIVE{$auth} = () if not exists $ALIVE{$auth}; | ||
| 177 | push @{ $ALIVE{$auth} }, {name=>$name, alive=>undef}; | ||
| 178 | $alive_handle->shunlock(); | ||
| 179 | exit(0); | ||
| 180 | } unless $out; | ||
| 181 | } else { | ||
| 182 | IPC::Shareable->clean_up_all; | ||
| 183 | die "couldnt fork: $!\n"; | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | # dont proceed until we are completed with the first batch of children! | ||
| 188 | wait, $count-- while $count > 0; | ||
| 189 | ( $second, $minute, $hour, @whatever ) = localtime(); | ||
| 190 | $hour = "0$hour" if $hour <= 9; | ||
| 191 | $second = "0$second" if $second <= 9; | ||
| 192 | $minute = "0$minute" if $minute <= 9; | ||
| 193 | print "Testing of '$cat' services from '$authURI' completed @ $hour:$minute:$second \n"; | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | my $doc = XML::LibXML::Document->new( "1.0", "UTF-8" ); | ||
| 198 | my $root = $doc->createElement('Services'); | ||
| 199 | $doc->setDocumentElement($root); | ||
| 200 | |||
| 201 | for my $auth ( sort keys %ALIVE ) { | ||
| 202 | my $element = $doc->createElement('authority'); | ||
| 203 | $element->setAttribute( 'id', $auth ); | ||
| 204 | my @services = @{$ALIVE{$auth}}; | ||
| 205 | next unless @services; | ||
| 206 | foreach my $s (@services) { | ||
| 207 | next unless $s; | ||
| 208 | my $child = $doc->createElement('service'); | ||
| 209 | $child->setAttribute( 'id', $auth . ',' . $s->{name} ); | ||
| 210 | $child->appendText(($s->{alive} ? 'true' : 'false')); | ||
| 211 | $element->appendChild($child); | ||
| 212 | } | ||
| 213 | $root->appendChild($element); | ||
| 214 | } | ||
| 215 | |||
| 216 | # clean up the shared memory | ||
| 217 | eval { | ||
| 218 | warn "Attempting to clean up shared memory ..."; | ||
| 219 | IPC::Shareable->clean_up_all; | ||
| 220 | warn "Shared memory cleaned up successfully"; | ||
| 221 | }; | ||
| 222 | |||
| 223 | warn "There was a problem cleaning up shared memory segments:\n$@\n" if $@; | ||
| 224 | |||
| 225 | open( OUT, ">$DIRECTORY/$FILENAME" ) || die("Cannot Open File $DIRECTORY/$FILENAME $!"); | ||
| 226 | print OUT $doc->toString(1); | ||
| 227 | close OUT; | ||
| 228 | |||
| 229 | sub _empty_input { | ||
| 230 | return <<'END_OF_XML'; | ||
| 231 | <?xml version="1.0" encoding="UTF-8"?> | ||
| 232 | <moby:MOBY xmlns:moby="http://www.biomoby.org/moby"> | ||
| 233 | <moby:mobyContent> | ||
| 234 | </moby:mobyContent> | ||
| 235 | </moby:MOBY> | ||
| 236 | END_OF_XML | ||
| 237 | } |