have freeside-queued put billing jobs in the queue, so they run in their own short...
authorivan <ivan>
Sun, 21 Dec 2008 18:09:55 +0000 (18:09 +0000)
committerivan <ivan>
Sun, 21 Dec 2008 18:09:55 +0000 (18:09 +0000)
FS/FS/Schema.pm
FS/FS/queue.pm
FS/bin/freeside-cdrd

index 61f7437..17665b1 100644 (file)
@@ -1439,18 +1439,18 @@ sub tables_hashref {
 
     'queue' => {
       'columns' => [
-        'jobnum', 'serial', '', '', '', '', 
-        'job', 'text', '', '', '', '', 
-        '_date', 'int', '', '', '', '', 
-        'status', 'varchar', '', $char_d, '', '', 
-        'statustext', 'text', 'NULL', '', '', '', 
-        'svcnum', 'int', 'NULL', '', '', '', 
-        'secure',  'char', 'NULL', 1, '', '', # Y = needs to be run on machine
-                                              #     w/private key
+        'jobnum',      'serial',     '',      '', '', '', 
+        'job',           'text',     '',      '', '', '', 
+        '_date',          'int',     '',      '', '', '', 
+        'status',     'varchar',     '', $char_d, '', '', 
+        'statustext',    'text', 'NULL',      '', '', '', 
+        'svcnum',         'int', 'NULL',      '', '', '', 
+        'custnum',        'int', 'NULL',      '', '', '',
+        'secure',        'char', 'NULL',       1, '', '',
       ],
       'primary_key' => 'jobnum',
       'unique'      => [],
-      'index'       => [ [ 'svcnum' ], [ 'status' ] ],
+      'index'       => [ [ 'job' ], [ 'svcnum' ], [ 'custnum' ], [ 'status' ] ],
     },
 
     'queue_arg' => {
index ff94a4e..61cee0a 100644 (file)
@@ -70,7 +70,18 @@ UNIX timestamp
 
 =item svcnum
 
-Optional link to service (see L<FS::cust_svc>)
+Optional link to service (see L<FS::cust_svc>).
+
+=item custnum
+
+Optional link to customer (see L<FS::cust_main>).
+
+=item secure
+
+Secure flag, 'Y' indicates that when using encryption, the job needs to be
+run on a machine with the private key.
+
+=cut
 
 =back
 
@@ -103,7 +114,7 @@ created (see L<FS::queue_arg>).
 
 #false laziness w/part_export.pm
 sub insert {
-  my $self = shift;
+  my( $self, @args ) = @_;
 
   local $SIG{HUP} = 'IGNORE';
   local $SIG{INT} = 'IGNORE';
@@ -116,13 +127,16 @@ sub insert {
   local $FS::UID::AutoCommit = 0;
   my $dbh = dbh;
 
+  my %args = @args;
+  $self->custnum( $args{'custnum'} ) if $args{'custnum'};
+
   my $error = $self->SUPER::insert;
   if ( $error ) {
     $dbh->rollback if $oldAutoCommit;
     return $error;
   }
 
-  foreach my $arg ( @_ ) {
+  foreach my $arg ( @args ) {
     my $queue_arg = new FS::queue_arg ( {
       'jobnum' => $self->jobnum,
       'arg'    => $arg,
index 882fd54..1346531 100644 (file)
@@ -2,10 +2,11 @@
 
 use strict;
 use FS::Daemon ':all'; #daemonize1 drop_root daemonize2 myexit logfile sig*
-use FS::UID qw(adminsuidsetup);
-use FS::Record qw(qsearch ); #qsearchs);
+use FS::UID qw( adminsuidsetup );
+use FS::Record qw( qsearch ); #qsearchs);
 #use FS::cdr;
 use FS::cust_pkg;
+use FS::queue;
 
 my $user = shift or die &usage;
 
@@ -51,8 +52,16 @@ my $extra_sql =
                                        || svc_phone.phonenum
                             )
                   )
-    ) 
+    )
+    AND 0 = (
+      SELECT COUNT(*) FROM queue
+        WHERE job = 'FS::cust_main::queued_bill'
+          AND job.custnum = cust_pkg.custnum
+    )
+    
   ";
+# don't repeatedly queue failures
+#          AND status != 'failed'
 
 while (1) {
 
@@ -71,10 +80,17 @@ while (1) {
 
     #my $work_cust_pkg = $cust_pkg;
 
-    my $cust_main = $cust_pkg->cust_main;
+    #my $cust_main = $cust_pkg->cust_main;
 
     my $time = time;
-    $cust_main->bill_and_collect( 
+
+    my $job = new FS::queue {
+      'job'     => 'FS::cust_main::queued_bill',
+      'secure'  => 'Y',
+      'custnum' => $cust_pkg->custnum,
+    };
+    my $error = $job->insert(
+      'custnum'      => $cust_pkg->custnum, 
       'time'         => $time,
       'invoice_time' => $time,
       'actual_time'  => $time,
@@ -82,6 +98,13 @@ while (1) {
       #'debug'        => 1,
     );
 
+    if ( $error ) {
+      #die "FATAL: error inserting billing job: $error\n";
+      warn "WARNING: error inserting billing job (will retry in 30 seconds):".
+           " $error\n";
+      sleep 30; #i dunno, wait and see if the database comes back?
+    }
+
   }
 
   myexit() if sigterm() || sigint();
@@ -114,21 +137,21 @@ sub _shouldrun {
 }
 
 sub usage { 
-  die "Usage:\n\n  freeside-prepaidd user\n";
+  die "Usage:\n\n  freeside-cdrd user\n";
 }
 
 =head1 NAME
 
-freeside-prepaidd - Real-time daemon for prepaid packages
+freeside-cdrd - Real-time daemon for CDRs
 
 =head1 SYNOPSIS
 
-  freeside-prepaidd
+  freeside-cdrd
 
 =head1 DESCRIPTION
 
-Runs continuously and suspends or cancels any prepaid customer packages which
-have passed their renewal date (next bill date).
+Runs continuously, searches for CDRs and bills customers who have VoIP
+price plands with the B<bill_every_call> option set.
 
 =head1 SEE ALSO