fixes for spool_upload in multiprocess mode, #6802, #18333
authorMark Wells <mark@freeside.biz>
Wed, 28 Nov 2012 03:25:25 +0000 (19:25 -0800)
committerMark Wells <mark@freeside.biz>
Wed, 28 Nov 2012 03:25:25 +0000 (19:25 -0800)
FS/FS/Conf.pm
FS/FS/Cron/upload.pm
FS/bin/freeside-queued

index 7a3a10b..d4f10fd 100644 (file)
@@ -3099,21 +3099,21 @@ and customer address. Include units.',
   {
     'key'         => 'cust_bill-ftpusername',
     'section'     => 'invoicing',
-    'description' => 'Enable FTP of raw invoice data - server.',
+    'description' => 'Enable FTP of raw invoice data - login.',
     'type'        => 'text',
   },
 
   {
     'key'         => 'cust_bill-ftppassword',
     'section'     => 'invoicing',
-    'description' => 'Enable FTP of raw invoice data - server.',
+    'description' => 'Enable FTP of raw invoice data - password.',
     'type'        => 'text',
   },
 
   {
     'key'         => 'cust_bill-ftpdir',
     'section'     => 'invoicing',
-    'description' => 'Enable FTP of raw invoice data - server.',
+    'description' => 'Enable FTP of raw invoice data - path.',
     'type'        => 'text',
   },
 
index c266797..dfdacf0 100644 (file)
@@ -146,7 +146,7 @@ sub spool_upload {
   my $conf = new FS::Conf;
   my $dir = '%%%FREESIDE_EXPORT%%%/export.'. $FS::UID::datasrc. '/cust_bill';
 
-  my $agentnum = $opt{agentnum} or die "no agentnum provided\n";
+  my $agentnum = $opt{agentnum} || '';
   my $url      = $opt{url} or die "no url for agent $agentnum\n";
   $url =~ s/^\s+//; $url =~ s/\s+$//;
 
@@ -166,12 +166,31 @@ sub spool_upload {
   local $FS::UID::AutoCommit = 0;
   my $dbh = dbh;
 
-  my $agent = qsearchs( 'agent', { agentnum => $agentnum } )
-    or die "no such agent: $agentnum";
-  $agent->select_for_update; #mutex 
+  # wait for any ongoing billing jobs to complete
+  # (should this exclude status='failed')?
+  if ($opt{m}) {
+    my $sql = "SELECT count(*) FROM queue LEFT JOIN cust_main USING(custnum) ".
+    "WHERE queue.job='FS::cust_main::queued_bill'";
+    $sql .= " AND cust_main.agentnum = $agentnum" if $agentnum =~ /^\d+$/;
+    my $sth = $dbh->prepare($sql) or die $dbh->errstr;
+    while (1) {
+      $sth->execute()
+        or die "Unexpected error executing statement $sql: ". $sth->errstr;
+      last if $sth->fetchrow_arrayref->[0] == 0;
+      sleep 300;
+    }
+  }
+
+  if ( $agentnum ) {
+    my $agent = qsearchs( 'agent', { agentnum => $agentnum } )
+      or die "no such agent: $agentnum";
+    $agent->select_for_update; #mutex 
+  }
 
   if ( $opt{'format'} eq 'billco' ) {
 
+    die "no agentnum provided\n" unless $agentnum;
+
     my $zipfile  = "$dir/agentnum$agentnum-$opt{date}.zip";
 
     unless ( -f "$dir/agentnum$agentnum-header.csv" ||
@@ -183,19 +202,6 @@ sub spool_upload {
       return;
     }
 
-    # a better way?
-    if ($opt{m}) {
-      my $sql = "SELECT count(*) FROM queue LEFT JOIN cust_main USING(custnum) ".
-        "WHERE queue.job='FS::cust_main::queued_bill' AND cust_main.agentnum = ?";
-      my $sth = $dbh->prepare($sql) or die $dbh->errstr;
-      while (1) {
-        $sth->execute( $agentnum )
-          or die "Unexpected error executing statement $sql: ". $sth->errstr;
-        last if $sth->fetchrow_arrayref->[0];
-        sleep 300;
-      }
-    }
-
     foreach ( qw ( header detail ) ) {
       rename "$dir/agentnum$agentnum-$_.csv",
              "$dir/agentnum$agentnum-$opt{date}-$_.csv";
index 756b699..f136c39 100644 (file)
@@ -186,7 +186,7 @@ while (1) {
       dbh->{'private_profile'} = {} if UNIVERSAL::can(dbh, 'sprintProfile');
 
       #auto-use classes...
-      if (    $ljob->job =~ /(FS::(part_export|cust_main|cust_pkg)::\w+)::/
+      if (    $ljob->job =~ /(FS::(part_export|cust_main|cust_pkg|Cron)::\w+)::/
            || $ljob->job =~ /(FS::\w+)::/
          )
       {