use Net::SSH::ssh_cmd for all job queueing rather than local duplicated ssh subs
[freeside.git] / FS / bin / freeside-queued
index 1eef00c..56475d0 100644 (file)
@@ -7,31 +7,29 @@ use Fcntl qw(:flock);
 use POSIX qw(setsid);
 use Date::Format;
 use IO::File;
-use FS::UID qw(adminsuidsetup forksuidsetup);
+use FS::UID qw(adminsuidsetup forksuidsetup driver_name);
 use FS::Record qw(qsearchs);
 use FS::queue;
 
 # no autoloading just yet
 use FS::cust_main;
 use FS::svc_acct;
-use Net::SSH;
+use Net::SSH 0.05;
 
 my $pid_file = '/var/run/freeside-queued.pid';
 
-$SIG{CHLD} = sub { wait }; #zombie prevention
-
 my $user = shift or die &usage;
 
 &daemonize;
 
+sub REAPER { my $pid = wait; $SIG{CHLD} = \&REAPER; }
+$SIG{CHLD} =  \&REAPER;
+
  $sigterm = 0;
  $sigint = 0;
 $SIG{INT} = sub { warn "SIGINT received; shutting down\n"; $sigint++; };
 $SIG{TERM} = sub { warn "SIGTERM received; shutting down\n"; $sigterm++; };
 
-#pickup zombie status
-$SIG{CHLD} = sub { wait; };
-
 $> = $FS::UID::freeside_uid unless $>;
 $< = $>;
 $ENV{HOME} = (getpwuid($>))[7]; #for ssh
@@ -42,7 +40,7 @@ $log_file = "/usr/local/etc/freeside/queuelog.". $FS::UID::datasrc;
 $SIG{__DIE__} = \&_die;
 $SIG{__WARN__} = \&_logmsg;
 
-warn "freesied-queued starting\n";
+warn "freeside-queued starting\n";
 
 while (1) {
 
@@ -50,7 +48,9 @@ while (1) {
     'queue',
     { 'status' => 'new' },
     '',
-    'ORDER BY jobnum FOR UPDATE LIMIT 1'
+    driver_name =~ /^mysql$/i
+      ? 'ORDER BY jobnum LIMIT 1 FOR UPDATE'
+      : 'ORDER BY jobnum FOR UPDATE LIMIT 1'
   ) or do {
     sleep 5;
     next;
@@ -69,6 +69,7 @@ while (1) {
     warn "WARNING: can't fork: $!\n";
     my %hash = $job->hash;
     $hash{'status'} = 'failed';
+    $hash{'statustext'} = "[freeside-queued] can't fork: $!";
     my $ljob = new FS::queue ( \%hash );
     my $error = $ljob->replace($job);
     die $error if $error;
@@ -78,7 +79,7 @@ while (1) {
 
     #get new db handles
     $FS::UID::dbh->{InactiveDestroy} = 1;
-    $FS::svc_acct::icradius_dbh->{InactiveDestroy}
+    $FS::svc_acct::icradius_dbh->{InactiveDestroy} = 1
       if $FS::svc_acct::icradius_dbh;
     forksuidsetup($user);
 
@@ -89,6 +90,7 @@ while (1) {
       warn "job $eval failed";
       my %hash = $ljob->hash;
       $hash{'status'} = 'failed';
+      $hash{'statustext'} = $@;
       my $fjob = new FS::queue( \%hash );
       my $error = $fjob->replace($ljob);
       die $error if $error;