Bug 31028: Add new Koha::Object(s) classes
[koha.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     my $overlay_frameworkcode = $args->{overlay_framework};
63
64     my @messages;
65     my $job_progress = 0;
66
67     my ( $num_added, $num_updated, $num_items_added,
68         $num_items_replaced, $num_items_errored, $num_ignored );
69     try {
70         my $size = Koha::Import::Records->search({ import_batch_id => $import_batch_id })->count;
71         $self->size($size)->store;
72         ( $num_added, $num_updated, $num_items_added,
73           $num_items_replaced, $num_items_errored, $num_ignored ) =
74           BatchCommitRecords({
75             batch_id => $import_batch_id,
76             framework => $frameworkcode,
77             overlay_framework => $overlay_frameworkcode,
78             progress_interval => 50,
79             progress_callback =>
80                 sub { my $job_progress = shift; $self->progress( $job_progress )->store },
81             skip_intermediate_commit => 1,
82             });
83         my $count = $num_added + $num_updated;
84         if( $count ) {
85             $self->set({ progress => $count, size => $count });
86         } else { # TODO Refine later
87             $self->set({ progress => 0, status => 'failed' });
88         }
89     }
90     catch {
91         warn $_;
92         Koha::Database->schema->storage->txn_rollback; # TODO BatchCommitRecords started a transaction
93         die "Something terrible has happened!"
94           if ( $_ =~ /Rollback failed/ );    # Rollback failed
95         $self->set({ progress => 0, status => 'failed' });
96     };
97
98     my $report = {
99         num_added          => $num_added,
100         num_updated        => $num_updated,
101         num_items_added    => $num_items_added,
102         num_items_replaced => $num_items_replaced,
103         num_items_errored  => $num_items_errored,
104         num_ignored        => $num_ignored,
105         import_batch_id    => $import_batch_id,
106     };
107     my $data = $self->decoded_data;
108     $data->{messages} = \@messages;
109     $data->{report}   = $report;
110
111     $self->finish($data);
112 }
113
114 =head3 enqueue
115
116 Enqueue the new job
117
118 =cut
119
120 sub enqueue {
121     my ( $self, $args) = @_;
122
123     $self->SUPER::enqueue({
124         job_size  => Koha::Import::Records->search({ import_batch_id => $args->{import_batch_id} })->count,
125         job_args  => $args,
126         job_queue => 'long_tasks',
127     });
128 }
129
130 1;