mysql compatibility?
[freeside.git] / FS / bin / freeside-queued
index 1539a48..67e5e2b 100644 (file)
@@ -7,9 +7,10 @@ use Fcntl qw(:flock);
 use POSIX qw(setsid);
 use Date::Format;
 use IO::File;
-use FS::UID qw(adminsuidsetup forksuidsetup driver_name);
+use FS::UID qw(adminsuidsetup forksuidsetup driver_name dbh);
 use FS::Record qw(qsearchs);
 use FS::queue;
+use FS::queue_depend;
 
 # no autoloading just yet
 use FS::cust_main;
@@ -17,13 +18,14 @@ use FS::svc_acct;
 use Net::SSH 0.05;
 use FS::part_export;
 
-my $pid_file = '/var/run/freeside-queued.pid';
-
 $max_kids = '10'; #guess it should be a config file...
 $kids = 0;
 
 my $user = shift or die &usage;
 
+#my $pid_file = "/var/run/freeside-queued.$user.pid";
+my $pid_file = "/var/run/freeside-queued.pid";
+
 &daemonize1;
 
 sub REAPER { my $pid = wait; $SIG{CHLD} = \&REAPER; $kids--; }
@@ -59,26 +61,44 @@ while (1) {
   }
   $warnkids=0;
 
-  my $nodepend = 'AND 0 = ( SELECT COUNT(*) FROM queue_depend'.
-                          ' WHERE queue_depend.jobnum = queue.jobnum ) ';
-
-  my $job = qsearchs(
-    'queue',
-    { 'status' => 'new' },
-    '',
-    driver_name =~ /^mysql$/i
-      ? "$nodepend ORDER BY jobnum LIMIT 1 FOR UPDATE"
-      : "$nodepend ORDER BY jobnum FOR UPDATE LIMIT 1"
-  ) or do {
-    sleep 5; #connecting to db is expensive
-    next;
-  };
+  my $nodepend = driver name eq 'mysql'
+   ? ''
+   : 'AND 0 = ( SELECT COUNT(*) FROM queue_depend'.
+     ' WHERE queue_depend.jobnum = queue.jobnum ) ';
+
+  my($job, $ljob);
+  {
+    my $oldAutoCommit = $FS::UID::AutoCommit;
+    local $FS::UID::AutoCommit = 0;
+    my $dbh = dbh; 
+  
+    $job = qsearchs(
+      'queue',
+      { 'status' => 'new' },
+      '',
+      driver_name eq 'mysql'
+        ? "$nodepend ORDER BY jobnum LIMIT 1 FOR UPDATE"
+        : "$nodepend ORDER BY jobnum FOR UPDATE LIMIT 1"
+    ) or do {
+      $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+      sleep 5; #connecting to db is expensive
+      next;
+    };
+
+    if ( driver_name eq 'mysql'
+         && qsearch('queue_depend', { 'jobnum' => $job->jobnum } ) ) {
+      $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+      next;
+    }
+
+    my %hash = $job->hash;
+    $hash{'status'} = 'locked';
+    $ljob = new FS::queue ( \%hash );
+    my $error = $ljob->replace($job);
+    die $error if $error;
 
-  my %hash = $job->hash;
-  $hash{'status'} = 'locked';
-  my $ljob = new FS::queue ( \%hash );
-  my $error = $ljob->replace($job);
-  die $error if $error;
+    $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+  } 
 
   my @args = $ljob->args;