[freeside-commits] branch FREESIDE_3_BRANCH updated. 0501f29819775ab5a167ab81bcd9baba45c198ab

Mark Wells mark at 420.am
Wed May 22 16:58:47 PDT 2013


The branch, FREESIDE_3_BRANCH has been updated
       via  0501f29819775ab5a167ab81bcd9baba45c198ab (commit)
      from  a5be7f4e5e7398207636d729a168211842ac706c (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 0501f29819775ab5a167ab81bcd9baba45c198ab
Author: Mark Wells <mark at freeside.biz>
Date:   Wed May 22 16:56:13 2013 -0700

    fully automate coordinate lookups, #23040

diff --git a/FS/FS/Cron/cleanup.pm b/FS/FS/Cron/cleanup.pm
new file mode 100644
index 0000000..4c5cff2
--- /dev/null
+++ b/FS/FS/Cron/cleanup.pm
@@ -0,0 +1,18 @@
+package FS::Cron::cleanup;
+use base 'Exporter';
+use vars '@EXPORT_OK';
+use FS::queue;
+
+ at 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;
diff --git a/FS/FS/cust_location.pm b/FS/FS/cust_location.pm
index 1cb5e52..d772dab 100644
--- a/FS/FS/cust_location.pm
+++ b/FS/FS/cust_location.pm
@@ -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
diff --git a/FS/bin/freeside-daily b/FS/bin/freeside-daily
index ac93aaf..d613258 100755
--- a/FS/bin/freeside-daily
+++ b/FS/bin/freeside-daily
@@ -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');
 
 ###

-----------------------------------------------------------------------

Summary of changes:
 FS/FS/Cron/cleanup.pm  |   18 ++++++++++++++++++
 FS/FS/cust_location.pm |   43 +++++++++++++++++++++++++++++++++++++++++++
 FS/bin/freeside-daily  |    4 ++++
 3 files changed, 65 insertions(+), 0 deletions(-)
 create mode 100644 FS/FS/Cron/cleanup.pm




More information about the freeside-commits mailing list