Bug 27421: (QA follow-up) Similar changes for revert commit
[koha.git] / Koha / BackgroundJob / MARCImportRevertBatch.pm
1 package Koha::BackgroundJob::MARCImportRevertBatch;
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 C4::ImportBatch qw(
24     BatchRevertRecords
25 );
26
27 =head1 NAME
28
29 Koha::BackgroundJob::MARCImportRevertBatch - Revert a batch
30
31 This is a subclass of Koha::BackgroundJob.
32
33 =head1 API
34
35 =head2 Class methods
36
37 =head3 job_type
38
39 Define the job type of this job: marc_import_revert_batch
40
41 =cut
42
43 sub job_type {
44     return 'marc_import_revert_batch';
45 }
46
47 =head3 process
48
49 Revert a batch
50
51 =cut
52
53 sub process {
54     my ( $self, $args ) = @_;
55
56     $self->start;
57
58     my $import_batch_id = $args->{import_batch_id};
59
60     my @messages;
61     my $job_progress = 0;
62     my (
63         $num_deleted,       $num_errors, $num_reverted,
64         $num_items_deleted, $num_ignored
65     );
66
67     try {
68         (
69             $num_deleted,       $num_errors, $num_reverted,
70             $num_items_deleted, $num_ignored
71         ) = BatchRevertRecords( $import_batch_id, 50,
72             sub { my $job_progress = shift; $self->progress( $job_progress )->store } );
73     }
74     catch {
75         warn $_;
76         die "Something terrible has happened!"
77           if ( $_ =~ /Rollback failed/ );    # Rollback failed
78     };
79
80     my $report = {
81         num_deleted       => $num_deleted,
82         num_items_deleted => $num_items_deleted,
83         num_errors        => $num_errors,
84         num_reverted      => $num_reverted,
85         num_ignored       => $num_ignored,
86         import_batch_id   => $import_batch_id,
87     };
88
89     my $data = $self->decoded_data;
90     $data->{messages} = \@messages;
91     $data->{report}   = $report;
92
93     $self->finish($data);
94 }
95
96 =head3 enqueue
97
98 Enqueue the new job
99
100 =cut
101
102 sub enqueue {
103     my ( $self, $args) = @_;
104
105     $self->SUPER::enqueue({
106         job_size => 0, # unknown for now
107         job_args => $args
108     });
109 }
110
111 1;