From: Mark Wells Date: Wed, 22 May 2013 23:58:31 +0000 (-0700) Subject: fully automate coordinate lookups, #23040 X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=commitdiff_plain;h=e6210bad824b2efc0a2d54abe8fc79093e7cc5ed fully automate coordinate lookups, #23040 --- diff --git a/FS/FS/Cron/cleanup.pm b/FS/FS/Cron/cleanup.pm new file mode 100644 index 000000000..4c5cff278 --- /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; + +@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 1cb5e52c4..d772dab08 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 0e43276ad..b6ee5188e 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'); ###