fully automate coordinate lookups, #23040
authorMark Wells <mark@freeside.biz>
Wed, 22 May 2013 23:58:31 +0000 (16:58 -0700)
committerMark Wells <mark@freeside.biz>
Wed, 22 May 2013 23:58:31 +0000 (16:58 -0700)
FS/FS/Cron/cleanup.pm [new file with mode: 0644]
FS/FS/cust_location.pm
FS/bin/freeside-daily

diff --git a/FS/FS/Cron/cleanup.pm b/FS/FS/Cron/cleanup.pm
new file mode 100644 (file)
index 0000000..4c5cff2
--- /dev/null
@@ -0,0 +1,18 @@
+package FS::Cron::cleanup;
+use base 'Exporter';
+use vars '@EXPORT_OK';
+use FS::queue;
+
+@EXPORT_OK = qw( cleanup );
+
+# start janitor jobs
+sub cleanup {
+# fix locations that are missing coordinates
+  my $job = FS::queue->new({
+      'job'     => 'FS::cust_location::process_set_coord',
+      'status'  => 'new'
+  });
+  $job->insert('_JOB');
+}
+
+1;
index 1cb5e52..d772dab 100644 (file)
@@ -322,6 +322,11 @@ sub check {
       } );
   }
 
+  # set coordinates, unless we already have them
+  if (!$import and !$self->latitude and !$self->longitude) {
+    $self->set_coord;
+  }
+
   $self->SUPER::check;
 }
 
@@ -638,6 +643,44 @@ sub in_county_sql {
   }
 }
 
+sub process_set_coord {
+  my $job = shift;
+  # avoid starting multiple instances of this job
+  my @others = qsearch('queue', {
+      'status'  => 'locked',
+      'job'     => $job->job,
+      'jobnum'  => {op=>'!=', value=>$job->jobnum},
+  });
+  return if @others;
+
+  $job->update_statustext('finding locations to update');
+  my @missing_coords = qsearch('cust_location', {
+      'disabled'  => '',
+      'latitude'  => '',
+      'longitude' => '',
+  });
+  my $i = 0;
+  my $n = scalar @missing_coords;
+  for my $cust_location (@missing_coords) {
+    $cust_location->set_coord;
+    my $error = $cust_location->replace;
+    if ( $error ) {
+      warn "error geocoding location#".$cust_location->locationnum.": $error\n";
+    } else {
+      $i++;
+      $job->update_statustext("updated $i / $n locations");
+      dbh->commit; # so that we don't have to wait for the whole thing to finish
+      # Rate-limit to stay under the Google Maps usage limit (2500/day).
+      # 86,400 / 35 = 2,468 lookups per day.
+    }
+    sleep 35;
+  }
+  if ( $i < $n ) {
+    die "failed to update ".$n-$i." locations\n";
+  }
+  return;
+}
+
 =head1 BUGS
 
 =head1 SEE ALSO
index 0e43276..b6ee518 100755 (executable)
@@ -77,6 +77,10 @@ unlink <${deldir}.CGItemp*>;
 use FS::Cron::backup qw(backup);
 backup();
 
+#except we'd rather not start cleanup jobs until the backup is done
+use FS::Cron::cleanup qw(cleanup);
+cleanup();
+
 $log->info('finish');
 
 ###