Bug 30755: Remove auto_too_soon from error count
[koha.git] / Koha / BackgroundJob / BatchDeleteAuthority.pm
1 package Koha::BackgroundJob::BatchDeleteAuthority;
2
3 use Modern::Perl;
4
5 use C4::AuthoritiesMarc;
6
7 use Koha::DateUtils qw( dt_from_string );
8 use Koha::SearchEngine;
9 use Koha::SearchEngine::Indexer;
10
11 use base 'Koha::BackgroundJob';
12
13 sub job_type {
14     return 'batch_authority_record_deletion';
15 }
16
17 sub process {
18     my ( $self, $args ) = @_;
19
20     if ( $self->status eq 'cancelled' ) {
21         return;
22     }
23
24     # FIXME If the job has already been started, but started again (worker has been restart for instance)
25     # Then we will start from scratch and so double delete the same records
26
27     my $job_progress = 0;
28     $self->started_on(dt_from_string)
29         ->progress($job_progress)
30         ->status('started')
31         ->store;
32
33     my $mmtid = $args->{mmtid};
34     my @record_ids = @{ $args->{record_ids} };
35
36     my $report = {
37         total_records => scalar @record_ids,
38         total_success => 0,
39     };
40     my @messages;
41     my $schema = Koha::Database->new->schema;
42     RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
43
44         last if $self->get_from_storage->status eq 'cancelled';
45
46         next unless $record_id;
47
48         $schema->storage->txn_begin;
49
50         my $authid = $record_id;
51         eval {
52             C4::AuthoritiesMarc::DelAuthority(
53                 { authid => $authid, skip_record_index => 1 } );
54         };
55         if ( $@ ) {
56             push @messages, {
57                 type => 'error',
58                 code => 'authority_not_deleted',
59                 authid => $authid,
60                 error => "$@",
61             };
62             $schema->storage->txn_rollback;
63             next;
64         } else {
65             push @messages, {
66                 type => 'success',
67                 code => 'authority_deleted',
68                 authid => $authid,
69             };
70             $report->{total_success}++;
71             $schema->storage->txn_commit;
72         }
73
74         $self->progress( ++$job_progress )->store;
75     }
76
77     my @deleted_authids =
78       map { $_->{code} eq 'authority_deleted' ? $_->{authid} : () }
79           @messages;
80
81     if ( @deleted_authids ) {
82         my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::AUTHORITIES_INDEX });
83         $indexer->index_records( \@deleted_authids, "recordDelete", "authorityserver" );
84     }
85
86     my $json = $self->json;
87     my $job_data = $json->decode($self->data);
88     $job_data->{messages} = \@messages;
89     $job_data->{report} = $report;
90
91     $self->ended_on(dt_from_string)
92         ->data($json->encode($job_data));
93     $self->status('finished') if $self->status ne 'cancelled';
94     $self->store;
95 }
96
97 sub enqueue {
98     my ( $self, $args) = @_;
99
100     # TODO Raise exception instead
101     return unless exists $args->{record_ids};
102
103     my @record_ids = @{ $args->{record_ids} };
104
105     $self->SUPER::enqueue({
106         job_size => scalar @record_ids,
107         job_args => {record_ids => \@record_ids,},
108         queue    => 'long_tasks',
109     });
110 }
111
112 1;