Guest User

Untitled

a guest
Dec 2nd, 2017
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.69 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. package Dispatch;
  4. use Data::Dumper;
  5. use MIME::Base64 qw(decode_base64);
  6. use Crypt::CBC;
  7. use Crypt::Cipher::AES;
  8. use strict;
  9. use Redis;
  10. use DBI;
  11. use POSIX qw(:signal_h :errno_h :fcntl_h);
  12. use Time::HiRes qw(  gettimeofday tv_interval );
  13.  
  14. sub dbtime {
  15.     my($r)=@_;
  16.     $r->send_http_header('text/plain');
  17.     $gl::dbh{$$} ||= DBI->connect("dbi:mysql:test:localhost:3306","root","") || die;
  18.     my $dbh =  $gl::dbh{$$};
  19.  
  20.     my $t = $dbh->selectall_arrayref("select now()");
  21.     $r->print(Dumper($t));
  22.     return 'OK';
  23. }
  24.  
  25.  
  26. sub set_actions {
  27.     $gl::mask{$$} ||= POSIX::SigSet->new( SIGALRM );
  28.     $gl::action{$$} ||= POSIX::SigAction->new( sub { die "Query timeout"; }, $gl::mask{$$} );
  29.     $gl::oldaction{$$} ||= POSIX::SigAction->new();
  30.     sigaction('ALRM', $gl::action{$$}, $gl::oldaction{$$} );
  31.     return 1;
  32. }
  33.  
  34.  
  35. sub dbtimeout {
  36.     my($r)=@_;
  37.     $r->send_http_header('text/plain');
  38.     set_actions();
  39.  
  40.  
  41.     $gl::dbh{$$} ||= DBI->connect("dbi:mysql:test:localhost:3306","root","") || die;
  42.     my $dbh =  $gl::dbh{$$};
  43.  
  44.     my $sql = "select sleep(20)";
  45.     my $sth = $dbh->prepare($sql);
  46.     eval {  
  47.     alarm 5;
  48. #    $SIG{ALRM}= sub { die "Mar gaya\n"; } ;
  49.     $sth->execute() || die $dbh->errstr;
  50.     alarm(0);
  51.     };
  52.     alarm(0);
  53.     sigaction('ALRM', $gl::oldaction{$$});
  54.     if($@) {
  55.     $r->print("Exit  due to Alarm \n '$@'\n");
  56.     my $t0 = [gettimeofday];
  57.     $dbh->clone()->do("KILL QUERY ".$dbh->{"mysql_thread_id"});
  58.     my $elapsed = tv_interval ( $t0 );
  59.     $r->print("After Cancel  due to Alarm \n Elapsed = <$elapsed> \n");
  60.     } else {
  61.     $r->print("Query Done\n");
  62.     }
  63.     dbtime($r);
  64.     $r->print("Bye \n");
  65.     return 'OK';
  66. }
Add Comment
Please, Sign In to add comment