Андрей (andy) wrote in changelog,
Андрей
andy
changelog

[TheSchwartz] r158: LJSUP-12405 (make schwartzmon and schwar...

Committer: ailyin
LJSUP-12405 (make schwartzmon and schwartz-rate work on dev servers)
U   trunk/bin/schwartzmon
Modified: trunk/bin/schwartzmon
===================================================================
--- trunk/bin/schwartzmon	2012-02-27 13:48:22 UTC (rev 157)
+++ trunk/bin/schwartzmon	2012-05-30 10:38:09 UTC (rev 158)
@@ -112,15 +112,17 @@
     $dbs->foreach(sub {
         my $db = shift;
         my $dbh = $db->dbh or next;
+        my $prefix = $db->table_prefix;
 
-        my $funcmap = $dbh->selectall_hashref("SELECT funcid, funcname FROM funcmap", "funcid");
+        my $funcmap = $dbh->selectall_hashref(
+            "SELECT funcid, funcname FROM ${prefix}funcmap", "funcid");
 
         foreach my $funcid (sort { $funcmap->{$a}{funcname} cmp $funcmap->{$b}{funcname} } keys %$funcmap) {
             my $funcname = $funcmap->{$funcid}{funcname};
             next if $job && $funcname ne $job;
 
             my $now = time();
-            my $inf = $dbh->selectrow_hashref("SELECT COUNT(*) as 'ct', MIN(run_after) 'oldest' FROM job WHERE funcid=? AND run_after <= $now",
+            my $inf = $dbh->selectrow_hashref("SELECT COUNT(*) as 'ct', MIN(run_after) 'oldest' FROM ${prefix}job WHERE funcid=? AND run_after <= $now",
                                               undef, $funcid);
             my $behind = $inf->{ct} ? ($now - $inf->{oldest}) : 0;
 
@@ -153,8 +155,10 @@
         my $db = shift;
         my $dbh = $db->dbh
             or next;
+        my $prefix = $db->table_prefix;
 
-        my $funcmap = $dbh->selectall_hashref("SELECT funcid, funcname FROM funcmap", "funcid");
+        my $funcmap = $dbh->selectall_hashref(
+            "SELECT funcid, funcname FROM ${prefix}funcmap", "funcid");
 
         my $extra_where = '';
         if ($opt_func) {
@@ -164,11 +168,13 @@
 
         my $sql;
         if ($opt_last) {
-            $sql = "SELECT error_time, jobid, message, funcid FROM error WHERE 1=1 $extra_where " .
+            $sql = "SELECT error_time, jobid, message, funcid " .
+                "FROM ${prefix}error WHERE 1=1 $extra_where " .
                 "ORDER BY error_time DESC LIMIT $opt_last";
         } elsif ($opt_inlast) {
             my $since = time() - $opt_inlast;
-            $sql = "SELECT error_time, jobid, message, funcid FROM error WHERE error_time >= $since $extra_where " .
+            $sql = "SELECT error_time, jobid, message, funcid " .
+                "FROM ${prefix}error WHERE error_time >= $since $extra_where " .
                 "ORDER BY error_time LIMIT 50000";
         }
 
@@ -201,6 +207,7 @@
             my $dbh = $db->dbh
                 or next;
             my $notes = $db->notes;
+            my $prefix = $db->table_prefix;
 
             my $lastmax = $notes->{lastmax} || time();
             my $seen    = $notes->{seen}    ||= {};
@@ -211,9 +218,13 @@
                 $extra_where = "AND funcid=$funcid";
             }
 
-            my $funcmap = $dbh->selectall_hashref("SELECT funcid, funcname FROM funcmap", "funcid");
+            my $funcmap = $dbh->selectall_hashref(
+                "SELECT funcid, funcname FROM ${prefix}funcmap", "funcid");
 
-            my $sth = $dbh->prepare("SELECT error_time, jobid, message, funcid FROM error WHERE error_time >= ? $extra_where ORDER BY error_time");
+            my $sth = $dbh->prepare(
+                "SELECT error_time, jobid, message, funcid " .
+                "FROM ${prefix}error " .
+                "WHERE error_time >= ? $extra_where ORDER BY error_time" );
             $sth->execute($lastmax);
             my @errors;
             push @errors, $_ while $_ = $sth->fetchrow_hashref;
@@ -260,15 +271,7 @@
         my $db = shift;
         my $dbh = $db->dbh or next;
 
-        ## On dev servers TheSchwartz tables have 'sch_' prefix. On production they don't.
-        my $prefix = '';
-        # show tables like 'sch_%';
-        my $tables = $dbh->selectall_arrayref("show tables like 'sch_%'");
-        if (grep { $_->[0] =~ /^(sch_job|sch_funcmap)$/} @$tables and not  ## there are dev tables
-            grep { $_->[0] =~ /^(job|funcmap)$/ } @$tables                 ## and no production tables
-        ){
-            $prefix = "sch_";
-        }
+        my $prefix = $db->table_prefix;
 
         ## func -> funcid
         my ($funcid) = $dbh->selectrow_array("SELECT funcid FROM ${prefix}funcmap WHERE funcname=?", undef, $opt_func);
@@ -324,14 +327,39 @@
 
 }
 
+# On dev servers TheSchwartz tables have 'sch_' prefix. On production
+# they don't. The detection logic is as follows: if we find 'dev' tables
+# but not 'production' tables; otherwise, 'production' tables are used
+sub table_prefix {
+    my ($self) = @_;
+
+    unless ( defined $self->{'_table_prefix'} ) {
+        my $tables = $self->dbh->selectcol_arrayref('SHOW TABLES');
+        my %table_present = map { $_ => 1 } @$tables;
+
+        if ( $table_present{'sch_job'} && $table_present{'sch_funcmap'} &&
+            ! $table_present{'job'} && ! $table_present{'funcmap'} )
+        {
+            $self->{'_table_prefix'} = 'sch_';
+        } else {
+            $self->{'_table_prefix'} = '';
+        }
+    }
+
+    return $self->{'_table_prefix'};
+}
+
 sub funcid_of_func {
     my ($self, $func) = @_;
     my $notes = $self->notes;
     return $notes->{"funcid_of_$func"} if exists $notes->{"funcid_of_$func"};
 
     my $dbh = $self->dbh;
+    my $prefix = $self->table_prefix;
+
     return $notes->{"funcid_of_$func"} =
-        $dbh->selectrow_array("SELECT funcid FROM funcmap WHERE funcname=?",
-                              undef, $func);
+        $dbh->selectrow_array(
+            "SELECT funcid FROM ${prefix}funcmap WHERE funcname=?",
+            undef, $func );
 }
 

Tags: ailyin, andy, theschwartz
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments