Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- package Dispatch;
- use Data::Dumper;
- use MIME::Base64 qw(decode_base64);
- use Crypt::CBC;
- use Crypt::Cipher::AES;
- use strict;
- use Redis;
- use DBI;
- use POSIX qw(:signal_h :errno_h :fcntl_h);
- use Time::HiRes qw( gettimeofday tv_interval );
- sub dbtime {
- my($r)=@_;
- $r->send_http_header('text/plain');
- $gl::dbh{$$} ||= DBI->connect("dbi:mysql:test:localhost:3306","root","") || die;
- my $dbh = $gl::dbh{$$};
- my $t = $dbh->selectall_arrayref("select now()");
- $r->print(Dumper($t));
- return 'OK';
- }
- sub set_actions {
- $gl::mask{$$} ||= POSIX::SigSet->new( SIGALRM );
- $gl::action{$$} ||= POSIX::SigAction->new( sub { die "Query timeout"; }, $gl::mask{$$} );
- $gl::oldaction{$$} ||= POSIX::SigAction->new();
- sigaction('ALRM', $gl::action{$$}, $gl::oldaction{$$} );
- return 1;
- }
- sub dbtimeout {
- my($r)=@_;
- $r->send_http_header('text/plain');
- set_actions();
- $gl::dbh{$$} ||= DBI->connect("dbi:mysql:test:localhost:3306","root","") || die;
- my $dbh = $gl::dbh{$$};
- my $sql = "select sleep(20)";
- my $sth = $dbh->prepare($sql);
- eval {
- alarm 5;
- # $SIG{ALRM}= sub { die "Mar gaya\n"; } ;
- $sth->execute() || die $dbh->errstr;
- alarm(0);
- };
- alarm(0);
- sigaction('ALRM', $gl::oldaction{$$});
- if($@) {
- $r->print("Exit due to Alarm \n '$@'\n");
- my $t0 = [gettimeofday];
- $dbh->clone()->do("KILL QUERY ".$dbh->{"mysql_thread_id"});
- my $elapsed = tv_interval ( $t0 );
- $r->print("After Cancel due to Alarm \n Elapsed = <$elapsed> \n");
- } else {
- $r->print("Query Done\n");
- }
- dbtime($r);
- $r->print("Bye \n");
- return 'OK';
- }
Add Comment
Please, Sign In to add comment