1 package Koha::BackgroundJob::BatchDeleteAuthority;
4 use JSON qw( encode_json decode_json );
6 use Koha::BackgroundJobs;
7 use Koha::DateUtils qw( dt_from_string );
8 use C4::AuthoritiesMarc;
10 use base 'Koha::BackgroundJob';
13 return 'batch_authority_record_deletion';
17 my ( $self, $args ) = @_;
19 my $job_type = $args->{job_type};
21 my $job = Koha::BackgroundJobs->find( $args->{job_id} );
23 if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) {
27 # FIXME If the job has already been started, but started again (worker has been restart for instance)
28 # Then we will start from scratch and so double delete the same records
31 $job->started_on(dt_from_string)
32 ->progress($job_progress)
36 my $mmtid = $args->{mmtid};
37 my @record_ids = @{ $args->{record_ids} };
40 total_records => scalar @record_ids,
44 my $schema = Koha::Database->new->schema;
45 RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
47 last if $job->get_from_storage->status eq 'cancelled';
49 next unless $record_id;
51 $schema->storage->txn_begin;
53 my $authid = $record_id;
54 eval { C4::AuthoritiesMarc::DelAuthority({ authid => $authid }) };
58 code => 'authority_not_deleted',
60 error => ($@ ? $@ : 0),
62 $schema->storage->txn_rollback;
67 code => 'authority_deleted',
70 $report->{total_success}++;
71 $schema->storage->txn_commit;
74 $job->progress( ++$job_progress )->store;
77 my $job_data = decode_json $job->data;
78 $job_data->{messages} = \@messages;
79 $job_data->{report} = $report;
81 $job->ended_on(dt_from_string)
82 ->data(encode_json $job_data);
83 $job->status('finished') if $job->status ne 'cancelled';
88 my ( $self, $args) = @_;
90 # TODO Raise exception instead
91 return unless exists $args->{record_ids};
93 my @record_ids = @{ $args->{record_ids} };
95 $self->SUPER::enqueue({
96 job_size => scalar @record_ids,
97 job_args => {record_ids => \@record_ids,}