View | Details | Raw Unified | Return to bug 29325
Collapse All | Expand All

(-)a/C4/ImportBatch.pm (-1 / +11 lines)
Lines 569-574 sub BatchCommitRecords { Link Here
569
    my $batch_id = shift;
569
    my $batch_id = shift;
570
    my $framework = shift;
570
    my $framework = shift;
571
571
572
    my $schema = Koha::Database->schema;
573
572
    # optional callback to monitor status 
574
    # optional callback to monitor status 
573
    # of job
575
    # of job
574
    my $progress_interval = 0;
576
    my $progress_interval = 0;
Lines 607-617 sub BatchCommitRecords { Link Here
607
    my $logged_in_patron = Koha::Patrons->find( $userenv->{number} );
609
    my $logged_in_patron = Koha::Patrons->find( $userenv->{number} );
608
610
609
    my $rec_num = 0;
611
    my $rec_num = 0;
612
613
    $schema->txn_begin; # We commit in a transaction
610
    while (my $rowref = $sth->fetchrow_hashref) {
614
    while (my $rowref = $sth->fetchrow_hashref) {
611
        $record_type = $rowref->{'record_type'};
615
        $record_type = $rowref->{'record_type'};
616
612
        $rec_num++;
617
        $rec_num++;
618
613
        if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
619
        if ($progress_interval and (0 == ($rec_num % $progress_interval))) {
614
            &$progress_callback($rec_num);
620
            # report progress and commit
621
            $schema->txn_commit;
622
            &$progress_callback( $rec_num );
623
            $schema->txn_begin;
615
        }
624
        }
616
        if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'imported') {
625
        if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'imported') {
617
            $num_ignored++;
626
            $num_ignored++;
Lines 727-732 sub BatchCommitRecords { Link Here
727
            SetImportRecordStatus($rowref->{'import_record_id'}, 'ignored');
736
            SetImportRecordStatus($rowref->{'import_record_id'}, 'ignored');
728
        }
737
        }
729
    }
738
    }
739
    $schema->txn_commit; # Commit final records that may not have hit callback threshold
730
    $sth->finish();
740
    $sth->finish();
731
    SetImportBatchStatus($batch_id, 'imported');
741
    SetImportBatchStatus($batch_id, 'imported');
732
    return ($num_added, $num_updated, $num_items_added, $num_items_replaced, $num_items_errored, $num_ignored);
742
    return ($num_added, $num_updated, $num_items_added, $num_items_replaced, $num_items_errored, $num_ignored);
(-)a/misc/commit_file.pl (-11 / +13 lines)
Lines 1-7 Link Here
1
#!/usr/bin/perl
1
#!/usr/bin/perl
2
2
3
use strict;
3
use Modern::Perl;
4
use warnings;
4
5
BEGIN {
6
    # find Koha's Perl modules
7
    # test carefully before changing this
8
    use FindBin ();
9
    eval { require "$FindBin::Bin/kohalib.pl" };
10
}
5
11
6
use Koha::Script;
12
use Koha::Script;
7
use C4::Context;
13
use C4::Context;
Lines 39-46 if ($list_batches) { Link Here
39
# in future, probably should tie to a real user account
45
# in future, probably should tie to a real user account
40
C4::Context->set_userenv(0, 'batch', 0, 'batch', 'batch', 'batch', 'batch');
46
C4::Context->set_userenv(0, 'batch', 0, 'batch', 'batch', 'batch', 'batch');
41
47
42
my $dbh = C4::Context->dbh;
43
$dbh->{AutoCommit} = 0;
44
if ($batch_number =~ /^\d+$/ and $batch_number > 0) {
48
if ($batch_number =~ /^\d+$/ and $batch_number > 0) {
45
    my $batch = GetImportBatch($batch_number);
49
    my $batch = GetImportBatch($batch_number);
46
    die "$0: import batch $batch_number does not exist in database\n" unless defined $batch;
50
    die "$0: import batch $batch_number does not exist in database\n" unless defined $batch;
Lines 53-59 if ($batch_number =~ /^\d+$/ and $batch_number > 0) { Link Here
53
            unless $batch->{'import_status'} eq "staged" or $batch->{'import_status'} eq "reverted";
57
            unless $batch->{'import_status'} eq "staged" or $batch->{'import_status'} eq "reverted";
54
        process_batch($batch_number);
58
        process_batch($batch_number);
55
    }
59
    }
56
    $dbh->commit();
57
} else {
60
} else {
58
    die "$0: please specify a numeric batch ID\n";
61
    die "$0: please specify a numeric batch ID\n";
59
}
62
}
Lines 80-86 sub process_batch { Link Here
80
83
81
    print "... importing MARC records -- please wait\n";
84
    print "... importing MARC records -- please wait\n";
82
    my ($num_added, $num_updated, $num_items_added, $num_items_replaced, $num_items_errored, $num_ignored) =
85
    my ($num_added, $num_updated, $num_items_added, $num_items_replaced, $num_items_errored, $num_ignored) =
83
        BatchCommitRecords($import_batch_id, $framework, 100, \&print_progress_and_commit);
86
        BatchCommitRecords($import_batch_id, $framework, 100, \&print_progress);
84
    print "... finished importing MARC records\n";
87
    print "... finished importing MARC records\n";
85
88
86
    print <<_SUMMARY_;
89
    print <<_SUMMARY_;
Lines 105-111 sub revert_batch { Link Here
105
108
106
    print "... reverting batch -- please wait\n";
109
    print "... reverting batch -- please wait\n";
107
    my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored) =
110
    my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored) =
108
        BatchRevertRecords($import_batch_id, 100, \&print_progress_and_commit);
111
        BatchRevertRecords( $import_batch_id );
109
    print "... finished reverting batch\n";
112
    print "... finished reverting batch\n";
110
113
111
    print <<_SUMMARY_;
114
    print <<_SUMMARY_;
Lines 117-132 Number of records deleted: $num_deleted Link Here
117
Number of errors:                $num_errors
120
Number of errors:                $num_errors
118
Number of records reverted:      $num_reverted
121
Number of records reverted:      $num_reverted
119
Number of records ignored:       $num_ignored
122
Number of records ignored:       $num_ignored
120
Number of items added:           $num_items_deleted
123
Number of items deleted:         $num_items_deleted
121
124
122
_SUMMARY_
125
_SUMMARY_
123
}
126
}
124
127
125
128
126
sub print_progress_and_commit {
129
sub print_progress {
127
    my $recs = shift;
130
    my ( $recs ) = @_;
128
    print "... processed $recs records\n";
131
    print "... processed $recs records\n";
129
    $dbh->commit();
130
}
132
}
131
133
132
sub print_usage {
134
sub print_usage {
(-)a/tools/manage-marc-import.pl (-22 / +12 lines)
Lines 232-253 sub commit_batch { Link Here
232
    my $job = undef;
232
    my $job = undef;
233
    my ( $num_added, $num_updated, $num_items_added,
233
    my ( $num_added, $num_updated, $num_items_added,
234
        $num_items_replaced, $num_items_errored, $num_ignored );
234
        $num_items_replaced, $num_items_errored, $num_ignored );
235
    my $schema = Koha::Database->new->schema;
235
    my $callback = sub { };
236
    $schema->storage->txn_do(
236
    if ($runinbackground) {
237
        sub {
237
        $job = put_in_background($import_batch_id);
238
            my $callback = sub { };
238
        $callback = progress_callback( $job );
239
            if ($runinbackground) {
239
    }
240
                $job = put_in_background($import_batch_id);
240
    (
241
                $callback = progress_callback( $job, $dbh );
241
        $num_added, $num_updated, $num_items_added,
242
            }
242
        $num_items_replaced, $num_items_errored, $num_ignored
243
            (
243
      )
244
                $num_added, $num_updated, $num_items_added,
244
      = BatchCommitRecords( $import_batch_id, $framework, 50,
245
                $num_items_replaced, $num_items_errored, $num_ignored
245
        $callback );
246
              )
247
              = BatchCommitRecords( $import_batch_id, $framework, 50,
248
                $callback );
249
        }
250
    );
251
246
252
    my $results = {
247
    my $results = {
253
        did_commit => 1,
248
        did_commit => 1,
Lines 276-290 sub revert_batch { Link Here
276
    my $schema = Koha::Database->new->schema;
271
    my $schema = Koha::Database->new->schema;
277
    $schema->txn_do(
272
    $schema->txn_do(
278
        sub {
273
        sub {
279
            my $callback = sub { };
280
            if ($runinbackground) {
274
            if ($runinbackground) {
281
                $job = put_in_background($import_batch_id);
275
                $job = put_in_background($import_batch_id);
282
                $callback = progress_callback( $job, $dbh );
283
            }
276
            }
284
            (
277
            (
285
                $num_deleted,       $num_errors, $num_reverted,
278
                $num_deleted,       $num_errors, $num_reverted,
286
                $num_items_deleted, $num_ignored
279
                $num_items_deleted, $num_ignored
287
            ) = BatchRevertRecords( $import_batch_id, 50, $callback );
280
            ) = BatchRevertRecords( $import_batch_id );
288
        }
281
        }
289
    );
282
    );
290
283
Lines 340-350 sub put_in_background { Link Here
340
333
341
sub progress_callback {
334
sub progress_callback {
342
    my $job = shift;
335
    my $job = shift;
343
    my $dbh = shift;
344
    return sub {
336
    return sub {
345
        my $progress = shift;
337
        my $progress = shift;
346
        $job->progress($progress);
338
        $job->progress($progress);
347
        $dbh->commit();
348
    }
339
    }
349
}
340
}
350
341
351
- 

Return to bug 29325