summaryrefslogtreecommitdiff
path: root/FS/FS/Cron/export_batch.pm
blob: cb16eeed2a452a4f3ccab7bbb57bc22703502a85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package FS::Cron::export_batch;

use strict;
use vars qw( @ISA @EXPORT_OK $me $DEBUG );
use Exporter;
use FS::UID qw(dbh);
use FS::Record qw( qsearch qsearchs );
use FS::Conf;
use FS::export_batch;

@ISA = qw( Exporter );
@EXPORT_OK = qw ( export_batch_submit );
$DEBUG = 0;
$me = '[FS::Cron::export_batch]';

#freeside-daily %opt:
#  -v: enable debugging
#  -l: debugging level
#  -m: Experimental multi-process mode uses the job queue for multi-process and/or multi-machine billing.
#  -r: Multi-process mode dry run option
#  -a: Only process customers with the specified agentnum

sub export_batch_submit {
  my %opt = @_;
  local $DEBUG = ($opt{l} || 1) if $opt{v};
  
  warn "$me batch_submit\n" if $DEBUG;

  # like pay_batch, none of this is per-agent
  if ( $opt{a} ) {
    warn "Export batch processing skipped in per-agent mode.\n" if $DEBUG;
    return;
  }
  my @batches = qsearch({
      table     => 'export_batch',
      extra_sql => "WHERE status IN ('open', 'closed')",
  });

  foreach my $batch (@batches) {
    my $export = $batch->part_export;
    next if $export->disabled;
    warn "processing batchnum ".$batch->batchnum.
         " via ".$export->exporttype.  "\n"
      if $DEBUG;
    local $@;
    eval {
      $export->process($batch);
    };
    if ($@) {
      dbh->rollback;
      warn "export batch ".$batch->batchnum." failed: $@\n";
      $batch->set(status => 'failed');
      $batch->set(statustext => $@);
      my $error = $batch->replace;
      die "error recording batch status: $error"
        if $error;
      dbh->commit;
    }
  }
}

# currently there's no batch_receive() or anything of that sort

1;