Koha/sms/sms_listen.pl
Jonathan Druart 4ae3665ad8 Bug 10611: Use mysql_auto_reconnect instead of ping
DBD::Mysql provides a mysql_auto_reconnect flag. Using it avoids
the time required to do a $dbh->ping().

Benchmarks:

use Modern::Perl;
use C4::Context;
for ( 1 .. 1000 ) {
    $dbh = C4::Context->dbh;
}

* without this patch on a local DB:
perl t.pl  0,49s user 0,02s system 98% cpu 0,525 total
* without this patch on a remote DB:
perl t.pl  0,52s user 0,05s system 1% cpu 37,358 total
* with this patch on a local DB:
perl t.pl  0,46s user 0,04s system 99% cpu 0,509 total
* with this patch on a remote DB:
perl t.pl  0,49s user 0,02s system 56% cpu 0,892 total

Testing the auto reconnect:
use Modern::Perl;
use C4::Context;
my $ping = $dbh->ping;
say $ping;
$dbh->disconnect;
$ping = $dbh->ping;
say $ping;

Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com>
Comment: Real improvement. No koha-qa errors

prove t/db_dependent/Circulation_issuingrules.t produces no error
prove t/db_dependent/Context.t produces no error

Test
1) dumped Koha DB, load it on a non-local server
2) run sample script whit and without patch, local and remote

use Modern::Perl;
use C4::Context;
for ( 1 .. 100000 ) {
    my $dbh = C4::Context->dbh;
}

Main difference I note is with remote server
a) without patch
real    0m16.357s
user    0m2.592s
sys     0m2.132s

b) with patch
real    0m0.259s
user    0m0.240s
sys     0m0.012s

I think this could be good for DBs placed on
remote servers

Bug 10611: add a "new" parameter to C4::Context->dbh

When dbh->disconnect is called and the mysql_auto_reconnect flag is set,
the dbh is not recreated: the old one is used.

Adding a new flag, we can now force the C4::Context->dbh method to
return a new dbh.

Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com>

Bug 10611: Followup: remove useless calls to dbh->disconnect

These 3 calls to disconnect are done at the end of the script, they are
useless.

Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com>
Signed-off-by: Paul Poulain <paul.poulain@biblibre.com>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>
2014-03-10 23:15:35 +00:00

125 lines
3.2 KiB
Perl
Executable file

#!/usr/bin/perl
use strict;
use warnings;
use C4::SMS;
use C4::Auth;
use C4::Context;
use C4::Members;
use C4::Circulation;
my ($res,$ua);
my %commands;
my $message;
my $phone;
my $result;
my $errorcode;
my $smsid;
my $wait=600;## 10 mn. wait between sms checking
my $dbh=C4::Context->dbh;
STARTAGAIN:
($res,$ua)=get_sms_auth();
AGAIN:
$errorcode=0;
if ($res->{pRetCode}==200){
$result=read_sms($ua,$res->{pSessionId});
$errorcode=$result->{pErrCode};
print "connected\n";
} else {
kill_sms($ua,$res->{pSessionId});
warn (error_codes($res->{pErrCode}),$res->{pErrcode}) ;
# sleep $wait;
goto FINISH;
}
if ($errorcode && $errorcode !=-9005){
kill_sms($ua,$res->{pSessionId});
warn error_codes($errorcode) ;
# sleep $wait;
goto FINISH;
} elsif ($errorcode ==-9005){
print "no more messages to read\n";
goto WAITING;
}
#Parse the message to a useful hash
my @action=parse_message( $result->{pContent});
## Log the request in our database;
$smsid=write_sms($action[1], $result->{pContent},$result->{pMsisdn});
print "message logged\n";
##Now do the service required
if (uc($action[0]) eq "RN"){
print "dealing request\n";
my ($ok,$cardnumber)=C4::Auth::checkpw($dbh,$action[1],$action[2]);
unless ($ok) {
##wrong user/pass
$message="Yanlis kullanici/sifre! :Wrong username/password!";
my $send=send_message($result,$message,$smsid);
goto AGAIN;
}
my $item=getiteminformation(undef,0,$action[3]);
if ($item){
my $borrower=getmember($cardnumber);
my $status=renewstatus(undef,$borrower->{borrowernumber},$item->{itemnumber});
if ($status==1) {
my $date=renewbook(undef,$borrower->{borrowernumber},$item->{itemnumber});
$message="Uzatildi :Renewed ".$item->{barcode}." : ".$date;
} elsif($status==2) {
$message="Cok erken- yenilenmedi! :Too early-not renewed:".$item->{barcode};
} elsif($status==3) {
$message="Uzatamazsiniz GERI getiriniz! :No more renewals RETURN the item:".$item->{barcode};
} elsif($status==4) {
$message="Ayirtildi GERI getiriniz! :Reserved RETURN the item:".$item->{barcode};
} elsif($status==0) {
$message="Uzatilamaz! :Can not renew:".$item->{barcode};
}
} else {
$message="Yanlis barkot! :Wrong barcode!";
}
} else {
## reply about error
$message="Yanlis mesaj formati! :Wrong message! :
RN usercardno password barcode";
} ### wrong service
send_message($result,$message,$smsid);
goto AGAIN;
WAITING:
##Now send the messages waiting in queue
my $smssth=$dbh->prepare("SELECT smsid,user_phone,message from sms_messages where date_replied like '0000-00-00%' ");
$smssth->execute();
my @phones;
while (my $data=$smssth->fetchrow_hashref){
push @phones,$data;
}
$smssth->finish;
foreach my $user(@phones){
print "replying $user->{user_phone}";
my $send=send_sms($ua,$user->{user_phone},$user->{message},$res->{pSessionId});
my $reply="--failed\n";
if ($send->{pRetCode}==200){
$reply= "--replied\n";
mod_sms($user->{smsid},"Sent");
}
print $reply;
}
sub send_message {
my ($mes,$message,$smsid)=@_;
my $send=send_sms($ua,$mes->{pMsisdn},$message,$res->{pSessionId});
if ($send->{pRetCode}==200){
mod_sms($smsid,$message);
} else {
my $error=error_codes($send->{pErrCode});
mod_sms($smsid,"Not replied error:".$error);
}
return $send;
}
FINISH:
1;
__END__