summaryrefslogtreecommitdiff
path: root/FS/FS/part_event.pm
diff options
context:
space:
mode:
authorMark Wells <mark@freeside.biz>2016-10-14 09:34:06 -0700
committerMark Wells <mark@freeside.biz>2016-10-14 12:04:11 -0700
commit8282a324857d658d17061e3f0867d8c7d71b098a (patch)
treeb1b81a24a43dcf135c5ab9e54f5e6fe1329612fe /FS/FS/part_event.pm
parentc8d971625edc253e1fa011c08a96030c12dde3bb (diff)
cursorize part_event::initialize so that it works on large data sets
Diffstat (limited to 'FS/FS/part_event.pm')
-rw-r--r--FS/FS/part_event.pm63
1 files changed, 37 insertions, 26 deletions
diff --git a/FS/FS/part_event.pm b/FS/FS/part_event.pm
index 58e0127..1c23899 100644
--- a/FS/FS/part_event.pm
+++ b/FS/FS/part_event.pm
@@ -6,6 +6,7 @@ use vars qw( $DEBUG );
use Carp qw(confess);
use FS::Record qw( dbh qsearch qsearchs );
use FS::Conf;
+use FS::Cursor;
use FS::part_event_option;
use FS::part_event_condition;
use FS::cust_event;
@@ -251,10 +252,28 @@ but can be useful when configuring events.
=cut
-sub targets {
+sub targets { # may want to cursor this also
my $self = shift;
my %opt = @_;
- my $time = $opt{'time'} || time;
+ my $time = $opt{'time'} ||= time;
+
+ my $query = $self->_target_query(%opt);
+ my @objects = qsearch($query);
+ my @tested_objects;
+ foreach my $object ( @objects ) {
+ my $cust_event = $self->new_cust_event($object, 'time' => $time);
+ next unless $cust_event->test_conditions;
+
+ $object->set('cust_event', $cust_event);
+ push @tested_objects, $object;
+ }
+ @tested_objects;
+}
+
+sub _target_query {
+ my $self = shift;
+ my %opt = @_;
+ my $time = $opt{'time'};
my $eventpart = $self->eventpart;
$eventpart =~ /^\d+$/ or die "bad eventpart $eventpart";
@@ -285,23 +304,15 @@ sub targets {
# and don't enforce disabled because we want to be able to see targets
# for a disabled event
- my @objects = qsearch({
+ {
table => $eventtable,
hashref => {},
addl_from => $join,
extra_sql => "WHERE $where",
- });
- my @tested_objects;
- foreach my $object ( @objects ) {
- my $cust_event = $self->new_cust_event($object, 'time' => $time);
- next unless $cust_event->test_conditions;
-
- $object->set('cust_event', $cust_event);
- push @tested_objects, $object;
- }
- @tested_objects;
+ };
}
+
=item initialize PARAMS
Identify all objects eligible for this event and create L<FS::cust_event>
@@ -323,26 +334,26 @@ sub initialize {
my $self = shift;
my $error;
- my $oldAutoCommit = $FS::UID::AutoCommit;
- local $FS::UID::AutoCommit = 0;
- my $dbh = dbh;
+ my $time = time;
+
+ local $FS::UID::AutoCommit = 1;
+ my $cursor = FS::Cursor->new( $self->_target_query('time' => $time) );
+ while (my $object = $cursor->fetch) {
+
+ my $cust_event = $self->new_cust_event($object, 'time' => $time);
+ next unless $cust_event->test_conditions;
- my @objects = $self->targets;
- foreach my $object ( @objects ) {
- my $cust_event = $object->get('cust_event');
$cust_event->status('initial');
$error = $cust_event->insert;
- last if $error;
+ die $error if $error;
}
- if ( !$error and $self->disabled ) {
+
+ # on successful completion only, re-enable the event
+ if ( $self->disabled ) {
$self->disabled('');
$error = $self->replace;
+ die $error if $error;
}
- if ( $error ) {
- $dbh->rollback;
- return $error;
- }
- $dbh->commit if $oldAutoCommit;
return;
}