[freeside-commits] branch master updated. e6210bad824b2efc0a2d54abe8fc79093e7cc5ed

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


The branch, master has been updated
       via  e6210bad824b2efc0a2d54abe8fc79093e7cc5ed (commit)
      from  2d2fad4dc5654636abf690e0980b851540f64a5b (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 e6210bad824b2efc0a2d54abe8fc79093e7cc5ed
Author: Mark Wells <mark at freeside.biz>
Date:   Wed May 22 16:58:31 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 0e43276..b6ee518 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