From 8282a324857d658d17061e3f0867d8c7d71b098a Mon Sep 17 00:00:00 2001 From: Mark Wells Date: Fri, 14 Oct 2016 09:34:06 -0700 Subject: [PATCH 1/1] cursorize part_event::initialize so that it works on large data sets --- FS/FS/part_event.pm | 63 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 26 deletions(-) diff --git a/FS/FS/part_event.pm b/FS/FS/part_event.pm index 58e01271c..1c2389989 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 @@ -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; } -- 2.11.0