X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2Fbin%2Ffreeside-torrus-srvderive;h=525d6ceb5194c30d62580671f6ceecca505e93bb;hb=144668a01beef406a027c78bc2883e111e621cc2;hp=f95029a5cfa20c36498aabebe394adaa0249070b;hpb=297bec469bf8c9257b58c57aabbcbd11a87e47c7;p=freeside.git diff --git a/FS/bin/freeside-torrus-srvderive b/FS/bin/freeside-torrus-srvderive index f95029a5c..525d6ceb5 100644 --- a/FS/bin/freeside-torrus-srvderive +++ b/FS/bin/freeside-torrus-srvderive @@ -6,12 +6,13 @@ use Sys::SigAction qw( set_sig_handler ); use Date::Parse; use Date::Format; use FS::Daemon ':all'; #daemonize1 drop_root daemonize2 myexit logfile sig* -use FS::UID qw( adminsuidsetup dbh driver_name ); +use FS::UID qw( adminsuidsetup forksuidsetup dbh driver_name ); use FS::Record qw( qsearch str2time_sql str2time_sql_closing concat_sql ); use FS::torrus_srvderive; our $DEBUG = 2; -our $max_kids = 3; +our $max_kids = 6; +our $search_timeout = 30*60; # 15*60 5*60; #$torrus_srvderive->last_srv_date ? 5*60 : 15*60); our %kids; my $user = shift or die &usage; @@ -62,17 +63,25 @@ my $orderlimit = " LIMIT 50 "; +if ( driver_name eq 'mysql' ) { + dbh->do('SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED'); + dbh->commit or die dbh->errstr; +} + our $kids = 0; -MAIN: while (1) { +#MAIN: while (1) { +while (1) { my $found = 0; - SERVICEID: foreach my $torrus_srvderive ( qsearch('torrus_srvderive', {}) ) { + #SERVICEID: foreach my $torrus_srvderive ( qsearch('torrus_srvderive', {}) ) { + foreach my $torrus_srvderive ( qsearch('torrus_srvderive', {}) ) { &reap_kids; if ( $kids >= $max_kids ) { - sleep 10; + sleep 5; + myexit() if sigterm() || sigint(); redo; } @@ -95,7 +104,13 @@ MAIN: while (1) { my $serviceid = $torrus_srvderive->serviceid; my @serviceids = $torrus_srvderive->component_serviceids; - next unless @serviceids; #don't try to search for empty virtual ports + exit unless @serviceids; #don't try to search for empty virtual ports + + #nonlocking select statements; rows in this table never change + if ( driver_name eq 'mysql' ) { + dbh->do('SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED'); + dbh->commit or die dbh->errstr; + } my @in = (); for my $dir ('_IN', '_OUT') { @@ -118,7 +133,7 @@ MAIN: while (1) { die $error if $error; } else { warn "no initial last_srv_date for $serviceid; skipping\n" if $DEBUG; - next; + exit; } } @@ -142,24 +157,26 @@ MAIN: while (1) { eval { my $h = set_sig_handler( 'ALRM', sub { die "_timeout\n"; } ); - alarm(10*60); #5*60); #$torrus_srvderive->last_srv_date ? 5*60 : 15*60); + alarm($search_timeout); $sth->execute($serviceid, $serviceid) or die $sth->errstr; alarm(0); }; alarm(0); if ( $@ && $@ eq "_timeout\n" ) { - warn "search timed out; reconnecting and restarting\n"; + #warn "search timed out; reconnecting and restarting\n"; + warn "search timed out\n"; dbh->clone()->do("KILL QUERY ". dbh->{"mysql_thread_id"}) if driver_name eq 'mysql'; dbh->rollback; #or die dbh->errstr; - adminsuidsetup($user); - next SERVICEID; #MAIN; + #adminsuidsetup($user); + #next SERVICEID; #MAIN; + exit; } elsif ( $@ ) { die $@; } - warn "search finished; checking results\n" if $DEBUG; + warn "search for $serviceid finished; checking results\n" if $DEBUG; my $prev = 0; while ( my $row = $sth->fetchrow_arrayref ) { @@ -178,9 +195,12 @@ MAIN: while (1) { my $sin = join(',', map dbh->quote("$_$dir"), @serviceids); + my $srv_date = time2str('%Y-%m-%d', $cur); + my $sum = " SELECT COALESCE(SUM(value),0) FROM srvexport AS other WHERE other.serviceid IN ($sin) + AND other.srv_date = '$srv_date' AND ABS( $cur - $other_date ) <= 60 "; @@ -188,10 +208,10 @@ MAIN: while (1) { INSERT INTO srvexport ( srv_date, srv_time, serviceid, value, intvl ) VALUES ( ?, ?, ?, ($sum), ? ) "; - my @param = ( time2str('%Y-%m-%d', $cur), #srv_date - time2str('%X', $cur), #srv_time - "$serviceid$dir", - 300, #intvl ... + my @param = ( $srv_date, + time2str('%X', $cur), #srv_time + "$serviceid$dir", #serviceid + 300, #intvl ... ); warn $isql. ' with param '. join(',',@param). "\n" if $DEBUG > 2; @@ -200,11 +220,13 @@ MAIN: while (1) { #stupid mysql deadlocks all the time on insert, so we need to recover unless ( $isth->execute(@param) ) { - warn "Error inserting data for $serviceid$dir (restarting): ". - $isth->errstr; + #warn "Error inserting data for $serviceid$dir (restarting): ". + # $isth->errstr; + warn "Error inserting data for $serviceid$dir: ". $isth->errstr; dbh->rollback; #or die dbh->errstr; - sleep 5; - next SERVICEID; #MAIN; + #sleep 5; + #next SERVICEID; #MAIN; + exit; } } @@ -230,7 +252,7 @@ MAIN: while (1) { myexit() if sigterm() || sigint(); warn "restarting main loop\n" if $DEBUG > 1; - sleep 60 unless $found; + #sleep 60 unless $found; } sub _shouldrun {