Index: trunk/DNSDB.pm
===================================================================
--- trunk/DNSDB.pm	(revision 763)
+++ trunk/DNSDB.pm	(revision 764)
@@ -1399,23 +1399,9 @@
   my ($iplist) = $self->{dbh}->selectrow_array("SELECT auxdata FROM records WHERE record_id = ?", undef, $args{recid});
   my $warnmsg;
-
-  my $res = Net::DNS::Resolver->new;
-  # Set short timeouts to minimize disruption.  If the target's DNS is slow the site will likely be broken anyway.
-  $res->tcp_timeout(2);
-  $res->udp_timeout(2);
-  my $reply = $res->query(${$args{val}});
-  my @newlist;
-  if ($reply) { 
-    foreach my $rr ($reply->answer) {
-      next unless $rr->type eq "A";
-      push @newlist, $rr->address;
-    }
-  } else {
-    $warnmsg = "Failure retrieving IP list from DNS for cache validation/update on ALIAS '${$args{host}} -> ${$args{val}}': ".
-	$res->errorstring;
-  }
-
-  # we don't need this to be perfectly correct IP address order, just consistent.
-  my $liveips = join(':', sort(@newlist));
+  $iplist = '' if !$iplist;
+
+  # shared target-name-to-IP converter
+  my $liveips = $self->_grab_65300($args{recid}, ${$args{val}});
+  $liveips = '' if !$liveips;
 
   # check to see if there was an OOOOPS checking for updated A records on the target.  also make sure we have something cached.
@@ -1424,7 +1410,5 @@
       # not fatal since we do the lookup on export as well
       return ('WARN',
-        join("\n", $warnmsg, "No cached data and no live DNS data for ALIAS target ${$args{val}};  record may be SKIPPED on export!") );
-#    } else {
-#      return ('WARN', "No live DNS data for ALIAS target ${$args{val}};  falling back to cache");
+        join("\n", $errstr, "No cached data and no live DNS data for ALIAS target ${$args{val}};  record may be SKIPPED on export!") );
     }
   }
@@ -1440,4 +1424,40 @@
   return ('OK','OK');
 } # done ALIAS record
+
+# this segment used multiple places to update ALIAS target details
+sub _grab_65300 {
+  my $self = shift;
+  my $dbh = $self->{dbh};
+
+  my $recid = shift;
+  my $target = shift;
+
+  my $res = Net::DNS::Resolver->new;
+  $res->tcp_timeout(2);
+  $res->udp_timeout(2);
+  my $reply = $res->query($target);
+
+  my $liveips;
+  if ($reply) {
+    # default to a one-hour TTL, which should be variously modified down the chain.  Arguably this could
+    # default even lower, since "The Cloud" often uses sub-1-minute TTLs on the final A records.
+    my $minttl = 3600;
+    my @newlist;
+    foreach my $rr ($reply->answer) {  #@alist) {
+      next unless $rr->type eq "A";
+      push @newlist, $rr->address;
+      $minttl = $rr->ttl if $rr->ttl < $minttl;
+    }
+    # safety limit.  could arguably take this lower, or for extra
+    # complexity, reference off the zone SOA minTTL
+    $minttl = 60 if $minttl < 60;
+    # we don't need this to be perfectly correct IP address order, just consistent.
+    $liveips = "$minttl:".join(':', sort(@newlist));
+  } else {
+    $errstr = "Lookup failure retrieving ALIAS IP list: ".$res->errorstring;
+  }
+
+  return $liveips;
+} # _grab_65300()
 
 
@@ -6740,28 +6760,20 @@
 
     my ($iplist) = $self->{dbh}->selectrow_array("SELECT auxdata FROM records WHERE record_id = ?", undef, $recid);
-    my $res = Net::DNS::Resolver->new;
-    my $reply = $res->query($val);
-
-    if ($reply) {
-      my $liveips;
-      my @newlist;
-      foreach my $rr ($reply->answer) {  #@alist) {
-        next unless $rr->type eq "A";
-        push @newlist, $rr->address;
-      }
-      # we don't need this to be perfectly correct IP address order, just consistent.
-      $liveips = join(':', sort(@newlist));
-      if ($iplist ne $liveips) {
-        # update the cache of IPs from the target
-        $self->{dbh}->do("UPDATE records SET auxdata = ? WHERE record_id = ?", undef, $liveips, $recid);
-        $iplist = $liveips;
-      }
-    } else {
-      warn "Failure retrieving IP list for cache validation/update on ALIAS '$host -> $val': ", $res->errorstring, "\n";
-    }
+
+    # shared target-name-to-IP converter
+    my $liveips = $self->_grab_65300($recid, $val);
+    if ($iplist ne $liveips) {
+      $self->{dbh}->do("UPDATE records SET auxdata = ? WHERE record_id = ?", undef, $liveips, $recid);
+      $iplist = $liveips;
+    }
+
+    # slice the TTL we'll actually publish off the front
+    my @asubs = split ':', $iplist;
+    my $attl = shift @asubs;
 
     # output a plain old A record for each IP the target name really points to.
-    foreach my $subip (split ':', $iplist) {
-      print "+$host:$subip:$ttl:$stamp:$loc\n" or die $!;
+    # in the event that, for whatever reason, no A records are available for $val, nothing will be output.
+    foreach my $subip (@asubs) {
+      print $datafile "+$host:$subip:$attl:$stamp:$loc\n" or die $!;
     }
 
