Bug 32154: DBIC schema
[koha-ffzg.git] / Koha / BackgroundJob / MARCImportCommitBatch.pm
1 package Koha::BackgroundJob::MARCImportCommitBatch;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19 use Try::Tiny;
20
21 use base 'Koha::BackgroundJob';
22
23 use Koha::Database;
24 use Koha::Import::Records;
25 use C4::ImportBatch qw(
26     BatchCommitRecords
27 );
28
29 =head1 NAME
30
31 Koha::BackgroundJob::MARCImportCommitBatch - Commit records
32
33 This is a subclass of Koha::BackgroundJob.
34
35 =head1 API
36
37 =head2 Class methods
38
39 =head3 job_type
40
41 Define the job type of this job: marc_import_commit_batch
42
43 =cut
44
45 sub job_type {
46     return 'marc_import_commit_batch';
47 }
48
49 =head3 process
50
51 Commit the records
52
53 =cut
54
55 sub process {
56     my ( $self, $args ) = @_;
57
58     $self->start;
59
60     my $import_batch_id = $args->{import_batch_id};
61     my $frameworkcode = $args->{frameworkcode};
62
63     my @messages;
64     my $job_progress = 0;
65
66     my ( $num_added, $num_updated, $num_items_added,
67         $num_items_replaced, $num_items_errored, $num_ignored );
68     try {
69         my $size = Koha::Import::Records->search({ import_batch_id => $import_batch_id })->count;
70         $self->size($size)->store;
71         ( $num_added, $num_updated, $num_items_added,
72           $num_items_replaced, $num_items_errored, $num_ignored ) =
73           BatchCommitRecords(
74             $import_batch_id, $frameworkcode, 50,
75             sub { my $job_progress = shift; $self->progress( $job_progress )->store },
76             { skip_intermediate_commit => 1 },
77         );
78         my $count = $num_added + $num_updated;
79         if( $count ) {
80             $self->set({ progress => $count, size => $count });
81         } else { # TODO Refine later
82             $self->set({ progress => 0, status => 'failed' });
83         }
84     }
85     catch {
86         warn $_;
87         Koha::Database->schema->storage->txn_rollback; # TODO BatchCommitRecords started a transaction
88         die "Something terrible has happened!"
89           if ( $_ =~ /Rollback failed/ );    # Rollback failed
90         $self->set({ progress => 0, status => 'failed' });
91     };
92
93     my $report = {
94         num_added          => $num_added,
95         num_updated        => $num_updated,
96         num_items_added    => $num_items_added,
97         num_items_replaced => $num_items_replaced,
98         num_items_errored  => $num_items_errored,
99         num_ignored        => $num_ignored,
100         import_batch_id    => $import_batch_id,
101     };
102     my $data = $self->decoded_data;
103     $data->{messages} = \@messages;
104     $data->{report}   = $report;
105
106     $self->finish($data);
107 }
108
109 =head3 enqueue
110
111 Enqueue the new job
112
113 =cut
114
115 sub enqueue {
116     my ( $self, $args) = @_;
117
118     $self->SUPER::enqueue({
119         job_size => Koha::Import::Records->search({ import_batch_id => $args->{import_batch_id} })->count,
120         job_args => $args,
121         queue    => 'long_tasks',
122     });
123 }
124
125 1;