lock table for tax district updates, #73185
authorMark Wells <mark@freeside.biz>
Wed, 16 Nov 2016 22:07:57 +0000 (14:07 -0800)
committerMark Wells <mark@freeside.biz>
Wed, 16 Nov 2016 22:11:58 +0000 (14:11 -0800)
FS/FS/Cron/tax_rate_update.pm
FS/FS/geocode_Mixin.pm
FS/bin/freeside-upgrade

index 2c021b8..b6ac63c 100755 (executable)
@@ -40,6 +40,7 @@ sub tax_rate_update {
 
   my $taxname = $conf->config('tax_district_taxname') || '';
 
 
   my $taxname = $conf->config('tax_district_taxname') || '';
 
+  FS::cust_main_county->lock_table;
   if ($method eq 'wa_sales') {
     # download the update file
     my $now = DateTime->now;
   if ($method eq 'wa_sales') {
     # download the update file
     my $now = DateTime->now;
index 46f8128..ef16e74 100644 (file)
@@ -260,6 +260,7 @@ sub process_district_update {
   die $@ if $@;
   die "$class has no location data" if !$class->can('location_hash');
 
   die $@ if $@;
   die "$class has no location data" if !$class->can('location_hash');
 
+  my $error;
   my $conf = FS::Conf->new;
   my $method = $conf->config('tax_district_method')
     or return; #nothing to do if null
   my $conf = FS::Conf->new;
   my $method = $conf->config('tax_district_method')
     or return; #nothing to do if null
@@ -269,9 +270,11 @@ sub process_district_update {
   my $tax_info = get_district({ $self->location_hash }, $method);
   return unless $tax_info;
 
   my $tax_info = get_district({ $self->location_hash }, $method);
   return unless $tax_info;
 
-  $self->set('district', $tax_info->{'district'} );
-  my $error = $self->replace;
-  die $error if $error;
+  if ($self->district ne $tax_info->{'district'}) {
+    $self->set('district', $tax_info->{'district'} );
+    $error = $self->replace;
+    die $error if $error;
+  }
 
   my %hash = map { $_ => uc( $tax_info->{$_} ) } 
     qw( district city county state country );
 
   my %hash = map { $_ => uc( $tax_info->{$_} ) } 
     qw( district city county state country );
@@ -281,6 +284,9 @@ sub process_district_update {
   my $taxname = $conf->config('tax_district_taxname');
   # there must be exactly one cust_main_county for each district+taxclass.
   # do NOT exclude taxes that are zero.
   my $taxname = $conf->config('tax_district_taxname');
   # there must be exactly one cust_main_county for each district+taxclass.
   # do NOT exclude taxes that are zero.
+
+  # mutex here so that concurrent queue jobs can't make duplicates.
+  FS::cust_main_county->lock_table;
   foreach my $taxclass (@classes) {
     my @existing = qsearch('cust_main_county', {
       %hash,
   foreach my $taxclass (@classes) {
     my @existing = qsearch('cust_main_county', {
       %hash,
index 77087c3..ffd574e 100755 (executable)
@@ -298,7 +298,7 @@ print "\n" if $DRY_RUN;
 
 if ( $dbh->{Driver}->{Name} =~ /^mysql/i && ! $opt_s ) {
 
 
 if ( $dbh->{Driver}->{Name} =~ /^mysql/i && ! $opt_s ) {
 
-  foreach my $table (qw( svc_acct svc_phone )) {
+  foreach my $table (qw( svc_acct svc_phone cust_main_county )) {
 
     my $sth = $dbh->prepare(
       "SELECT COUNT(*) FROM duplicate_lock WHERE lockname = '$table'"
 
     my $sth = $dbh->prepare(
       "SELECT COUNT(*) FROM duplicate_lock WHERE lockname = '$table'"